简体   繁体   中英

onStart() not being called

My app has one primary activity which houses 5 fragments. Everything is working fine except if I were to close the app and restart it, then the app opens with a blank screen (ie no fragment loaded) instead of the assigned default fragment. So I tried to override onStart() but now it is crashing with the following error:

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 looks like this:

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();
    }
}

I just want to be able to open the default fragment if the app were to be resumed or restarted.

The reason you see the blank screen is because you are starting a fragment transaction in onCreate() of your activity, this get's called only one time depending on the lifecycle of the app. If your activity is stopped in onCreate() , it will follow a pattern, onCreate() -> onPause() -> onStop() -> onSaveInstance() .

What you need to do is to override onPostResume() and start your fragment transaction there. This callback will be invoked when the activity is fully settled and is visible. Also, commit() does not mean it will commit the transaction immediately, instead it is scheduled, which explains why this exception is thrown because the commit happens when the activity is no longer available.

Post Honeycomb, activities are considered killable after onStop()

 @Override
protected void onPostResume() {
    super.onPostResume();

   if (getSupportFragmentManager().findFragmentById(R.id.fragment_container) == null)
        openOrders();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM