简体   繁体   中英

onStop not being called … Home button pressed

In my main activity, under onStop, I set myVar = true. In onResume, I check if myVar = true and do something.

If you hit the home button while on my main activity and launch it again from the home screen/app drawer, it works correctly.

If you hit the home button while on a different activity and launch it again from the home screen/app drawer, it brings up the main activity as I have clearTaskOnLaunch="true" set on my main activity and android:finishOnTaskLaunch="true" set on my other activities. However, it doesn't appear that it hit onStop in the main activity when the Home button was pressed.

I start the other activities for result. If result code = result_ok or result_canceled, I set myVar = false. But... if the home button is pressed, it shouldn't be setting results and doing the onActivityResult.

Any idea how to solve this?

Edit:

above onCreate.. I set startNew = true;
@Override
    public void onStop() {
        super.onStop();
        startNew = true;
    }

    @Override
    public void onRestart() {
        super.onRestart();
        if (startNew) {
            getCurrentDate(0);
            updateDisplay();
            fillData();
        }
    }

The only other reference to startNew, is in my onActivityResult method, if an activity that was launched from my main activity returns either result_ok or result_canceled, I set startNew = false.

I'm trying to ensure that every time my app is launched from home screen / app drawer, that it runs those 3 methods.

转到onPause()因为根据文档,在某些情况下不会调用onStop()。

What are you attempting to do in onStop? Typically you want to do it in the onPause method. The closer you get to onDestroy in the lifecycle, the less likely it is that the method will be called before your Activity is destroyed to recover memory.

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