I'm navigating from:
Main activity to Activity 2
Activity 2 to Activity 3
Activity 3 to Activity 4
through Intent.
I've also created the menu so that user can directly navigate from Activity 4 to Main activity. But after navigating from Activity 4 to Main activity by using menu, when I press back, it takes me to Activity 3 rather than exiting the application.
I tried:
#Override
public void onBackPressed() {
super.onBackPressed();
MainActivity.this.finish();
}
But no gain. Any suggestions?
Just try this code when you navigate from 4th Activity to Main Activity.
Intent inMain=new Intent(Activity4.this, MainActivity.class);
inMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(inMain);
Note:
This code in your case, clears the previous activities and launches the main activity with no activity in the backstack.
You can set flag Intent.FLAG_ACTIVITY_CLEAR_TOP to finish all the intermediate activities and your called activity will come to top of the activity stack.
use the flag Intent.FLAG_ACTIVITY_CLEAR_TOP with your intent. For more detail -
FLAG_ACTIVITY_CLEAR_TOP
call startActivity Method using clear top flag
startActivity(new Intent(this, UI.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
You need to consider your "back stack" more closely.
What is happening exactly is, your back stack gets populated as follows:
Main activity -> Activity 2 -> Activity 3 -> Activity 4
Then from Activity 4 you launch your Main Activity. So your stack becomes:
Main activity -> Activity 2 -> Activity 3 -> Main Activity
Hence, when you press back, you land up in Activity 3.
Solution:
Either call finish() on every Activity when you navigate away from them. Or use Intent.FLAG_ACTIVITY_CLEAR_TOP to clear all intermediate Activities.
More about Intent.FLAG_ACTIVITY_CLEAR_TOP.
When you go back to the MainActivity you need to use the Intent.FLAG_ACTIVITY_CLEAR_TOP on your Intent. This is an example of a goHome method that you can used in your Activity:
public void goHome ()
{
Intent homeIntent = new Intent();
homeIntent.setClass(this, MainActivity.class);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
In activity 2 and 3, after calling startActivity(intent), call this.finish(). This will also be one solution.
Finish previous activity when you are go to the next activity means write finish(); after startactivity(intent); and write below code for start first activity from fourth activity's button click event.
Intent in1=new Intent(Act4.this, Act1.class);
startActivity(in1);
finish();
Related
In android, Let's assume I have the below activities
Activity A -> Activity B -> Activity C -> Activity D
In some scenarios, I have to directly navigate from Activity D to Activity A (Example: Logout scenario). In such case, I am using the below method to clear the backstack and navigate to Activity A. It is nothing but like killing & opening the app again.
// Go to LoginActivity.java
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlag(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
It is absolutely working fine! No doubts on it.
What I need now?
When I navigate from Activity D to A (using FLAG_ACTIVITY_NEW_TASK.) I would expect OnDestroy() method of Activity D, C, B to execute. But it is not currently hitting/triggering.
Why I am expecting OnDestroy method to call?
I would need to invoke one trackevent(de-register) method from all the activities when clearing the back stack. Eg: From Activity D, C and B. Hence, I expect the OnDestroy method to invoke (or any method is also fine). Please assist me on this?
According to docs
FLAG_ACTIVITY_CLEAR_TOP: If an instance of the Activity to be launched already exists in the back stack, destroy any other Activity on top of it and route the Intent to that existing instance. When used in conjunction with FLAG_ACTIVITY_NEW_TASK, this flag locates any existing instances of the Activity in any task and brings it to the foreground.
You can achieve with by replacing Intent.FLAG_ACTIVITY_CLEAR_TASK with Intent.FLAG_ACTIVITY_CLEAR_TOP.
Modified code:
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
So, all the activities in back stack onDestroy method will be called including your LoginActivity's onDestroy method in the same order they inserted.
finishAffinity();
startActivity(new Intent(this,ActivityA.class));
For navigating from last(ActivityD) to first activity(ActivityA) after clearing backstack, try above code.
Clear all activities from backstack using.
After that call the activity.
I have3 Activities.
The first one A contains a Fragment A1 which contains a Spinner.
When I click the Spinner i ll go to Activity B, which contains a Button.
When I click the Button will go to Activity C.
I ovverrode the Activity C's OnBackPressed
(
Super OnBackPressed()
Intent i = new intent (this,ActivityB.class)
Finish();
StartActivity i
}
I overrode the Activity B's OnBackPressed
(
Super OnBackPressed()
Intent i = new intent (this,ActivityA.class)
Finish();
StartActivity i
}
I overrode the Activity A's OnBackPressed
(
Super OnBackPressed()
Finish();
}
My problem is that each time I click on Android's return Button when I am in Activity A, the application doesn't exit and goes to my old Spinner choice.
I have to click several times to really quit.
What should I do to leave the application by simply clicking on the Android return Button when I am in the Fragment A1?
You can use getActivity().finish(); from Fragment
You can try to finish the current activity before moving to other one.
For ex: When in Activity A, you click on spinner, inside onclick you start activity B and then call finish(); This will start Activity B and finish Activity A.
This way you will end up with one activity throughout. Once u back press, you will directly come out of the application.
In Activity A, you can do this to make sure you clear your backstack, so your app doesn't go the old spinner choice.
In your onCLickListener
Context context = view.getContext();
Intent intent = new Intent(context, ActivityB.class);
startActivity(intent);
finish(); // call this to finish the current activity
or you can also, do that in the manifest
<activity android:name=".ActivityA" android:noHistory="true" ... />
I have a 1st and a 3rd activity. I wish to push a button on the 3rd activity and it finishes the 3rd and 2nd activity switching all the way back to the 1st activity(it's the MainActivity might I add). In other words, is there a way to finish automaticly the call stack of activities to a specific activity?
You can use method startActivityForResult() to start your activity.
You need to call method setResult() when you finish the activity.
And override method onActivityResult() to do what you want to do.
Actually, seems 2 methods can solve this.
Assuming Activities: A -> B -> C, and all alive in the same stack.
1) Use android:launchMode="singleTask" for Activity A. When C calls A, A starts and both B & C finish.
2) When starting A, adding flag Intent.FLAG_ACTIVITY_CLEAR_TOP.
if you don't have to pass any data to the main activity, i guess this is what you are looking for: how back to the main activity in android?
execute this code at the button click in your 3rd activity:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this code clears your activity stack and reopens your main activity.
I have 3 activity, I want to return to the first activity and Refresh it after clicking on the button at second and Third activity, without using "new Intent(...)"!
I use this code:
ActivityFirst actFirst = (ActivityFirst) _context;
actFirst.recreate();
but it doesn't work! with Error : java.lang.ClassCastException
Is there any way to refresh first activity from second or third activity without use below code?
Intent intent = new Intent(this, ActivityFirst.class);
startActivity(intent);
You cannot refresh your first Activity because the thread is paused when the Activity is not in the foreground. Also the reason you are getting a ClassCastException I'm assuming is because your trying to cast your second or third Activity into the first Activity by passing your current Context.
You should just refresh your first Activity inside onResume like so:
#Override
protected void onResume() {
super.onResume();
recreate();
}
To start your first Activity again you could call this.
Intent intent = new Intent(this, FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
If you want to finish second or third activity when you click on button on these activity then just use onActivityResult in First Activity and refresh you first activity.
If you only want to refresh without finish second ot third activity then send broadcast message to first activity.
Let me know this is useful for you?
Go through Activity Life Cycle, When you have opened the 2nd activity the first activity will be in pause state.so while resuming the activity you can refresh the activity.Try these method to implement it : onResume()onPause()
startActivity(new Intent(B.this,A.class));
finish();
If you pass Intent from second activity(B.this) to first activity(A.class) ; it will be recreate that first activity. When you press back than not second activity open because of finish();.
I have two activities and one fragment in my app. The first activity, MainActivity, is used to hold the fragment. It contains an ActionBarSherlock ActionBar and the fragment, WFrag. WFrag opens up the second activity, SettingsActivity, when you press a button. In SettingsActivity, you can choose settings that affect the behavior of WFrag. Naturally, I want the onBackPressed() method to go back to WFrag and update it with the user's new settings. My question is: how do you make it so that when you go back (via the back button) from SettingsActivity, you recreate (call onCreate() again) WFrag?
override onBackPressed
or
add the update in onStart/onResume
First you have to override onBackPressed() of SettingActivity.
Second when you start second activity i.e SettingActivity from MainActivity you have to clear Stack of previous activity in this way
Intent intent = new Intent(this, SettingActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Same goes for SettingActivity in onBackPressed() method you have to run your MainActivity in this way
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Or if you are redirecting to MainActivity from button click you can use above code for same.