0

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

Chandana
  • 2,578
  • 8
  • 38
  • 55
  • You haven't really described your problem. *Why* do you need to call MainActivity from onResume? onResume from *which* Activity? You shouldn't be calling any methods across the boundaries of an Activity. – smith324 Dec 09 '11 at 03:27
  • Let assume user pause the application in ListView or DetailView. When resume the application it's goes to one of them. But without going to those two view, I need to navigate user in to MainActivity. – Chandana Dec 09 '11 at 03:39
  • Do you mean that after exiting the app from whichever of these 3 activities, when going back to your app, the MainActivity should appear intead of other activities? – Huang Dec 09 '11 at 03:40
  • Yes, Exactly that what i need to do. – Chandana Dec 09 '11 at 03:44
  • 1
    call finish() in the onPause() or onStop() in the ListView and DetailView activies, so that the activites will get killed and UI returns to MainActivity – Vamsi Dec 09 '11 at 03:46
  • Can you navigate between ListView and DetailsView activies or does everything flow through the MainActivity? – brianestey Dec 09 '11 at 04:03
  • Please see my update, I just tested and this should work. – Huang Dec 09 '11 at 13:13

3 Answers3

4

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.

Huang
  • 4,812
  • 3
  • 21
  • 20
1

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.

Ring
  • 2,249
  • 5
  • 27
  • 40
1

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.

brianestey
  • 8,202
  • 5
  • 33
  • 48