如何通过导航抽屉活动中的后退按钮单击从其他片段返回特定片段

时间:2016-08-23 14:42:22

标签: java android

我之前在这里讨论过这个主题,但是对于我的代码来说,它们都没有成果。我做了什么:

Home.java中的

(导航抽屉主要活动)将其用于导航抽屉功能

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

    int id = item.getItemId();
    Fragment fragment = null;
    Class fragmentClass = null;

    if (id == R.id.nav_breakfast) {
        fragmentClass = Breakfast.class;
    } else if (id == R.id.nav_lunch) {
        fragmentClass = Lunch.class;
    } else if (id == R.id.nav_wallet) {
        fragmentClass = Wallet.class;
    } else if (id == R.id.nav_history) {
        fragmentClass = History.class;
    } else if (id == R.id.nav_reset) {
        fragmentClass = ResetPass.class;
    } else if (id == R.id.nav_send) {
        fragmentClass = ContactUs.class;
    } else if (id == R.id.nav_help) {
        fragmentClass = Help.class;
    } else if (id == R.id.nav_about) {
        fragmentClass = AboutUs.class;
    }

    try {
        assert fragmentClass != null;
        fragment = (Fragment) fragmentClass.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
    }

    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).addToBackStack(null).commit();

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    assert drawer != null;
    drawer.closeDrawer(GravityCompat.START);

    return true;
}

通过单击导航抽屉项目选择打开每个声明的片段。另一方面,我还创建了另一个片段,默认情况下,当Home活动实际开始时设置为打开

 if (savedInstanceState == null) {
        Fragment fragment = null;
        Class fragmentClass;
        fragmentClass = AllProduct.class;
        try {
            fragment = (Fragment) fragmentClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

和onBackPressed()我使用了这个:

@Override
public void onBackPressed() {
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    }
    else if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
        getSupportFragmentManager().popBackStack();
    }
    else if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
    }
    else {
        this.doubleBackToExitPressedOnce = true;
        Toast.makeText(this, "Press again to exit", Toast.LENGTH_SHORT).show();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                doubleBackToExitPressedOnce = false;
            }
        }, 2000);
    }

并且所有片段活动都以这种方式编码:

public class AllProduct extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_all_product, container, false);

    return view;
}

@Override
public void onResume() {
    super.onResume();
    getActivity().setTitle(R.string.app_name);
}

}

现在一切正常,只有一个问题。假设我想从导航抽屉打开Fragment [1](不是上面提到的默认片段),然后再次打开导航抽屉并打开Fragment [2],然后打开片段[3]。现在假设我在片段[3]并按下后退按钮我直接想要去上面提到的默认片段。它首先进入Fragment [2],片段[1]然后是默认片段。但我想要的地方,我在哪个片段,我必须通过后退按钮返回默认片段。

2 个答案:

答案 0 :(得分:1)

只需使用

getSupportFragmentManager()
    .beginTransaction()
    .replace(...)
    .addToBackStack("tag").commit()

并删除所有其他代码,尤其是onBackPressed()

中的代码

答案 1 :(得分:0)

Ok finally solve this problem, just remove .addToBackStack(null) from onNavigationItemSelected. Then add:

 getView().setFocusableInTouchMode(true);
    getView().requestFocus();
    getView().setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){
                Fragment fragment = null;
                Class fragmentClass;
                fragmentClass = AllProduct.class;
                try {
                    fragment = (Fragment) fragmentClass.newInstance();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

                return true;
            }
            return false;
        }
    });

in every fragment under onResume()

and delete:

else if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
        getSupportFragmentManager().popBackStack();
    }