1

When my app goes to background and i receive a broadcast that my app is in inconsistent state and i want to ensure that the next time my app comes to foreground, i launch the Launcher(or root) activity. To do this I have the following approaches,

  1. All my activities derive from a base . When resuming check for the state of app in base. Launch Root activity if the state is inconsistent. Problem : App resume should be fast and checking state is a db call. Also there would be a visible flip to Root activity which i want to avoid.

  2. Catch hold of foreground activity in broadcast receiver, clear the task and launch main activity. Problem: I have to maintain a reference to top activity like described here.

  3. Kill the process in broadcast receiver when inconsistent state is reached.

What is the best approach?

Community
  • 1
  • 1
Yagna
  • 423
  • 1
  • 5
  • 15

2 Answers2

3

You could set android:noHistory="true" in your AndroidManifest.xml.

If the user navigates away from your app (it moves into the background), the activity is finished and thus your app will be restarted.


To start the root activit only when an event happens, use this code:
Intent intent = new Intent(context, yourRootActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

This makes sure that all old activities are finished and the newly started activity will be the "root activity" of the new task. Source

Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
0

In your main Activity you could check the state in onCreate() and either continue launching your main Activity or, if your state commands so, immediately call finish() on your main activity and launch whatever appropriate activity should be launched.

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158