Implementing proper back navigation and home button handling using Toolbar in Android

前端 未结 6 1823
粉色の甜心
粉色の甜心 2021-01-30 23:31

I am using a single activity and multiple fragments(screenshot attached) within the same activity to provide a seamless navigation. But after implementing the latest toolbar and

6条回答
  •  既然无缘
    2021-01-31 00:32

    Try something like this:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar()!=null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    
        final ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(drawerToggle);
        drawerToggle.syncState();
    
        final View.OnClickListener originalToolbarListener = drawerToggle.getToolbarNavigationClickListener();
    
        final View.OnClickListener navigationBackPressListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
            }
        };
    
        getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
            @Override
            public void onBackStackChanged() {
                if (getFragmentManager().getBackStackEntryCount() > 0) {
                    drawerToggle.setDrawerIndicatorEnabled(false);
                    drawerToggle.setToolbarNavigationClickListener(navigationBackPressListener);
                } else {
                    drawerToggle.setDrawerIndicatorEnabled(true);
                    drawerToggle.setToolbarNavigationClickListener(originalToolbarListener);
                }
            }
        });
    
        // Though below steps are not related but I have included to show drawer close on Navigation Item click. 
    
        navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                int id = item.getItemId();
                /**
                 * handle item clicks using id
                 */
                drawer.closeDrawer(GravityCompat.START);
                return true;
            }
        });
    }
    

    Handle the drawer state onBackPressed:

    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
    

    To reload previous fragment on back press, always add the fragment transaction to back stack like this:

    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    SomeFragment fragmentToBeLoaded = new SomeFragment();
    fragmentTransaction.replace(R.id.fragment_container, fragmentToBeLoaded,
                    fragmentToBeLoaded.getName());
    fragmentTransaction.addToBackStack(fragmentToBeLoaded.getName());
    fragmentTransaction.commit();
    

    To dynamically change the page title, you can call this from every Fragments onStart or onResume method:

    @Override
    public void onStart() {
       super.onStart();
       getActivity().setTitle("Title for fragment");
    }
    

    Note: I have considered standard layout declaration and thus I have not included any layouts.

提交回复
热议问题