Pergunta

I have an ActionBarActivity that has a Navigation Drawer. When opening the app, the default fragment shown is Fragment A.

Fragment B (which is another selection on the nav drawer), there is a ListView that when clicked on an item, will open Fragment C in the same container. When I am pressing the back button in Fragment C, instead of going to Fragment B, it goes to Fragment A.

Also, when I click on an item in the nav drawer, I clear the entire backstack. Not sure if I am doing this right either. I have included the code below.

Can't figure why this is happening!

@Override
public void onBackPressed() {
    if (getSupportFragmentManager().getBackStackEntryCount() == 0) { showExitDialog(); }
    else { super.onBackPressed(); }
}

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fm = getSupportFragmentManager();
    switch (position) {
    case 0:     // User Profile
        fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        fm.beginTransaction().replace(R.id.container, 
                ProfileFragment.newInstance(GlobalMethods.readCurrentUserObject(this)), 
                "profileFragment").commit();
        return;
    case 1:     // Find Match
        fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        fm.beginTransaction().replace(R.id.container, DashboardFragment.newInstance(), "dashboardFragment").commit();
        return;
    case 2:     // Match Settings
        fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        fm.beginTransaction().replace(R.id.container, MatchSettingsFragment.newInstance(), 
                "matchSettingsFragment").commit();
        return;
    case 3:     // Previous Matches
        fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        fm.beginTransaction().replace(R.id.container, PreviousMatchesFragment.newInstance(), 
                "previousMatchesFragment").commit();
        return;
    case 4:     // Messages
        fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        fm.beginTransaction().replace(R.id.container, LatestMessagesFragment.newInstance(), 
                "latestMessagesFragment").commit();
        return;
    case 5:     // Settings
        return;
    case 6:     // Log Out
        startActivity(new Intent(this, LoginActivity.class));
        finish();
        return;
    }
}

UPDATE 1

listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            FragmentManager fm = getActivity().getSupportFragmentManager();
            android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.container, ProfileFragment.newInstance(users.get(position)), "profileFragment");
            ft.addToBackStack(null);
            ft.commit();
        }
    });
Foi útil?

Solução

Try this:

ft.add(R.id.container, ProfileFragment.newInstance(users.get(position)), "profileFragment");
ft.addToBackStack(reference to B);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top