0

I'm having problem with managing activities in history stack.

Background:

I've two Activities: Main and Preference.

I've Broadcast receiver that starts a notification at boot. The notification starts Main activity. The flags are: Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP for the intent and for notification Notification.FLAG_AUTO_CANCEL. (In most tutorial, broadcast receiver started a service which started notification. But I'm starting notification directly on broadcastreceiver. It's working fine for me but just a heads up.)

The intent for starting preference activity has no flags set.

Problem

  • Start Main activity from Launcher and open Preference activity.
  • While in Preference activity click notification which launches Main activity again. Open preference activity again.
  • Press Home and launch the application which opens Main activity.
  • Now if I click back instead of exiting the app I get to Preference activity(the one opened earlier) and another back moves me to Main activity(the one opened earlier).

In my last app I had used activityMode="singleInstance" to get rid of new instance of activities getting created from notification. But I want to fix this one with better way, i.e. using correct Flags.

amol
  • 75
  • 6

1 Answers1

0

Override the onBackPressed method in Main activity to explicitly finish() any Main and Preference Activities.

donkey
  • 204
  • 1
  • 7
  • While this will work I don't think this is an ideal way of handling this situation. – amol Aug 20 '13 at 08:47
  • Well, ideally when you press Home button and launch the app, it should have saved the state, which means it should get back to the Preference activity. This would require implementation in onPause and onResume. Also, ideally we would like to be able to set a flag when launching from the launcher, as discussed in this thread: http://stackoverflow.com/questions/6650514/how-to-add-flags-with-my-intent-in-the-manifest-file – donkey Aug 20 '13 at 09:52
  • The back key is doing its job. It is closing the correct activity on back key. Problem here is when I start new activity from settings instead of resuming new instance is being created. – amol Aug 20 '13 at 11:53
  • Well, now i'm confused about your original question. If you pressed home and launch your app again, it should go back to the Preference Activity, since that is where you left off. Either way, if you started Main Activity from Preference Activity with the correct flag FLAG_ACTIVITY_CLEAR_TOP, then yes a new instance is created, but that should've also cleared the stack. Check out common problems with this here: http://stackoverflow.com/questions/11945274/android-intent-flag-activity-clear-top-seems-doesnt-work – donkey Aug 21 '13 at 10:36
  • No luck with this as well. – amol Aug 22 '13 at 03:17