0

I have the following workflow in my application:

Activity 1 (a ListView) -> Activity 2 (a ListView which loads Fragments) -> Fragment 1 -> Fragment 2.

It all worked out beautifully, I could go forward to Fragment 2, then press the Back button to traverse backwards each step.

At some point I needed to add an ActionBar. Due to some weird programming on behalf of google, I have to extend ActionBarActivity which imports android.support.v7.app.ActionBarActivity, which requires me to change all my Fragment managers to 'support' which import android.support.v4.app.FragmentManager.

The result, is that I have an ActionBar through out my workflow, but I can only go forward to Fragment 2. Any press of the Back button, results in Activity 1 appearing, there is no BackStack.

So, is having Actions/Fragments with an ActionBar and a BackStack impossible, or is my BackStack broken for another reason?

Any help would be appreciated.

Thank you!

Jimba
  • 57
  • 5
  • You only need to extend ActionBarActivity if you need to support api levels < 11, just in case you didn't know. – ci_ Mar 11 '15 at 22:56
  • My minimum level is 16, but I couldn't find any other way to force the ActionBar to appear. I tried various hacks found in various posts, but none of them could force the ActionBar, so I ended up using ActionBarActivity. – Jimba Mar 12 '15 at 00:34

2 Answers2

0

If you are using FragmentTransaction calls like add(), replace(), etc., you call addToBackStack() on the FragmentTransaction before you call commit(). This will set up so that the back button will work with the fragments as you would expect.

kris larson
  • 30,387
  • 5
  • 62
  • 74
0

I solved the problem. Apparently, ActionBarActivity does not call the usual onBackPressed(), and neither does it work with the various other hacks.

What I found, is that the back button is only captured by onOptionsItemSelected(), like:

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if(id == android.R.id.home) {
        // use popBackStack() and super.onBackPressed()
        return true;
    }
    // check for other button presses...
}
Jimba
  • 57
  • 5