我有1 Activity
和3 fragments
他们是FirstFragment
,SecondFragment
&分别为ThirdFragment
。
FirstFragment
打开时自动加载 MainActivity
。
通过按住SecondFragment
并FirstFragment
标记,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
。
任何建议或帮助将不胜感激。谢谢你提前。
答案 0 :(得分:2)
您没有将事务添加到backstack。将您的交易添加到Backstack。
public void loadFragment(Fragment fragment, String TAG) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.flMain, fragment, TAG)
.addToBackStack(TAG)
.commit();
}