2

I have a problem with maintaining the activities.

I have activity A, and when starting Activity B from Notification, I should end Activity A, and start Activity B.

In other words, At a same time, in stack, Only Activity A or Activity B should be present. Its' very easy achieve when am starting Activity B from Activity A, by calling finish() in Activity A. But cannot achieve the same when starting activity from notification.

Stack should be like this,

  • MainActivity -- Activity A
  • MainActivity -- Activity B (When starting activity B from notification)

But My Stack looks like,

  • MainActivity -- Activity A -- Activity B

What Intent flags or launch mode should be used for this.

Boopathy
  • 367
  • 1
  • 7
  • 16
  • 1
    Have you tried the accepted answer from here? http://stackoverflow.com/questions/12403364/clear-activity-stack-and-start-new-activity-in-android – Nayan Nov 18 '15 at 18:08

4 Answers4

1

ClearTaskOnLaunch

If this attribute is set to "true" in the root activity of a task, the stack is cleared down to the root activity whenever the user leaves the task and returns to it.

Vishal Gaur
  • 658
  • 6
  • 18
0

Use your notification to send a PendingIntent to Activity A, process it in onNewIntent() and have it start Activity B, finishing Activity A as you described.

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33
  • Ya this sounds good.. But in my case, there are about 4 activities.. out of which only one should opened at a time.. Should I put all those 4 activities in a single task and clear the task each time I initiate another activity? – Boopathy Nov 20 '15 at 15:53
  • 1
    I'd suggest looking into using Fragments. You can use a single activity and just present the appropriate Fragment based upon the incoming `Intent`. – Larry Schiefer Nov 20 '15 at 16:09
0

why don't you call finish() in onStop() of both activities,

in this manner you will always have one activity On, down side of it as soon as you go somewhere apart from these activity they will be closed and you will loss that data,

looking as your current requirement I think only one Activity should live so this might solve your problem

Pankaj Nimgade
  • 4,529
  • 3
  • 20
  • 30
0

You could try setting

android:launchMode="singleTask"

in your manifest where you declare your activites.

See documentation of launch modes.

Emanuel Seidinger
  • 1,272
  • 1
  • 12
  • 21