简体   繁体   English

使用导航抽屉进行片段导航

[英]Fragment Navigation with Navigation Drawer

i actually have a little Problem with my Android App. 我的Android应用实际上有一个小问题。

My App Contains a Navigation Drawer which is used to navigate inside the App. 我的应用程序包含一个导航抽屉,用于在应用程序内部进行导航。 For Example 例如

Navigation Drawer: 导航抽屉:

  1. Overview 概观
  2. Search 搜索
  3. Users 用户
  4. Stuff 东西

When the App starts, i will load the Overview fragment. 当应用启动时,我将加载概述片段。 If the user clicks the other Items, i'll change the fragments with: 如果用户单击其他项目,我将使用以下方式更改片段:

final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment, TAG);
transaction.addToBackStack(null);
transaction.commit();

The problem was now, when the user switched, for example, from 1 > 2 > 4 > 3 and them pressing the back key, it will bring him just one fragment back. 现在的问题是,例如,当用户从1> 2> 4> 3切换并按下返回键时,这只会使他返回一个片段。 3 > 4 > 2 > 1 3> 4> 2> 1

So i added the code, which will bring him back to Fragment 1 on each Backpress. 所以我添加了代码,这将使他回到每个Backpress的Fragment 1。

public boolean onKeyDown(int keyCode, KeyEvent event) {
    int exit = 0;


    if (keyCode == KeyEvent.KEYCODE_BACK) {


        getFragmentManager().popBackStack();
        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = new Overview();
        fm.beginTransaction().add(R.id.frame_container, fragment).addToBackStack("fragBack").commit();
        return false;
    }
    return false;
}

Ok, first it was fine, but now i added some more fragments on a Deeper Level, but they should't go back to Fragment 1 on back press. 好的,首先还不错,但是现在我在“更深层次”上添加了更多片段,但是在回压时它们不应该回到片段1。

For Example 例如

  1. Overview (the Main Fragment) 概述(主要片段)
  2. Search (Back Key goes Back to Fragment 1) 搜索(返回键返回到片段1)
  3. Users (Back Key goes Back to Fragment 1) 用户(返回键返回到片段1)
  4. Stuff (Back Key goes Back to Fragment 1) 填充(返回键返回到片段1)

4.1 Details (Back Key goes Back to Fragment 4. Stuff) < 4.1.1 More Details (Back Key goes Back to Fragment 4.1) 4.1详细信息(返回键返回到片段4。填充)<4.1.1更多详细信息(返回键返回到片段4.1)。

Would be nice if somebody has a Idea how i could handle the Navigation in my app. 如果有人有一个想法,我将如何在我的应用程序中处理导航,那就太好了。

Greetz 格尔茨

Your code is correct and need to change the logic. 您的代码是正确的,需要更改逻辑。 Whenever you go from the navigationdrawer, first you clear all the fragments in the stack using this code 每当您离开NavigationDrawer时,首先都要使用此代码清除堆栈中的所有片段

private void clearBackStack() {
    FragmentManager manager = getSupportFragmentManager();
    manager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
} 

and second, add the corresponding selected(1 or 2 or 3) fragment to the stack. 然后,将相应的selected(1或2或3)片段添加到堆栈中。

private void navigateToFragment(Fragment fragment, String TAG){
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.frame_container, fragment, TAG);
    transaction.addToBackStack(null);
    transaction.commit();
}

And when you go deeper like 2 -> 2.1 -> 2.2, on backpress you will return 2.2 ->2.1 ->2. 当您像2-> 2.1-> 2.2那样深入时,在反压时您将返回2.2-> 2.1-> 2。 No need to override the onKeyDown() method and add fragments again 无需覆盖onKeyDown()方法并再次添加片段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM