由于addBackStack

时间:2017-09-03 11:30:31

标签: android android-fragments android-preferences onbackpressed fragment-backstack

在我的主要活动中,按“设置”按钮可打开带有FrameLayout的AppCompatActivity作为片段的容器。在OnCreate方法中,我添加的片段是PreferenceScreen容器。

@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();
    }
}

再次按下另一个名为Options的按钮,打开用Options片段替换片段,将当前类添加到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;
}

当我按下向上按钮时,我只是将选项片段替换为第一个片段,即设置,它不会弹出后栈。

@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);
}

因此,再次打开Options片段将向后堆栈和onBackPress添加另一个片段,它只会弹出最顶层并使其阴影与后堆栈中注册的另一个阴影重叠。

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

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

几乎相同的情况就像来自问题的第三张照片的here,但是我不能让它与他们的建议一起工作。

我做错了什么以及我能做些什么让这项工作成功?非常感谢。

1 个答案:

答案 0 :(得分:0)

检查FragmentTransactionSettingsSubscreenFragmentSettingsFragment正在延长android.support.v4.app.Fragment。如果没有,请尝试

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

在您的片段类中。