Android activity stack question [duplicate] - android

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Removing an activity from the history stack
I've 1 question for the Android stack and the push stack process as stated below:
A->B->C->C->C->C->D
C will be repeatedly call itself until certain condition met.
i use the code below to start activity.
Intent intent = new Intent(this, MyActivity.class);
startActivity(intent);
My problem is after activity D started i want straight away go back to activity B instead of go back activity C when press the back button.
A->B->C->C->C->C->D -----> A->B
I know i should use intent setFlags but i've no idea what to use
http://blog.akquinet.de/2010/04/15/android-activites-and-tasks-series-intent-flags/
Any suggestion?
Thanks in advance.
Edited:
For this case Activity C is question page and the Activity D is the result page.
A->B->Question 1->Question 2-> Question 3 -> Result.
I need to keep the history stack b4 activity D, this will allowed user to change the answer.

for activity: c:
try intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Try Intent.FLAG_ACTIVITY_CLEAR_TOP each time you call startActivity() with the intent for B.
This will bring B to the top, popping all activities that were on top of it.

#Override
public void onBackPressed() {
Intent i = new Intent(ACTIVITY_D, ACTIVITY_B);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
return;
}
Override the onBackPressed() and set the FLAG_ACTIVITY_CLEAR_TOP flag. Place the function above under your activity D.

Related

How to go to previous screen from an activity in Android? [duplicate]

This question already has answers here:
How to close activity and go back to previous activity in android
(19 answers)
Closed 8 years ago.
I have a class TestFragment which extends Fragment,from this Fragment I have started Activity1
and from Activity1 i have started Activity2,Now i am in Activity2 want to go to TestFragment
when a button inside Activity2 is clicked and i want to update the UI in TestFragment
Call finish() if you want to go back to the caller activity. If you want to launch the activity which contains the fragment create an intent and launch the activity, something like startActivity(new Intent(this,youractivity.class));
Please write some code..Anyway if you want start another activity take a look here: http://developer.android.com/training/basics/firstapp/starting-activity.html onClick event you just put the intent for example Intent intent = new Intent(this, TestFragment.class);
you can clear the stack activity like this:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
by this code you will clear the stack and MainActivity will be opend.
Hope this help you

How to remove all activity in Stack when press back button

I have a list of activities A - B -C -D - E and more, for example final activity is K. I want clear all these activities in stack when i press BACK button. How can i do ? In fact, i over ride
onBackPress(){
moveTaskToBack(true);
finish();
}
but only current activity is deleted and application exit. Then, i come back application, it resume activity before K. I want it start from begining when i re-open app. I think the reason here is because the list of activities in stack still are stored, so i want to clear all stack when clicking BACK button. Any suggestions ? thank you very much !
There is method called finishAffinity() for finishing all activity.
public void onBackPressed(){
super.onBackPressed();
this.finishAffinity();}
You need to call your activity with the FLAG_ACTIVITY_CLEAR_TOP inside your onBackPressed
#Override
public void onBackPressed()
{
Intent it = new Intent(YouCurrentActivity.this, YourFinalActivity.class);
it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(it);
finish();
}
Hope it Helps!
Either use the noHistory flag in the manifest or finish each activity yourself when the user navigates away.
startActivity(myIntent);
finish();
Another solution, maybe the best, if you have so many overlaying Activities: use only one Activity and handle the content in Fragments. This way you are in control what exactly you want to show when the user hits the back button.
In API level 11 or greater, use FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK flag on Intent to clear all the activity stack.
Add this code on your onBackPressed() method,
> This launch mode can also be used to
good effect in conjunction with
FLAG_ACTIVITY_NEW_TASK: if used to
start the root activity of a task, it
will bring any currently running
instance of that task to the
foreground, and then clear it to its
root state. This is especially useful,
for example, when launching an
activity from the notification
manager.
So your code to launch B would be:
Intent intent = new Intent(A.this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish(); // call this to finish the current activity

Activity history Stack is not getting clear

I have 4 Activity classes e.g A, B, C and D here i am going to B from A and then C and in the menu of this(C) activity i have given a link for Activity A so when i am selecting that menu i am going to A and again from A i am going to B from here i am going to D and come back to B and then going to C.
So overall scenario is A->C->A->B->D->B->C
now the problem is while pressing back button it goes like this
C->B->D->B->A->C->A exit
but i want to use like this if users in activity A its directly close the app no need to go back to B and then C but if its in B then no need to go back in C Activity.So for the C and D i am using android:noHistory="true" and its working fine but i can't use this thing for B. because while coming back from D to B , i can't skip B Activity and directly jump to A.
So now stack is like this:
C->B->A->A exit
Following that i have tried is:
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Thanks for considering.
Where you use Intent it stores the previous activity in the stack so use finish method to clear the stack like example
Intent intent = new Intent(A.this,b.class);
startActivity();
finish();
this code will help you
Intent intent = new Intent(this, FirstActivity.Class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

How to Clear all previous activities~?

Activity Flow in my app.
WelcomeActivity -> SignInActivity -> SignUpActivity ->
TabsActivity(this is main) -> ...
I want to close all previous activities (Welcome, SignIn, SignUp) when start TabsActivity.
I try several method...
TabsActivity. clear task on launch=true ? but not work (maybe)
TabsActivity. launch mode = singleTask ? but not work
But I do not want to "save 3 activities and call each activity.finish()"
Depending on the situation, "available 2 or 4 activities not 3", or
"I do not know What activities is in activity stack".
I want to clear all previous activities, regadless of any situation.
Help me :)
Sorry my poor english... Thanks.
If I understand correctly, you might want to try starting your TabsActivity with the following code:
Intent intent = new Intent(getApplicationContext(), TabsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
The flag Intent.FLAG_ACTIVITY_CLEAR_TOP clears the history.
Use
Intent intent = new Intent(getApplicationContext(), ClassToLaunch.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will finish the previous activities
To Close Previous Activities ,you should start New Activity with startActivityForResult and then before finishing the current Activity with finish() call , setResult(value) for previous Activity ,the previous Activity will then get a callback where you can call finish() for the previous Activity.
Clear Backstack of android, from where you are calling tabActivity
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

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