How to Kill Sub Activities and Bring Activity to Top of Stack

How to kill sub activities and bring activity to top of stack

You can use the FLAG_ACTIVITY_CLEAR_TOP flag on the intent to restart activity A.

How to bring an activity to foreground (top of stack)?

You can try this FLAG_ACTIVITY_REORDER_TO_FRONT (the document describes exactly what you want to)

Android Popping off the Activity Stack

If A is already running, you can use the FLAG_ACTIVITY_CLEAR_TOP flag when starting an intent to go back to A.

See also, similar questions:

How to clear current activities in the stack?

how to kill sub activities and bring activity to top of stack

Android popping activities off the stack - yes, again

You could just add singleTop to your main activity.

Here, read about it. It brings the existing instance of the activity rather than create a new one.

http://developer.android.com/guide/topics/manifest/activity-element.html

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_SINGLE_TOP

Android: Remove all the previous activities from the back stack

The solution proposed here worked for me:

Java

Intent i = new Intent(OldActivity.this, NewActivity.class);
// set the new task and clear flags
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);

Kotlin

val i = Intent(this, NewActivity::class.java)
// set the new task and clear flags
i.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(i)

However, it requires API level >= 11.

How to kill an activity

Call new screen using

Intent intent = new Intent(activity,secondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
activity.startActivity(intent);

When you hit home button , application get resume. so next time it open the same page. for this use

@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
startActivity(new Intent(Activity1.this,spalshscreen.class));
}

on restarting , you can also reset flag using Intent.FLAG_ACTIVITY_CLEAR_TOP if activity is in stack.

Finish an activity from another activity

  1. Make your activity A in manifest file: launchMode = "singleInstance"

  2. When the user clicks new, do FirstActivity.fa.finish(); and call the new Intent.

  3. When the user clicks modify, call the new Intent or simply finish activity B.

FIRST WAY

In your first activity, declare one Activity object like this,

public static Activity fa;
onCreate()
{
fa = this;
}

now use that object in another Activity to finish first-activity like this,

onCreate()
{
FirstActivity.fa.finish();
}

SECOND WAY

While calling your activity FirstActivity which you want to finish as soon as you move on,
You can add flag while calling FirstActivity

intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

But using this flag the activity will get finished evenif you want it not to. and sometime onBack if you want to show the FirstActivity you will have to call it using intent.



Related Topics



Leave a reply



Submit