0

I want to go back to MainActivity from any activities. For ex, my stack of activities: A - B - C - D. A (MainActivity) is in the bottom and D is in the top of stack .

When I use android:launchMode="singleTask" . I can go back to A at any actvities as I expected.

But when I use flag FLAG_ACTIVITY_NEW_TASK (without launchMode="singleTask"), it does not work as expected, it open a new Activity. And stacks are: A - B - C - D - A not as document wrote:

FLAG_ACTIVITY_NEW_TASK
Start the activity in a new task. If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity receives the new intent in onNewIntent().

This produces the same behavior as the "singleTask" launchMode value, discussed in the previous section.

Anyone knows the reason? Thanks.

RoShan Shan
  • 2,924
  • 1
  • 18
  • 38

5 Answers5

3

I hope this solution solve your problem::- Only set this to Intent

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
Nitin Patel
  • 1,605
  • 13
  • 31
  • Thanks for your answer. I try but not work as I expected. – RoShan Shan Apr 26 '17 at 04:59
  • Thanks very much, I still works with `intentToLaunch.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);`. With 2 flags, it works like `FLAG_ACTIVITY_NEW_TASK` (but this flags not work as expected) – RoShan Shan Apr 26 '17 at 06:28
  • Yes, you r right. Sometimes, flag is not work as per name or use or description. But, experience will teach how they works and solve problem. – Nitin Patel Apr 26 '17 at 06:34
1

Find the solution :

Note : It will clear all previous activity and will lauch HomeActivity

Intent homeActivity = new Intent(context, DJ_HomeActivity.class);
homeActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(homeActivity); 
Rajasekhar
  • 2,345
  • 1
  • 13
  • 20
0

Try this in one.You can add and remove activity from stack like this.

// Add activity
public static void addActivities(String actName, Activity _activity) {
    if (Config.screenStack == null)
        Config.screenStack = new HashMap<String, Activity>();
    if (_activity != null)
        Config.screenStack.put(actName, _activity);
}

// Remove Activity
public static void removeActivity(String key) {
    if (Config.screenStack != null && Config.screenStack.size() > 0) {
        Activity _activity = Config.screenStack.get(key);
        if (_activity != null) {
            _activity.finish();
        }
    }
}

User add activities before setContentView to add into the stack.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addActivities("DemoActivity", DemoActivity.this)
    setContentView(R.layout.activity_create_feed_post);
}

For remove activity you just call the removeActivity() and pass the key which you use when add the activity.

Andy Developer
  • 3,071
  • 1
  • 19
  • 39
  • Sorry but it 's not the answer I want. The answer from you answer `How to remove a particular activity from android back stack?` But with your answser, using HashMap to store stack, cannot remove activity like this : A-B-C-D-C-D. If I want to remove C in the stack, cannot. Because youf HashMap cannot store 2 C activties in Stack – RoShan Shan Apr 26 '17 at 04:47
  • You can store all your activity in this method.For e.g If you go from A-B-C-D-E-F now from F you want to go to activity A then onBackPress you just call removeActivity("Your E activity"), removeActivity("Your D activity") upto B activity and then call finish() the current activity.And that's it you are done. – Andy Developer Apr 26 '17 at 04:55
  • Cannot with your codes above. For ex: with stack A-B-C-D-C-D, your codes using `HashMap` cannot store stack like stack A-B-C-D-C-D, 2 C activities in the stack – RoShan Shan Apr 26 '17 at 04:57
  • From your case you written something like this A - B - C - D - A right. So it is very simple. 1)When you come to A add your activity, When B add your activity and so on. When you Call activity A from D you just call removeActivity("Your A activity") so from this way your activity now create new A activity and you have single stack for activity A. – Andy Developer Apr 26 '17 at 04:59
0

Override onBackpress method of each activity :

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK); 
startActivity(intent);
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Rajesh N
  • 6,198
  • 2
  • 47
  • 58
  • I know `FLAG_ACTIVITY_NEW_TASK` and `Intent.FLAG_ACTIVITY_CLEAR_TASK` It will clear all activities in the task and open new Activities, not go back to the Activities in the stack – RoShan Shan Apr 26 '17 at 04:49
-1

you should use below code to jump on the MainActivity.With the intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) following flag of intent it clears the all previous activity.

  Intent intent = new Intent(this, DashboardActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);

Note one thing, You used android:launchMode="singleTask"So while moving from A activity to another activity you must have to finish A Activity by using this.finish() method.

PRIYA PARASHAR
  • 777
  • 4
  • 15