How to kill stack activity - android

Please tell me any one, how to kill previous stacked activity.My requirement is like that
let I move from activity
A---->B----->C------>D----->E
Then activity stack will be E-D-C-B-A
Now I am at E if my all work is done then I move to activity B
using
startActivity(intent);
this.finish();
this code will be kill only E activity but I want to kill C D activity also.
how can I do that.
thanks.

One solution is to call Activity [B] with flag FLAG_ACTIVITY_CLEAR_TOP
Intent b = new Intent(this,B.class);
b.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(b);
this.finish();
This will kill C D E

Intent intent = new Intent(this, B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

you have to pass activity context into intet so that you can call finish on the activity.
also goo idea would be to start the activities by using startActivityForResult(Intent, int).
Where int is code for the activity you are starting.
later you can call finishActivity(int requestCode) which will kill the activity for the code you provided. You can have codes (id's) for activities in your strings.XML.
http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)

Related

How to handle onActivityResult?

I am blocking in onActivity result.
Activity A startActivityForResult() to Activity B, Some business reason i am removing activity B and moving to till Activity F. From Activity F have to send setResult() to Activity A same time i have to clear stacks while moving to Activity A.
How to handle this scenario?
We can use a flag from Intent class (didn't try myself) -Intent.FLAG_ACTIVITY_FORWARD_RESULT
Right before calling finish() in each of your middle actvities, you must be making a call to startActivity(intent). Pass this flag to this intent:
showC.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
In your last activity, do the regular setResult() ceremony. And then try to get result in your first activity as usual.
using intent start ActivityF and pass result to ActivityA using intent.putExtra
Intent intent = new Intent(ActivityF.this, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("RESULT", "result to ActivityA");
startActivity(intent);

How to close one activity to another

In my android app i have two activities, such as Activity a and Activity b. I want to close Activity a as well as Activity b from Activity a, I tried the code below, but an exception occurs,
a.this.Finish();//To finish current activity works fine
b.this.Finish();//Exception occurs because i tried to close from a Activity class.
So how to finish Activity b from Activity a? Guide me,
You need to start activity b for result and close it when activity a is closing:
Start of activity b:
Intent it = new Intent(a.this, b.class);
startActivityForResult(it, REQUEST_CODE); // REQUEST_CODE is int value
Finish of activity b:
finishActivity(REQUEST_CODE);
Activity A to B
Intent b = new Intent(A.this,B.class);
startActivity(b);
finish(); // Activity A will close it before starting B Activity.
Activity B to A
Intent a = new Intent(B.this,A.class);
startActivity(a);
finish(); // Activity B will close it before starting A Activity.
To finish() an Activity, you need to get this instance. But I think that if you want to control many Activity, I suggest you to use fragment. I meet your problem before and try to fix them but I meet more issue with that. So let try to use Fragment.

Why does FLAG_ACTIVITY_CLEAR_TOP not work?

As the title says, Why intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) or intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) won't work?
I have 3 Activities let us say A, B and C.
When I am trying to launch Activity A from C with code:
Intent i = new Intent(this, A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
It simply starts Activity A but doesn't clear the top.! -_-
I have also tried using setFlags().
I read different questions on SO about this problem, but I couldn't find the right answer. >_<
Somebody please help!
Edit
Code for onBackPressed() in activity 'A' as requested by #codeMagic.
#Override
public void onBackPressed(){
if(wvLogin.canGoBack())
wvLogin.goBack();
else
super.onBackPressed();
}
From the documentation for FLAG_ACTIVITY_CLEAR_TOP:
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.
As you added in your comment, the activity A has been finished before calling B, so this situation doesn't apply. A new instance of activity A will be launched instead.
As I see it, you have two options here:
1) Use the Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK flags. This will start activity A as the root of the stack. It works, but any other activities in the stack will be lost. Assuming A was the first activity (or at least, that you are not interested in any previous activities in the task stack) then it doesn't matter. Note: CLEAR_TASK requires API Level 11.
2) Another possible solution (in case the previous assumption is not true) would be to not use intent flags at all:
B starts C with startActivityForResult().
Instead of calling A, C finishes, having set a result for B indicating that A must be launched.
In B.afterActivityResult(), finish B and launch A.
You are missing the Intent.FLAG_ACTIVITY_SINGLE_TOP flag
Try this:
Intent i = new Intent(this, A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
You used a diferrent intent: use the one you initialized:
Intent i = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); \\WRONG;;
startActivity(i);
solution:
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); \\RIGHT;;
You could either put a noHistory true to the Activity A in the manifest
android:noHistory=true

Start Activity and don't destroy other activity

I am new to Android and started the activities A - B - C - D. From activity D, when I open activity A again then how can I start so that activities B and C don't finish and A starts again? There should only be one activity A.
Thanks in advance.
Use the Intent Flag FLAG_ACTIVITY_REORDER_TO_FRONT
In D
Intent i = new Intent(ActivityD.this, ActivityA.class);
i.setFlags(FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
This will simply bring ActivityA to the front of the stack and leave B and C where they are which I believe is what you want. Then you can obviously call finish() on D if you want to remove it from the stack.
You can find all available flags in the Intent Docs

Android:Delete a activity in backstack

I have got 4 activity let it be A->B->C->D.In every A,B,C activity user need to enter data all data will sent to server in C activity if the user data is correct he will move on to D activity and all the activity A,B,C removed from stack.If the data is in correct i need give the user to reenter data i.e is on back press it has to move C->B->A.My question is How to remove A,B,C activity when user enter D activity.
Use FLAG_ACTIVITY_CLEAR_TOP this shall solve your problem
From the Android documentation:
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.
For example, consider a task consisting of the activities: A, B, C, D.
If D calls startActivity() with an Intent that resolves to the
component of activity B, then C and D will be finished and B receive
the given Intent, resulting in the stack now being: A, B.
Use it like
Intent intent = new Intent(getApplicationContext(),
yourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
and also, take a look at this question:
Activity with Flag FLAG_ACTIVITY_CLEAR_TOP (android)
Edit : I thought you want to move to your home activity from D and want to remove all activities from stack
Your stack would be like homeactivity , A , B , C , D
so i gave you this solution as this shall remove all activities in stack on top of your home activity.
If you want to clear the the stack while going to D, for that you can use FLAG_ACTIVITY_TASK_ON_HOME or FLAG_ACTIVITY_CLEAR_TASK
But both of these for api level 11.
The correct answer interesting for me also, but I can offer a solution:
For example you start activity A from O: O->A->B->C->D.
On activity O you can put in android manifest android:launchMode="singleTop"
Then, when data are ok, you can start activity O with flag "FLAG_ACTIVITY_CLEAR_TOP" - it remove from stack A,B,C and will be called method onNewIntent (Intent intent) http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent) in O, where you can start activity D.
You can start activities with startActivityForResult, and call setResult from for example activity D, in C activity you can listen activity result, and related of this result finish activity or not, or call setResalt from C activity ...
When you want to move D activity there you need to check your
conditions and if your condition is satisfied then you need to enter
into your Next activity(i.e., D) .In that case you need to use the
following code..
Intent intent = new Intent(this,D.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Suppose In on backpress you need to use finish(). to move back i.e., C
-> B -> A.
Try this piece of code with some modifications:
Intent intent = new Intent(this, D.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // To clean up all activities
startActivity(intent);
Try looking for Activity's public method startActivity(Intent i) and finish() here
In usage wise, it should look like this.
Intent i = new Intent(MainActivity.this, MainActivity.class);
startActivity(i);
finish();
Hope this helps :D

Categories

Resources