加载多个片段并将其添加到android

时间:2016-06-06 05:36:10

标签: android android-fragments fragment-backstack

我有1 Activity和3 fragments他们是FirstFragmentSecondFragment&分别为ThirdFragment

FirstFragment打开时自动加载

MainActivity。 通过按住SecondFragmentFirstFragment标记,addToBackStack加载ThirdFragment。同样的事情与SecondFragment相符。

现在,假设用户在其屏幕上显示SecondFragment时关闭了应用,而不是用户再次打开应用时,他应该直接切换到FirstFragment(内部SecondFragment加载比{ {1}}加载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();
    }
}

上面的实现只会加载最后一个已知的fragment但是,它的父addToBackStack不会执行fragments

任何建议或帮助将不胜感激。谢谢你提前。

1 个答案:

答案 0 :(得分:2)

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

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