Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: open activity without save into the stack

I have 2 activities: Main and List.

From Main you can open List; from List you can open Main.

I'd like it so that every opening of List does not get saved into the 'history'. So, pressing back from Main cannot return to List.

Is it possible?

like image 730
realtebo Avatar asked Sep 10 '12 19:09

realtebo


People also ask

How do I start an activity without intent?

There is no way to start an activity from anotherone without an intent. Show activity on this post. Inside onDestroy() method which checks whether the application is closing by the call finish() returning true or otherwise by anything else returning false then you can easily specify your code for each situation.

Is it possible to have an activity without UI layout?

Explanation. Generally, every activity is having its UI(Layout). But if a developer wants to create an activity without UI, he can do it.

How do I start activity and clear back stack?

Declare Activity A as SingleTop by using [android:launchMode="singleTop"] in Android manifest. Now add the following flags while launching A from anywhere. It will clear the stack.


2 Answers

When starting your list's Activity, set its Intent flags like so:

Intent i = new Intent(...); // Your list's Intent i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY); // Adds the FLAG_ACTIVITY_NO_HISTORY flag startActivity(i); 

The FLAG_ACTIVITY_NO_HISTORY flag keeps the new Activity from being added to the history stack.

NB: As @Sam points out, you can use i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); instead. There is no functional difference.

like image 54
Cat Avatar answered Oct 21 '22 21:10

Cat


In the manifest file add:

android:noHistory="true"  

to the activity that you don't want to keep on the stack.

like image 31
Marcin S. Avatar answered Oct 21 '22 21:10

Marcin S.