Android Fragments 在Android中加载多个片段并将其添加到backstack

2sbarzqh  于 2023-08-06  发布在  Android
关注(0)|答案(1)|浏览(153)

我有1个Activity和3个fragments,它们分别是FirstFragmentSecondFragmentThirdFragment
FirstFragment会在MainActivity开启时自动载入。通过单击带有addToBackStack标志的FirstFragment按钮,加载SecondFragmentThirdFragment也是如此。
现在,假设用户在屏幕上显示SecondFragment时关闭应用程序,而当用户再次打开应用程序时,他应直接切换到SecondFragment(在内部,FirstFragment加载比SecondFragment加载更大,带有addToBacStack标志)。
如何做到这一点呢?
这里有一些代码。目前,我无法加载多个片段。
SharePreferences用于当前显示fragment

public class PrefsMain {

    public static final String TAG = Prefs.class.getSimpleName();

    // Strings contains Fragment names BEGIN
    public static final String CurrentFragment_RoleSelectionFragment =
            RoleSelectionFragment.TAG;
    public static final String CurrentFragment_CheckInOutFragment =
            CheckInOutFragment.TAG;
    public static final String CurrentFragment_TeacherRegFragment =
            TeacherRegFragment.TAG;
    public static final String CurrentFragment_StudentSelectionFragment =
            StudentSelectionFragment.TAG;
    public static final String CurrentFragment_StudentFragment =
            StudentFragment.TAG;
    public static final String CurrentFragment_StudentRegistrationFragment =
            StudentRegistrationFragment.TAG;
    public static final String CurrentFragment_DiagnoseFragment =
            DiagnoseFragment.TAG;
    // Strings contains Fragment names END

    // SharedPreferences KEY
    private static final String CurrentFragment = "Fragment";

    private SharedPreferences mPrefs;

    public PrefsMain(Context context) {
        mPrefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
    }

    // Getter - Setter
    public String getCurrentFragment() {
        return mPrefs.getString(CurrentFragment, CurrentFragment_RoleSelectionFragment);
    }

    public void setCurrentFragment(String currentFragment) {
        mPrefs.edit().putString(CurrentFragment, currentFragment).apply();
    }
}

字符串
MainAvtivity.java

import static com.odoo.addons.employees.utils.PrefsMain.CurrentFragment_CheckInOutFragment;
import static com.odoo.addons.employees.utils.PrefsMain.CurrentFragment_DiagnoseFragment;
import static com.odoo.addons.employees.utils.PrefsMain.CurrentFragment_RoleSelectionFragment;
import static com.odoo.addons.employees.utils.PrefsMain.CurrentFragment_StudentFragment;
import static com.odoo.addons.employees.utils.PrefsMain.CurrentFragment_StudentRegistrationFragment;
import static com.odoo.addons.employees.utils.PrefsMain.CurrentFragment_StudentSelectionFragment;
import static com.odoo.addons.employees.utils.PrefsMain.CurrentFragment_TeacherRegFragment;

public class MainActivity extends AppCompatActivity {

    public static final String TAG = MainActivity.class.getSimpleName();

    private PrefsMain mPrefsMain;
    private AppCompatActivity mActivity;

    private RoleSelectionFragment mRoleSelectionFragment;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mActivity = this;
        mPrefsMain = new PrefsMain(this);
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        // simply get the fragment name from `SharedPreferences`.
        // and loads it 
        final String currentFragment = mPrefsMain.getCurrentFragment();
        if (currentFragment.equals(CurrentFragment_RoleSelectionFragment)) {
            loadFragment(RoleSelectionFragment.newInstance(), 
                            RoleSelectionFragment.TAG);
        } else if (currentFragment.equals(CurrentFragment_CheckInOutFragment)) {
            loadFragment(CheckInOutFragment.newInstance(), 
                            CheckInOutFragment.TAG);
        } else if (currentFragment.equals(CurrentFragment_TeacherRegFragment)) {
            loadFragment(TeacherRegFragment.newInstance(), 
                            TeacherRegFragment.TAG);
        } else if (currentFragment.equals(CurrentFragment_StudentSelectionFragment)) {
            loadFragment(StudentSelectionFragment.newInstance(), 
                            StudentSelectionFragment.TAG);
        } else if (currentFragment.equals(CurrentFragment_StudentFragment)) {
            loadFragment(StudentFragment.newInstance(), 
                            StudentFragment.TAG);
        } else if (currentFragment.equals(CurrentFragment_StudentRegistrationFragment)) {
            loadFragment(StudentRegistrationFragment.newInstance(), 
                            StudentRegistrationFragment.TAG);
        } else if (currentFragment.equals(CurrentFragment_DiagnoseFragment)) {
            loadFragment(DiagnoseFragment.newInstance(), 
                            DiagnoseFragment.TAG);
        }
    }

    public void loadFragment(Fragment fragment, String TAG) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.flMain, fragment, TAG)
                .commit();
    }
}


<br/ ><br/ >上面的实现只是加载了最后一个已知的fragment,但它并不对其父fragments执行addToBackStack

siotufzp

siotufzp1#

您没有将事务添加到backstack。将您的交易添加到backstack。

public void loadFragment(Fragment fragment, String TAG) {
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.flMain, fragment, TAG)
            .addToBackStack(TAG)
            .commit();
}

字符串

相关问题