我的应用有一项主要活动,其中包含5个片段。一切工作正常,除非我要关闭应用程序并重新启动它,然后该应用程序将以黑屏打开(即未加载任何片段),而不是分配的默认片段。因此,我尝试覆盖onStart(),但现在它崩溃了,并显示以下错误:
08-04 10:59:42.748 27413-27413/com.paraway.partner E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.paraway.partner, PID: 27413
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at androidx.fragment.app.FragmentManagerImpl.checkStateLoss(FragmentManagerImpl.java:1536)
at androidx.fragment.app.FragmentManagerImpl.enqueueAction(FragmentManagerImpl.java:1558)
at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:317)
at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:282)
at com.eataway.partner.MainActivity.openOrders(MainActivity.java:42)
at com.eataway.partner.MainActivity.onStart(MainActivity.java:93)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1265)
at android.app.Activity.performStart(Activity.java:6915)
at android.app.Activity.performRestart(Activity.java:6974)
at android.app.ActivityThread.handleSleeping(ActivityThread.java:4864)
at android.app.ActivityThread.access$3600(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2031)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
MainActivity.class 看起来像这样:
public class MainActivity extends AppCompatActivity {
static final String TAG = MainActivity.class.getSimpleName();
private Bundle bundle = new Bundle();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(navigationItemSelectedListener);
openOrders();
}
// Open default/main fragment
private void openOrders() {
bundle.putString("userId", getUser());
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new OrdersFragment()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener = menuItem -> {
// Handling bottom navigation
return true;
};
private String getUser() {
// non relevant code needed in the fragments
return userId;
}
@Override
protected void onStart() {
openOrders();
super.onStart();
}
}
如果要恢复或重新启动应用程序,我只希望能够打开默认片段。
答案 0 :(得分:2)
看到黑屏的原因是因为您要在活动的onCreate()
中启动片段事务,因此根据应用程序的生命周期只能调用一次。如果您的活动在onCreate()
中停止,它将遵循onCreate()
-> onPause()
-> onStop()
-> onSaveInstance()
的模式。
您需要做的是覆盖onPostResume()
并在那里开始片段事务。当活动完全解决并且可见时,将调用此回调。同样,commit()
并不意味着它将立即提交事务,而是安排了时间,这解释了为什么抛出此异常的原因,因为当活动不再可用时会发生提交。
蜂窝后,onStop()
之后的活动被认为可以杀死
@Override
protected void onPostResume() {
super.onPostResume();
if (getSupportFragmentManager().findFragmentById(R.id.fragment_container) == null)
openOrders();
}