2

I have this scenario: in Activity A, it navigate as this order: fragment F1 -> fragment F2, then, clicking a button in F2, it launched another Activity B.

Now what I want to achieve: when hitting the "Back" button in Device while in Activity B, I want to go back F1 in Activity A.

Please note that this involves 2 activities, not back in the same activity. I am thinking to pop up the last fragment F2 when leaving Activity A. But I couldn't figure out how to do that. I tried to add these in onSaveInstanceState(Bundle outState) in Activity A:

fragmentManager.popBackStackImmediate();

But it seems not to be working at all.

CaptJak
  • 3,592
  • 1
  • 29
  • 50
Shawn Chen
  • 95
  • 10
  • Activities still exist after starting a new activity. You should be able to use the fragmentManager to replace F2 after the new activity starts. If you are using default transitions then the transition time is 300 ms. You can post a runnable to perform your fragment operation 300 ms after beginning the new activity. – Helix Feb 23 '16 at 20:29
  • @Helix thanks for your reply. Can you provide some code segments? All examples I could find handle a similar requirement when navigating within the same activity. Here, I have 2 activities. What I want is that when leaving activity A, pop-up F2 from Activity A so that when it comes back to Activity A, F1 will be present instead of F2. Is this feasible at all? – Shawn Chen Feb 23 '16 at 21:46

2 Answers2

0

This is a little more elegant way of doing it...

I forgot about the onStart()... it is called when the activity is first started. To avoid the fragment being popped upon first opening the application a polymorphic check is applied in the if statement

public class MainActivity extends AppCompatActivity {

    private FrameLayout contentFrame;
    private FragmentManager fm;
    private Button nextButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        contentFrame = (FrameLayout) findViewById(R.id.content_frame);

        fm = getSupportFragmentManager();

        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.content_frame, new F1()).commit();

        nextButton = (Button) findViewById(R.id.next);

        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(fm.findFragmentById(R.id.content_frame).getClass() == F1.class) {
                    FragmentTransaction ft = fm.beginTransaction();
                    ft.addToBackStack("F2");
                    ft.add(R.id.content_frame, new F2());
                    ft.commit();
                } else {
                    startActivity(new Intent(MainActivity.this, ActivityTwo.class));
                }
            }
        });

    }

    @Override
    protected void onStart() {
        super.onStart();
        if(fm.findFragmentById(R.id.content_frame).getClass() == F2.class) {
            fm.popBackStackImmediate("F2", FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
    }
}

Best of luck. Code has been tested as working.

Helix
  • 144
  • 6
0

Thanks to this post, I am able to get what I want: in Activity A:

@Override
protected void onSaveInstanceState(Bundle outState) 
{

        FragmentManager manager = getSupportFragmentManager();
        FragmentManager.BackStackEntry first = manager.getBackStackEntryAt(0);
        manager.popBackStack(first.getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);

        super.onSaveInstanceState(outState);

}
Community
  • 1
  • 1
Shawn Chen
  • 95
  • 10