我的活动使用预先初始化的片段(初始化 onCreate)
当片段 (A) 将首先显示时,使用FragmentTransaction.add(A)。如果片段 B 请求显示(如果是第一个), FragmentTransaction.detach(A)和FragmentTransaction.add(B)。
片段 A 再次请求显示,使用FragmentTransaction.detach(B)和 FragmentTransaction.attach(A)。
此操作在BottomNavagationView.OnNavigationItemSelectedListener中。
在这种情况下,我使应用程序完成(使用后退按钮,但不使用 activity.finish),然后再次运行该应用程序,不显示任何片段(期望片段 B 显示)。
并且,当我单击 BottomNavagationView 按钮(片段添加)时,
导致java.lang.IllegalStateException:在 commit() 的onSaveInstanceState 之后无法执行此操作。
如何解决这个问题呢?
commitallowingstateloss()似乎无法正常工作...
附上我的代码:
public class TestActivity extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView navigation;
private Fragment homeFragment = null;
private Fragment seatFragment = null;
private Fragment settingFragment = null;
private Fragment dialogFragment = null;
private FragmentUtil fUtil = null;
private boolean finishFlag = false;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
fUtil.addOnMain(R.id.content, homeFragment, "home", FragmentTransaction.TRANSIT_FRAGMENT_FADE);
return true;
case R.id.navigation_seat:
fUtil.addOnMain(R.id.content, seatFragment, "seat", FragmentTransaction.TRANSIT_FRAGMENT_FADE);
return true;
case R.id.navigation_shelf:
return true;
case R.id.navigation_settings:
fUtil.addOnMain(R.id.content, settingFragment, "setting", FragmentTransaction.TRANSIT_FRAGMENT_FADE);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
// mTextMessage = (TextView) findViewById(R.id.message);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
BottomNavigationViewHelper.disableShiftMode(navigation);
fUtil = FragmentUtil.getInstance(this);
homeFragment = new HomeFragment();
seatFragment = new SeatMenuFragment();
settingFragment = new SettingFragment();
dialogFragment = new DialogFragment();
findViewById(R.id.login_popup).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(fUtil.isContained(dialogFragment)) {
return;
} else {
fUtil.add(R.id.content, dialogFragment, "", FragmentTransaction.TRANSIT_FRAGMENT_FADE);
}
}
});
}
并且, FragmentUtil :
public class FragmentUtil {
private Context context = null;
private FragmentManager manager = null;
private static FragmentUtil sInstance = null;
private Fragment currentFragment = null;
public static FragmentUtil getInstance(FragmentActivity activity) {
if (sInstance == null) {
sInstance = new FragmentUtil(activity);
}
return sInstance;
}
private FragmentUtil(FragmentActivity activity) {
this.context = activity.getApplicationContext();
this.manager = activity.getSupportFragmentManager();
}
public FragmentManager getManager() {
return manager;
}
private void setTransition(FragmentTransaction ft, int transition) {
ft.setTransition(transition);
}
public void attach(Fragment fragment) {
if(fragment.isDetached()) {
FragmentTransaction ft = manager.beginTransaction();
ft.attach(fragment);
ft.commit();
currentFragment = fragment;
} else {
return;
}
}
public void attach(Fragment fragment, int transition) {
if(fragment.isDetached()) {
FragmentTransaction ft = manager.beginTransaction();
ft.attach(fragment);
if (transition != FragmentTransaction.TRANSIT_UNSET) {
setTransition(ft, transition);
}
ft.commit();
currentFragment = fragment;
} else {
return;
}
}
public void detach(Fragment fragment, int transition) {
if(fragment.isDetached()) {
return;
} else {
FragmentTransaction ft = manager.beginTransaction();
ft.detach(fragment);
if(transition != FragmentTransaction.TRANSIT_UNSET) {
setTransition(ft, transition);
}
currentFragment = null;
ft.commit();
}
}
public void detach(Fragment fragment) {
if(fragment.isDetached()) {
return;
} else {
FragmentTransaction ft = manager.beginTransaction();
ft.detach(fragment);
ft.commit();
currentFragment = null;
}
}
public void add(int resId, Fragment fragment, String tag) {
//manager.executePendingTransactions();
if(fragment.isAdded()) {
return;
} else {
FragmentTransaction ft = manager.beginTransaction();
ft.addToBackStack(null);
ft.add(resId, fragment, tag);
ft.commit();
currentFragment = fragment;
}
}
public void addOnMain(int resId, Fragment fragment, String tag) {
//manager.executePendingTransactions();
if(fragment.isAdded()) {
return;
} else {
manager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentTransaction ft = manager.beginTransaction();
ft.add(resId, fragment, tag);
ft.commit();
currentFragment = fragment;
}
}
public void remove(Fragment fragment) {
//manager.executePendingTransactions();
manager.popBackStack();
FragmentTransaction ft = manager.beginTransaction();
ft.remove(fragment);
ft.commit();
}
public void add(int resId, Fragment fragment, String tag, int transition) {
//manager.executePendingTransactions();
if(fragment.isAdded()) {
return;
} else {
FragmentTransaction ft = manager.beginTransaction();
ft.addToBackStack(null);
ft.add(resId, fragment, tag);
setTransition(ft, transition);
ft.commit();
currentFragment = fragment;
}
}
public void addOnMain(int resId, Fragment fragment, String tag, int transition) {
//manager.executePendingTransactions();
if(fragment.isAdded()) {
return;
} else {
manager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentTransaction ft = manager.beginTransaction();
ft.add(resId, fragment, tag);
setTransition(ft, transition);
ft.commit();
currentFragment = fragment;
}
}
public void remove(Fragment fragment, int transition) {
//manager.executePendingTransactions();
FragmentTransaction ft = manager.beginTransaction();
ft.remove(fragment);
setTransition(ft, transition);
ft.commit();
}
HomeFragment 和其他人什么都不做。只是膨胀xml。