Hi!

I remember the pain with fragments and the back stack while working on an 
app last year. My solution was the following. In the activity's onCreate 
method I would do the following:

FragmentManager fm = getSupportFragmentManager();
> fm.addOnBackStackChangedListener(new OnBackStackChangedListener() {
>     @Override
>     public void onBackStackChanged() {
>         FragmentManager fm = getSupportFragmentManager();
>
>         if(fm.getBackStackEntryCount() == 0) {
>             finish();
>         }
>     }
> });
>

It basically listens to fragment back stack changes and forces the Activity 
to finish when the stack is empty

On Sunday, August 4, 2013 4:47:47 AM UTC-5, user123 wrote:
>
> Im trying to implement a fragment based navigation pattern, which can be 
> reduced to this:
>
>
> <https://lh4.googleusercontent.com/-Ws0I4LcPFNA/Uf4irMURI3I/AAAAAAAAA0s/D1wYtJBCwXc/s1600/screen2.png>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> <https://lh4.googleusercontent.com/-4CuK_LmDSxA/Uf4iuRVg4cI/AAAAAAAAA00/ht6e2Pt1y48/s1600/sk.png>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> In words: There is:
>
>    - 
>    
>    Home fragment
>    - 
>    
>    Fragment A
>    - 
>    
>    Fragment B
>    
> The fragments are shown in FrameLayout "content". The navigation bar is 
> part of the activity.
>
> Workflow should be: Press back on home: Exit App. Press back on A or B: 
> show Home.
>
> Most of it works as it should, but I have the problem that with a complex 
> workflow like this:
>
> Start(home) - A - B - A - Back - B - Back
>
> I expect that it will show home, but instead it shows A. I think it's 
> because the first transaction (home - A) it's in the backstack and it shows 
> A. But I don't understand why then it works correctly in the simple cases?
>
> The code:
>
> public class MainActivity extends FragmentActivity {
> boolean isInHome; //a flag to control when we add fragment to the backstack 
> or not
> @Overrideprotected void onCreate(Bundle savedInstanceState) {
>     super.onCreate(savedInstanceState);
>     setContentView(R.layout.activity_main);
>
>     //set home fragment programmatically first time
>     FragmentTransaction transaction = 
> getSupportFragmentManager().beginTransaction();
>     Fragment fragmentHome = new FragmentHome();
>     transaction.replace(R.id.content, fragmentHome);
>     transaction.commit();
>
>     isInHome = true;
>
>     //initialize layout
>     Button home = (Button) findViewById(R.id.home);
>     Button a = (Button) findViewById(R.id.a);
>     Button b = (Button) findViewById(R.id.b);
>
>     home.setOnClickListener(new View.OnClickListener() {
>         @Override
>         public void onClick(View v) {
>             //clear complete backstack (in this case, just pop home 
> transaction)
>             getSupportFragmentManager().popBackStackImmediate(null, 
> FragmentManager.POP_BACK_STACK_INCLUSIVE);
>
>             //show home fragment
>             FragmentTransaction transaction = 
> getSupportFragmentManager().beginTransaction();
>             Fragment fragmentHome = new FragmentHome();
>             transaction.replace(R.id.content, fragmentHome);
>             transaction.commit();
>
>             isInHome = true;
>         }
>     });
>
>     a.setOnClickListener(new View.OnClickListener() {
>         @Override
>         public void onClick(View v) {
>             FragmentTransaction transaction = 
> getSupportFragmentManager().beginTransaction();
>             Fragment fragmentA = new FragmentA();
>             transaction.replace(R.id.content, fragmentA);
>             if (isInHome) { //add the home transaction to the backstack, such 
> that press back on this fragment shows home again
>                 transaction.addToBackStack("home");
>             }
>             transaction.commit();
>             isInHome = false;
>         }
>     });
>
>     b.setOnClickListener(new View.OnClickListener() {
>         @Override
>         public void onClick(View v) {
>             FragmentTransaction transaction = 
> getSupportFragmentManager().beginTransaction();
>             Fragment fragmentB = new FragmentB();
>             transaction.replace(R.id.content, fragmentB);
>             if (isInHome) { //add the home transaction to the backstack, such 
> that press back on this fragment shows home again
>                 transaction.addToBackStack("home");
>             }
>             transaction.commit();
>             isInHome = false;
>         }
>     });}
> @Overridepublic void onBackPressed() {
>     super.onBackPressed();
>
>     isInHome = true; //quick solution to update status - when press back on A 
> or B, we are in home (and press back in home exits the app, so this doesn't 
> matter)}
>
> }
>
> What's wrong here?
>
> Thanks in advance.
>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to