How to clear specific activity from the stack history?
I'm not sure you can directly programmatically remove activities from the history, but if you use startActivityForResult()
instead of startActivity()
, then depending on the return value from your activity, you can then immediately finish()
the predecessor activity to simulate the behaviour you want. By using this method in all your activities, you can have this behaviour cascading the activity stack to allow you to go from activity D to activity A.
I know this isn't your situation, but in future if you know before you start the activity that you don't want the predecessor to remain, you can call finish()
immediately after startActivity()
.
Please see the section called "Lifetime of the New Screen" in Common Tasks and How to do Them in Android
How to remove particular activities from stack?
Launch the activity B from D with FLAG_ACTIVITY_CLEAR_TOP
flag..
Intent a = new Intent(this, B.class);
a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(a);
How to remove a particular activity from android back stack?
Try this in one.
// 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) {
Config.screenStack.remove(key);
_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);
}
If you want to finish all activity when you exist from app you can see this code.
Clear the entire history stack and start a new activity on Android
In API level 11 a new Intent Flag was added just for this: Intent.FLAG_ACTIVITY_CLEAR_TASK
Just to clarify, use this:
Java
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Kotlin
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
Unfortunately for API lvl <= 10, I haven't yet found a clean solution to this.
The "DontHackAndroidLikeThis" solution is indeed pure hackery. You should not do that. :)
Edit:
As per @Ben Pearson's comment, for API <=10 now one can use IntentCompat class for the same. One can use IntentCompat.FLAG_ACTIVITY_CLEAR_TASK
flag to clear task. So you can support pre API level 11 as well.
How to remove specific activity from the stack
Write an abstract
BaseActivity
class and implement a LocalBroadcastManager
in it and register a BroadcastReceiver
. Note that we have also introduced an abstract onFinishReceived
method.
public abstract class BaseActivity extends AppCompatActivity {
private LocalBroadcastManager mLBM;
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
onFinishReceived(intent.getStringExtra("activity"));
}
}
protected void onCreate(@Nullable Bundle savedInstanceState) {
...
mLBM = LocalBroadcastManager.getInstance(this);
IntentFilter filter = new IntentFilter();
filter.addAction(...);
mLBM.registerReceiver(receiver, filter);
}
protected void onDestroy() {
mLBM.unregisterReceiver(receiver);
mLBM = null;
}
abstract void onFinishReceived(String activity);
}
Then, extend all A
, B
and C
Activities from the BaseActivity
and override the onFinishReceived()
method.
public class A extends BaseActivity {
void onFinishReceived(String activity) {
if (activity.equals("A") { // or "B" or "C"
finish();
}
}
}
}
Now, whenever you want to finish a specific activity use,
LocalBroadcastManager.getInstance(context)
.sendBroadcast(new Intent()
.setAction(...)
.putExtra("activity", "A") // or "B" or "C"
);
LocalBroadcastManager
is now deprecated. It's better if you can use something like RxJava
. The idea is to use an asynchronous and event-based mechanism rather than going for a simple ugly solution like storing the Activity in a static variable.
How to remove activity from back stack in android?
In Activity A you can finish all activities in onResume for that you need to store all activity's this in staic variable
Activity B
public static B b;
oncreate(){
//...
b=this;
}
Do the same for C and D and in A
onResume(){
clearAllTasks();
}
public void clearAllTasks(){
if(B.b!=null){
B.b.finish();
}
if(C.c!=null){
C.c.finish();
}
if(D.d!=null){
D.d.finish();
}
}
in manifest
<application>
<activity android:name="A"
android:noHistory="true">
</activity>
//...
</application>
How to remove Specific activity/activities from activity stack based on some action?
You wrote:
What i want is that if the user selects Option Two on the
ReadingConfirmationActivity
then the user will be taken to the
NewReadingActivity
that means i want to remove
ReadingConfirmationActivity
(which is easy just call the finish()) and
the activity started before that activity i,eReadingInputActivity
.
To do this you will do the following when the user selects Option Two in ReadingConfirmationActivity
:
Intent intent = new Intent(this, NewReadingActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will cause all of the activities on top (and including) NewReadingActivity
to be finished. A new instance of NewReadingActivity
will be created and shown to the user. If you wish to return to the existing instance of NewReadingActivity
, you can also do that by also adding Intent.FLAG_ACTIVITY_SINGLE_TOP
to the Intent
.
Related Topics
How to Raise a Toast in Asynctask, I am Prompted to Used the Looper
How to Restrict Content Provider Data Across Applications
Recycle-View Inflating Different Row :- Getting Exception While Binding the Data
Multiple Table SQLite Db Adapter(S) in Android
What Are the Ipc Mechanisms Available in the Android Os
Scale Fit Mobile Web Content Using Viewport Meta Tag
How to Display Both Icon and Title of Action Inside Actionbar
Why a Viewmodel Factory Is Needed in Android
Single Observable with Multiple Subscribers
How to Prevent Multiple Toast Overlaps
Wrong Fragment in Viewpager Receives Oncontextitemselected Call
Speed Up 'Navigation Drawer' Animation Speed on Closing
Scrolling Edittext Inside Scrollview
Which View Should Be Used for New Material Design Bottom Navigation
Android Httpclient:Networkonmainthreadexception
Configuration on Demand Is Not Supported by the Current Version of the Android Gradle Plugin