简体   繁体   中英

I have a Navigation Drawer and want open another isolated fragment

I have a navigation drawer and when i click on one button i want to go to other fragment where i can't access to the navigation drawer and anything else. I just want have a black arrow in the top-left to go back and return to the navigation drawer with all the fragments. I tried somethings but don't works like i want normally navigator continues accessible and see bot layouts one over the other.

MAIN FRAGMENTS CONTROLLER

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = findViewById(R.id.toolbar);
        DrawerLayout drawerLayout=findViewById(R.id.drawer);
        NavigationView navigationView=findViewById(R.id.nav_view);

        setSupportActionBar(toolbar);

        appBarConfiguration= new AppBarConfiguration.Builder(R.id.nav_home,
                R.id.nav_user_data,R.id.nav_join_group,
                R.id.nav_user_groups,R.id.nav_search_documents)
                .setDrawerLayout(drawerLayout).build();

        navController = Navigation
                .findNavController(this,R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this,navController,appBarConfiguration);
        NavigationUI.setupWithNavController(navigationView,navController);



    }

    @Override
    public boolean onSupportNavigateUp() {
        return NavigationUI.navigateUp(navController,appBarConfiguration)
                || super.onSupportNavigateUp();
    }

I'm trying with a botton on my ActionBar, when i click it should go to my new fragment (with a generic app back but with one back button)

You simply should specify your action in navigation.xml file like others.

It will change hamburger icon by itself because you synchronize your navigation controller with NavDrawer in this two lines of code:

NavigationUI.setupActionBarWithNavController(this,navController,appBarConfiguration);
NavigationUI.setupWithNavController(navigationView,navController);

We need to add one fragment in our navigation.xml (the file where we define our fragments associated with the Fragment and the layout) for the Fragment where we want to go. Add there an action with a unique id and where we want to go in the fragment where we are.
Sorry, i know it's a little bit confused.

NAVIGATION
We go from there

<fragment
        android:id="@+id/nav_user_data"
        android:name="com.example.androidapplication_reto2.project.activities.navigationfragments.SeeAndModifyUserDataFragment"
        android:label="User Data"
        tools:layout="@layout/fragment_see_and_modify_user_data"
        >
        <action
            android:id="@+id/action_nav_user_data_to_nav_modify_data"
            app:destination="@+id/nav_modify_data" />
    </fragment>`

To there

<fragment
        android:id="@+id/nav_modify_data"
        android:name="com.example.androidapplication_reto2.project.activities.navigationfragments.ModifyUserDataFragment"
        android:label="Modify User Data"
        tools:layout="@layout/fragment_modify_user_data"
        />

With the action that we defined in the origen fragment.
ORIGEN FRAGMENT

And in the Fragment when you click on one button just happen this.

Navigation.findNavController(getView()).navigate(R.id.action_nav_user_data_to_nav_modify_data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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