Android Kill Previous Activities Without a New Intent

How to destroy the previous activity in android?

Simply finish the activity A when you are starting activity B.

Something like:

startActivty(intent);
this.finish();

this refers to current Activity (Activity A) and intent has the intent to open Activity B.

Edit: For removing the Activity A in certain condition only:

startActivityForResult(intent); // Starting Activity B.

Then in Activity B:

onBackPressed() {
setResult(...); // Set result as RESULT_OK etc based on condition. You can also send some data.
}

Then again in Activity A:

onActivityResult(...) {
if ( ... ) // check the condition value from the result
finish();
}

How to clear previous Activity on Android without start new Activity

Step1:Create a drawable:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/darker_gray" />
<item>
<bitmap
android:gravity="center"
android:src="@drawable/img_splash" />
</item>

Code link: splash_theme_bg.xml

Step2: Create Style

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/splash_theme_bg</item>
</style>

Code link: styles.xml

Step3: Set style in AndroidManifest.xml

<activity
android:name=".SplashActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Code link: AndroidManifest.xml

Let me know if you still get stuck anywhere.
Happy Coding!

Android - resuming activities without killing previous activity

You can use FLAG_ACTIVITY_REORDER_TO_FRONT, and if you already have an instance of the activity it will bring it to front and call its onResume().

The if is because Android can kill your background activity any time if the system lack of resources.

Intent i=new Intent(this,Activity2.class)
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);

Also check FLAG_ACTIVITY_PREVIOUS_IS_TOP, depends on your app logic.

Go back to a specific activity without starting it again

Using just a clear top flag causes the target activity to get destroyed first and then recreated. To avoid this, use two flags:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

this will cause the activity to not get finished thus your API call wont happen again. The intent you use here to start the activity A will be delivered to onNewIntent() method in activity A in this case if you need it.

This is all assuming that you have not specified any launch mode in your activity manifest declaration of are using "multiple" launch mode.

As per the docs:

If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().

How to destroy the previous activity but splash and home activity is not included when moving to the next activity?

When you move from Checkout Activity to Loading Activity you can simply call finish() to destroy the checkout activity.

In the onBackPressed of Loading Activity you can do like:

Intent intent = new Intent (Loading.this , Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.ACITIVTIY_SINGLE_TOP);
startActivity(intent);
finish();

This would redirect you to the Home Activity (previous instance) and will clear the activities which are on top of the stack. Therefore, what you want is achieved!

For more info: Understand Tasks and Back Stack

Update:

If you want the same thing to happen in the Order Details Activity, you just put the code in onBackPressed of order details:

Intent intent = new Intent (OrderDetails.this , Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.ACITIVTIY_SINGLE_TOP);
startActivity(intent);
finish();

Destroy All Previous Activities After Specific Activity is Open

Intent intent = new Intent(ActivityC.this, ActivityD.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

FLAG_ACTIVITY_NEW_TASK

If set, this activity will become the start of a new task on this
history stack. A task (from the activity that started it to the next
task activity) defines an atomic group of activities that the user can
move to. Tasks can be moved to the foreground and background; all of
the activities inside of a particular task always remain in the same
order. See Tasks and Back Stack for more information about tasks.

This flag is generally used by activities that want to present a
"launcher" style behavior: they give the user a list of separate
things that can be done, which otherwise run completely independently
of the activity launching them.

When using this flag, if a task is already running for the activity
you are now starting, then a new activity will not be started;
instead, the current task will simply be brought to the front of the
screen with the state it was last in. See FLAG_ACTIVITY_MULTIPLE_TASK
for a flag to disable this behavior.

This flag can not be used when the caller is requesting a result from
the activity being launched.

FLAG_ACTIVITY_CLEAR_TASK

If set in an Intent passed to Context.startActivity(), this flag will
cause any existing task that would be associated with the activity to
be cleared before the activity is started. That is, the activity
becomes the new root of an otherwise empty task, and any old
activities are finished. This can only be used in conjunction with
FLAG_ACTIVITY_NEW_TASK.

How to destroy previous activity in Activity?

finish Activity B when you are calling Activity C depends on your logic.
For example

if(true){
Intent in = new Intent(B.this,c.class);
startActivity(c);
}
else
{
Intent in = new Intent(B.this,c.class);
startActivity(c);
finish();
}

Finish all previous activities from other activity

try this solution..

Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);

hope it helps.

How to kill an Android activity when leaving it so that it cannot be accessed from the back button?

You just need to call finish()

Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
finish();


Related Topics



Leave a reply



Submit