简体   繁体   中英

Android avoid OnResume

I have 3 Acivity based application, It's work flow like this MainActivity ListView and DetailView . when onResume event trigger , need to call MainActivity. without going to other two activity.

Is there any way to call MainActivity when onResume event trigger?

Thank You

You can set the android:clearTaskOnLaunch="true" attribute for you MainActivity in the AndroidManifest.xml file. See here to find why and more details. I think this is the most convenient way to meet your demand.

Edit: I just tested and found this only works when you exit the app and launch the app from the app drawer( NOT long press on HOME and select the app ).

If you want to always bring the root activity to the front, no matter when you re-launch the app or from the recent screen. You can declare "android:launchMode="singleTask" for the root activity, here, the MainActivity.

The best solution I can think of is to start the activity again in the onResume of all your other activities:

@Override
public void onResume() {
    Intent myIntent = new Intent(this, MainActivity.class);
    startActivity(myIntent);
}

The user will still be able to hit the back button and go back to the previous activity, however.

If you want to quit your List/Details views when the user closes your app, have them finish() themselves in their onPause which is called when your Activity is closed.

The only caveat here is that calling finish() will move it one Activity back in the ActivityStack so if your MainActivity isn't the one launching the List/Details views, it will not go back to the MainActivity. In this case, you could specify in the AndroidManifest.xml

<activity android:name="sample.activity.MyActivity" android:noHistory="true" />

to prevent the List/Details activities from ever being put into the history.

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