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
Related
When my activity stack is like,
MainActivtiy -> Activity1 -> Activity2 -> Activity3
I need to go 3 back states to reach MainActivtiy from Activity3
I could be able to close opened activities from Activity3 like,
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
But, above code segment closes all activities including MainActivtiy and starts new MainActivtiy. I want to know are there any other options to do the task that I need. I dont want to create new Intent because it drops the data of static methods that I have created in my MainActivtiy.
Please help me, Thanks in advance.
You should use FLAG_ACTIVITY_CLEAR_TOP which will bring the running activity on top and remove all other activities above it
Note : it will trigger the onNewIntent of already running activity otherwise mention launchMode = "singleTask" in activity tag, inside manifest
In my app I have two fragment in ManiActivity.class .
In my fragment I have a listview , when I click a item in list I open MainActivity2.class . In MainActivity2.class I have listview related and when i click list related I open MainActivity2.class with new value. Now I want create a button back home in MainActivity2.class and when I open many MainAcitivty2.class when click list related on it I can click button back home to go to MainActivity2.class
I try it with
Intent intent = new Intent(MainActivity2.this,MainActivity.class)
startIntent(intent)
But when I click back button in MainActivity.class it comback MainActivity2.class , it not exit app.
How I do to clear it?
Please help me.
This is why Android maintains states of each activity in a Stack, that's why when you will press that Back-button, you just have to clear all previous activities from the stack and then open MainActivity
Just you have to set flags before starting intent:
Intent intent = new Intent(MainActivity2.this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
Use android:launchMode="singleTask" for your MainActivity in AndroidManifest.xml.
Which will keep only one instance of MainActivity if launched again this activity will come to top of the stack.
And Use this code
Intent intent = new Intent( MainActvity2.this, MyActivity.class );
intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP );
current_activity.startActivity( intent );
call finish method after startIntent
something like this:
Intent intent = new Intent(MainActivity2.this,MainActivity.class);
startIntent(intent);
finish();
and put finish method in your activity
#Override
public void finish() {
super.finish();
}
its very simple.. use finish();
Intent intent = new Intent(MainActivity2.this,MainActivity.class);
startIntent(intent);
finish();
bcz u create multi activity in mainActivity2. so u have to destroy your activity before u go to mainActivty.
otherwise when u open new many in mainactivity2. that time use finish.. bcz u must have to destroy first your mainActivity2.
if you not understand my answer then just post your whole code... i will edit...
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);
I have a little question which is bothering me. How can I finish activity C and start it's parent. But the tricky part is that I can start activity C from 20 other activites. The idea is to open the right one when i call finish on C. And the other thing is that I have tabhost , which childs opens activity C.
Any suggestions how to achieve this?
in your activity C save the following variable:
Class parent = ParentActivityClass.class;
override:
public void onBackPressed(){
//create an intent like
Intent i = new Intent(this, parent);
startActivity(i);
//add extras to intent if needed
this.finish();
}
please note that this might create a NEW parent activity. it is up to you handle this situation if this might create problems.
An alternate solution is to finish each other child activity when you launch a new activity. This will assure that on your stack you will have always the parent below the child activity.
I simply did something like this :
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
This did the trick.
Depend on your activity stack if your current exactly on top of the parent activity you can just finish current actvity and it will go to previous activity. If you want to clear all activity stack and start over new activity try
Intent intent1 = new Intent(context, activity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
which clear your stack and start over new if you have stack of activity over parent want to finish all use this and start over parent again.
Take a look at Task and Backstack, Implementing Temporal and Ancestral Navigation
There are more specific explanations. Hope this help
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.