我有1个Activity
和3个fragments
,它们分别是FirstFragment
、SecondFragment
和ThirdFragment
。FirstFragment
会在MainActivity
开启时自动载入。通过单击带有addToBackStack
标志的FirstFragment
按钮,加载SecondFragment
。ThirdFragment
也是如此。
现在,假设用户在屏幕上显示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
。
1条答案
按热度按时间siotufzp1#
您没有将事务添加到backstack。将您的交易添加到backstack。
字符串