Going back to previous fragment not working correctly

≯℡__Kan透↙ 提交于 2019-12-12 20:47:04

问题


I am using a navigation drawer and when I select an item, it correctly replaces the previous fragment with the one I have selected, but if I open another one, and then click the back button, it goes to the previous activity instead of the previous fragment, what is wrong that it is doing this?

private void selectItem(int position) {
    Fragment newFragment;
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    switch (position){
        case 0:
            //Update
            newFragment = new UpdateFragment();
            transaction.replace(R.id.content_frame, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();
            listView.setItemChecked(position, true);
            setTitle(navigationDrawerItems[position]);
            break;
        case 1:
            //Maps
            newFragment = new TimetableFragment();
            transaction.replace(R.id.content_frame, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();
            listView.setItemChecked(position, true);
            setTitle(navigationDrawerItems[position]);
            break;
        case 2:
            //Timetables
            newFragment = new TimetableFragment();
            transaction.replace(R.id.content_frame, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();
            listView.setItemChecked(position, true);
            setTitle(navigationDrawerItems[position]);
            break;
        case 3:
            //Notes
            newFragment = new TimetableFragment();
            transaction.replace(R.id.content_frame, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();
            listView.setItemChecked(position, true);
            setTitle(navigationDrawerItems[position]);
            break;
        case 4:
            //About
            newFragment = new AboutFragment();
            transaction.replace(R.id.content_frame, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();
            listView.setItemChecked(position, true);
            setTitle(navigationDrawerItems[position]);
            break;
        case 5:
            //Settings
            newFragment = new SettingsFragment();
            transaction.replace(R.id.content_frame, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();
            listView.setItemChecked(position, true);
            setTitle(navigationDrawerItems[position]);
            break;
    }
    //listView.setItemChecked(position, true);
    //setTitle(navigationDrawerItems[position]);
    drawerLayout.closeDrawer(listView);
}

回答1:


You can use getFragmentManager().popBackStack() method in onBackPressed:

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 0) {
         getFragmentManager().popBackStack();
         getFragmentManager().beginTransaction().commit();
    }
    else {
        super.onBackPressed();
    }
}

Don't forget to add the fragment in BackStack like :

transaction.addToBackStack(null);

Edit :

To show the current fragment name in actionbar, you can get it on onResume of your FragmentActivity :

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    FragmentManager fragManager = this.getSupportFragmentManager();
    int count = this.getSupportFragmentManager().getBackStackEntryCount();

    // Fetch last fragment
    Fragment fragment = fragManager.getFragments().get(count>0?count-1:count);

    // Check with your possible Fragment class
    if( fragment instanceof OneFragment)
    {
        setTitle("OneFragment");
    }
    else if(fragment instanceof TwoFragment)
    {
        setTitle("TwoFragment");
    }
}

Hope it helps ツ




回答2:


You are adding null to the backstack -

transaction.addToBackStack(null);

Replace this null with tag name of your fragment.

It will work.



来源:https://stackoverflow.com/questions/27399036/going-back-to-previous-fragment-not-working-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!