1

My activity consists of navigation drawer and currently have 5 options in the left menu. Which all opens in fragment.

I am looking for a way to keep a stack of all the fragments so when the user presses back button he moves to previous fragment.

Like- Activity consists of drawer menu which have 5 options menu1, menu2, menu3, menu4, menu5 having corresponding fragments F1, F2, F3, F4, F5.

User presses menu1 he is forwarded to F1 Then presses menu2, and then menu4.

When the user is at F4 and he presses back he should be moved to F2 rather than exiting the activity or app.

How can it implemented and example or sample code preferred.

I currently use this code but it does not help me out

FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
       .replace(R.id.frame_container, fragment)
       .addToBackStack(null)
       .commit();
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
WISHY
  • 11,067
  • 25
  • 105
  • 197

3 Answers3

1

I used to replace Fragment like as below :

public void replaceFragment(Fragment fragment, boolean addToBackStack, int transition) {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.activity_main_relative_container, fragment, fragment.getClass().getName());
    ft.setTransition(transition);
    if (addToBackStack) {
        ft.addToBackStack(fragment.getClass().getName());
    }
    ft.commitAllowingStateLoss();
}

// While to replace Fragment
replaceFragment(mFragment, true, FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
// False for not adding Fragment in back stack and true to add.

Hope this helps.

SilentKiller
  • 6,944
  • 6
  • 40
  • 75
  • `ft.commitAllowingStateLoss();` why? although I've run into a problem without this, but it was because I didn't use `onPostResume()`. – EpicPandaForce Apr 07 '15 at 05:34
  • I lose the fragment titles on back press. How do I retain those? – WISHY Apr 07 '15 at 05:39
  • 1
    @WISHY you can set title at `onResume()` of the frament – SilentKiller Apr 07 '15 at 05:42
  • on back press the fragment's on create is called again – WISHY Apr 07 '15 at 06:33
  • if `onCreate()` is calling it mean your fragment is not in backstack. and to setTitle you can setTitle in `onCreateView()` or in `onCreate()` too. – SilentKiller Apr 07 '15 at 06:36
  • /* * Add this transaction to the back stack. * This means that the transaction will be remembered after it is * committed, and will reverse its operation when later popped off * the stack. */ fragmentTransaction.addToBackStack(null); – Atish Agrawal Apr 07 '15 at 13:13
  • @AtishAgrawal please elaborate more why should I use `fragmentTransaction.addToBackStack(null);` – SilentKiller Apr 08 '15 at 04:19
1

I found some workaround for your query :

  1. Override onBackPressed() in code

  2. Use methods related to backstack maintained which contains your all fragment transactions

    public void onBackPressed(){
    
    FragmentManager fm = getFragmentManager();
    
        if (fm.getBackStackEntryCount() > 0) {
    
            Log.i("MainActivity", "popping backstack");
    
            fm.popBackStack();    // this will display last visible fragment
            getActinBar().setTitle(mTitle);    // save your title in some variable and restore here
    
        } else {
    
             Log.i("MainActivity", "nothing on backstack, calling super");
    
             super.onBackPressed();  // system will handle back key itself
    
        }
    
     }
    

Reference answer : this

Community
  • 1
  • 1
Kushal
  • 8,100
  • 9
  • 63
  • 82
  • I lose the fragment titles on back press. How do I retain those? – WISHY Apr 07 '15 at 05:42
  • when your fragment is visibble, you save your fragment title in `mTitle` and restore it when you are going back to that fragment.. I have edited my answer.. add some logic to save your title of every fragment – Kushal Apr 07 '15 at 05:54
  • because it was either in `onStop()` till now.. so while it is starting from stopped it gets `onCreate()` callback – Kushal Apr 07 '15 at 06:38
  • it wasnt in onStop() it was in onPause() – WISHY Apr 07 '15 at 06:49
  • No.. it should not be in `onPause()` possibly.. Have you checked with logs in `onPause()` and `onStop()` ? – Kushal Apr 07 '15 at 07:04
0

You need to add fragment to backstack

private FragmentTransaction m_fragmentTransaction;
FragmentOne m_fragOne = new FragmentOne();
m_fragmentTransaction = m_fragmentManager.beginTransaction();
m_fragmentTransaction.replace(R.id.dl_flContainer, m_fragOne , FragmentOne.class.getSimpleName());
m_fragmentTransaction.addToBackStack(FragmentOne.class.getSimpleName());
        m_fragmentTransaction.commit();