3

The main activity opens the main_fragment with this transaction:

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

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.fragment_main_container, new MainFragment())
                .commit();
    }

Then I replace that fragment with another one like this:

// method to handle Conversions button click
public void addConversionsFragment (View v) {
    // replace the main fragment with the conversion fragment
    UnitConversionFragment newFragment = new UnitConversionFragment();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    // Replace whatever is in the fragment_container view with this fragment,
    transaction.replace(R.id.fragment_main_container, newFragment);
    // and add the transaction to the back stack so the user can navigate back
    transaction.addToBackStack(null);
    // Commit the transaction
    transaction.commit(); 
}

Originally, I was using support.v4.app.fragment, and everything was working as expected (back button in the second fragment would pop that one out and return to the MainFragment. But then I decided to implement a PreferenceFragment elsewhere, which the support library didn't seem to like. So I converted the whole project to the regular app.fragment by deleting all the support imports, replacing with the regular imports, then editing all the getSupportFragmentManager() with getFragmentManager(), etc.

Good news is thePreferenceFragment works well, however any time I hit the back button in a fragment, it closes the hosting activity rather than reversing the transaction.

I did many searches and it seems that I am implementing the code correctly, but it is just not responding as I am expecting. Is there more involved in converting away from the support library? Or am I missing something else obvious? I saw a lot of answers out there overriding the onBackPressed(), but I really don't want to do that.

Is there some fundamental difference between the v4 support library and the regular library that requires me to handle the fragment transactions differently?

tshepang
  • 12,111
  • 21
  • 91
  • 136
user3127074
  • 33
  • 1
  • 3

1 Answers1

0

Preference Fragment has a bit of extra logic to handle hierarchical preferences. You can configure it to launch sub fragment screens, and navigate back, as demonstrated here.

<PreferenceScreen android:title="Sub Preferences"
        android:fragment="com.example.SettingsDemo.SubPrefFragment"/>

For normal fragments , fragment back-stack pops first before reaching Activity back stack. This is what is documented.

The code in both android.app.Activity and android.support.v4.app.FragmentActivity is exactly same:

 /**
 * Take care of popping the fragment back stack or finishing the activity
 * as appropriate.
 */
public void onBackPressed() {
    if (!mFragments.popBackStackImmediate()) {
        finish();
    }
}

The only reason it is not happening as expected is that something else consumes "back press", this may happen when there are 3 levels of components:

  1. The Activity is inside an ActivityGroup.
  2. Fragment is inside another Fragment. (for this, there are some bugs with v4 fragments).
Community
  • 1
  • 1
S.D.
  • 29,290
  • 3
  • 79
  • 130
  • I'm not sure I follow. The issue I am having is with any/all of my fragments, not just the PreferenceFragment. – user3127074 May 02 '14 at 04:34
  • @user3127074 added more info to answer. – S.D. May 02 '14 at 04:56
  • I think you are right, but I am not sure what was consuming the back press. At the moment, I have the app working, but I am not sure why. When I created the project, eclipse automatically – user3127074 May 02 '14 at 06:06
  • When I created the project, eclipse automatically extended the ActionBarActivity (appcompat_v7 library) and I just went with it. My min sdk is 11, so in my understanding, I should not need to use the compatability library. I decided to try removing all reference to that library (changed extends ActionBarActivity to extends Activity). In doing so, I found two instances of getSupportFragmentManager() that I missed when I was taking out the v4 support library. So I fixed those also, and now it works. My guess is my issue had to do with those support library references and not appcompat lib. – user3127074 May 02 '14 at 06:21