0

From my main activity, pressing Settings button opens an AppCompatActivity with FrameLayout as the container for the fragments. In OnCreate method, I'm adding the fragment which is a PreferenceScreen container.

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.custom_toolbar);
    setSupportActionBar(toolbar);

    ActionBar ab = getSupportActionBar();
    if (ab != null) {
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setTitle("Settings");
    }


    FragmentManager fragmentManager = getSupportFragmentManager();
    Fragment fragment = null;
    if (savedInstanceState == null) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        fragment = SettingsFragment.newInstance(null);
        fragmentTransaction.add(R.id.fragment_container, fragment);

        fragmentTransaction.commit();
    }
}

Pressing again another button called Options, opens replaces fragment with Options fragment adding the current class to backstack.

@Override
public boolean onPreferenceStartScreen(PreferenceFragmentCompat caller, PreferenceScreen pref) {

    FragmentManager manager = getSupportFragmentManager();

    SettingsSubscreenFragment fragment = null;
    Bundle args = new Bundle();

    switch (pref.getKey()) {
        case "pref_key_rejection_options":
            getSupportActionBar().setTitle("Rejection Options");
            fragment = SettingsSubscreenFragment.newInstance("Options");

            break;
    }

    args.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, pref.getKey());

    if (fragment != null) {
        fragment.setArguments(args);
    }

    FragmentTransaction ft = manager.beginTransaction();
    ft.replace(R.id.fragment_container, fragment, pref.getKey());
    ft.addToBackStack(null);
    ft.commit();

    return true;
}

When I press the Up Button, I'm just replacing the Options fragment to the first fragment, Settings, which does not pop the back stack.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            getSupportActionBar().setTitle("Settings");

            FragmentManager fragmentManager;
            FragmentTransaction ft;
            Fragment fragment = null;

            if (getSupportFragmentManager().findFragmentByTag("pref_key_options") != null) {
                fragmentManager = getSupportFragmentManager();
                ft = fragmentManager.beginTransaction();                 

                fragment = SettingsFragment.newInstance(null);


                ft.replace(R.id.fragment_container, fragment);
                ft.commit();
                fragmentManager.getFragments().clear();


                return true;
            } else {
                return super.onOptionsItemSelected(item);
            }

    }
    return super.onOptionsItemSelected(item);
}

So, opening the Options fragment again will add another to back stack and onBackPress, it only pops the topmost and leaves its shadow overlapping the other one registered in back stack.

@Override
public void onBackPressed() {
    int backStackCount = getSupportFragmentManager().getBackStackEntryCount();

    if (backStackCount >= 1) {
        getSupportActionBar().setTitle("Settings");
        getSupportFragmentManager().popBackStack();
    } else {
        super.onBackPressed();
    }
}

The almost the same situation is like from here, third picture of the question, but I can't make it work with their suggestions.

What am I doing wrong and what I can do to make this work? Thanks a lot.

Mr. Nacho
  • 315
  • 1
  • 3
  • 13

1 Answers1

0

Check FragmentTransaction , SettingsSubscreenFragment and SettingsFragmentare extending android.support.v4.app.Fragment . If not , try

class YourFragments extends android.support.v4.app.Fragment 

in your fragment classes.

Vinayak B
  • 4,430
  • 4
  • 28
  • 58
  • I'm using `PreferenceFragmentCompat` for those Fragments and `AppCompatActivity` for the activity holding the container for those fragments. For the `FragmentTransaction`, I'm also using support library. – Mr. Nacho Sep 04 '17 at 02:12