OnStop() is called after onStart()

893 Views Asked by At

In my application I am using robospice. So in my BaseActivity a created spice manager:

@Override
    protected void onStart() {
        super.onStart();
        if(!spiceManager.isStarted()) {
            spiceManager.start(this);
        }
    }

    @Override
    protected void onStop() {
        if (spiceManager.isStarted()) {
            spiceManager.shouldStop();
        }
        super.onStop();
    }

But when I start new activity, in new activity is called onStart() before parent onStop() - so it disconnect the service. Any ideas how to solve it?

2

There are 2 best solutions below

2
Muneef M On

Am not sure if i understood your question correctly. But if i did i guess you have problem as your BaseActivity onStop is called after you start new activity.

well in this case you can use onResume and onPause instead of onStart and onStop.

@Override
     protected void onResume() {
        super.onResume();
         if(!spiceManager.isStarted()) {
             spiceManager.start(this);
         }
     }

    @Override
    protected void onPause() {
         super.onPause();
         if (spiceManager.isStarted()) {
             spiceManager.shouldStop();
         }

     }

Hope this helps.

0
Taras Yurkiv On

Just add some boolean flag and switch it before starting another activity. Also, change your onStop method to this:

protected void onStop() {
    if (spiceManager.isStarted() && !mIsStartedNewActivity) {
        spiceManager.shouldStop();
    }
    super.onStop();
}