The question might be dumb but I'll ask it, for example I have 3 Activities, Activity 1,2,3.
Activity 1, I click a button, goes to Activity 2
Activity 2, I click a button, goes to Activity 3.
Activity 3, I click the back button, I want it to go back to Activity 1. Any idea how to do it? Sorry Android Newbie. Thanks!
override onbackpressed method and just do like this:-
#Override
public void onBackPressed() {
Intent intent = new Intent(Activity3.this,
Activity1.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
Enjoy....!
In the android.manifest file make your second Activity..
android:excludeFromRecents="true"
android:launchMode="singleTask"
Hope this does the trick. if this does not work then you can try by setting Intent.setFlags while you start the activity 2
Use SharedPref to store the 2nd prevActivity. Take to String pref (prevActivity_1 and prevActivity_2) to track your 1st and 2nd level previous activity. and update the pref when you are leaving the current activity. Now from any activity you can access the 2nd previous activity and call it.
Here is a Easy and Efficient way.. You can use forwarding to remove the previous activity from the activity stack while launching the next one. basically all you're doing is calling finish() immediately after calling startActivity().
My answer for you example is to call finish()immediately after calling startActivity() in activity b. and try backbutton after went activity c. Happy Coding..
ok when u click on 2nd activity start activity then clear 2 activity from stack and also write finish after call intent in 2nd activity and also see how to clear stack top :)
Try this : Back button start activity and Back button start activity
You can startActivity from onBackPressed() method.
Hope this helps.
Since Activity2 is pushed in the stack the back button of Activity3 will get you to Activity2. Hence the best you can do is not to push Activity2 in the stack.
In your manifest.xml, where you define your activity do the following:
<activity android:name=".Activity2" android:noHistory="true" ... />
developer.android.com says:
A value of "true" means that the activity will not leave a historical trace.
It will not remain in the activity stack for the task,
so the user will not be able to return to it.
For more details:
https://developer.android.com/guide/topics/manifest/activity-element.html
Hope this solves the issue :-) I know this question has been asked long time back, but it might help some...
Related
I have an activity. After checking some stuff, I want to go back to previous activity if a previous exists, and if not I want to start a SpecificActivity. How do i do it?
Edited:
Seems like no one is understanding what I meant, so let me rephrase. Lets say I am in Activity A. I don't know if A is the only activity in the stack. If there are other activities, then I want to finish A and pop the activity right below A in the stack into the foreground. If A is the only activity in the stack, I want to start some activity Z. How do i do it?
You have to pass class name as intent extra from both Splash and DashboardActiviy.
In List Activity you have to get the class name using getIntent().
When the user click back button, you need to check the class name based on that you can take decision.
if(name.equalIgnorecase(DashboardActivit.class.getSimpleName()){
//Add your intent
}else{
//
}
This may give you definite solution to you.Give a try
you can simply override the onBackpress()
#Override
public void onBackPressed() {
super.onBackPressed();
startActivity(new Intent(this, Destination_Activity.class));
finish();
}
call the next activity like.
Intent intent = new Intent(this,Your_Next_Activity.class);
startActivity(intent);
then it will call your another activity and your current activity will be on background if you will use finish() after calling next activity it will finish your current activity so don't use finish() after calling your next activity in this scenario.
After that when you press back button it will automatically finish current activity.
I have a problem cause when I go to different activities using startActivity function,
they always get created from scratch. Even if activity A was visited before, when I go to activity B and then A again, activity A is created again.
The problem is with back button, cause if I go to Activity A then B then A and then B,
in order to close the application I have to press back button 4 times.
I guess that it shouldn't act like it and user should be able to go to activity A when first pressed back button and the second press should close the application.
How to solve this issue?
Greetings
If you have activity transitions like:
Activity A -> Activity B
Activity B -> Activity A
and you want the user to go back to the same instance of Activity A in this case, maybe you just need to call finish() in Activity B after you call startActivity() for Activity A?
If this isn't helpful, please give us more information about what you are trying to do.
make sure you implement onSaveInstanceState and be prepared to restore your activity from a Bundle in onCreate. that's how you re-establish where you were when you return to an activity.
add launcheMode="singleTask" to your activity in the manifest
You need to set FLAG_ACTIVITY_SINGLE_TOP to your intent for launching activity A. Doing so will cause your previously created activity to re-use. Make sure you do handle your afterwards intents in onNewIntent method. For more info.
You need to set the flag FLAG_ACTIVITY_REORDER_TO_FRONT when you start activity A from B or vice versa, like
i = new Intent("....ActivityAorB");
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
I've tried solutions proposed so far, however they didn't do it for me.
What did however, is using flag FLAG_ACTIVITY_CLEAR_TOP while starting activities.
Thanks for pointing me in the right direction though.
I don't want the user to be able to go back to the splashscreen of my app. One solution seems to be to check if the activity below the current one is an instance of the splashscreen, and in that case exit the app, as shown in the code below. However, I don't know how to check what's the previous activity in the stack. Anybody can help? Is there any other way to disable 'go back' to a given activity?
#Override
public void onBackPressed() {
if(<previous activity in stack is an instance of splashscreen>){
Intent exit_intent=new Intent(CurrentActivity.this, SplashScreen.class);
exit_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
exit_intent.putExtra("EXIT", true);
context.startActivity(exit_intent);
}
}
Call finish() in your Splash Screen activity right after starting the next activity.
Another approach is to add this attribute to your activity in AndroidManifest.xml: android:noHistory="true"
Example:
<activity android:name=".SplashActivity" android:noHistory="true"/>
This attribute instructs Android to remove SplashActivity from the history stack once its navigated away from.
Just call context.finish() after context.startActivity()
try the following when calling the next Activity from your Splashscreen:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
<activity android:name=".SplashActivity" android:noHistory="true"/>
From the documentation:
Whether or not the activity should be removed from the activity stack
and finished (its finish() method called) when the user navigates away
from it and it's no longer visible on screen — "true" if it should be
finished, and "false" if not. The default value is "false".
A value of "true" means that the activity will not leave a historical
trace. It will not remain in the activity stack for the task, so the
user will not be able to return to it. In this case,
onActivityResult() is never called if you start another activity for a
result from this activity.
This attribute was introduced in API Level 3.
I am developing an application and in that case I've some activities which I don't want to show them again e.g like i have a splash screen which is my activity and i don't want it to show again on back key press.
Thanks in Advance
Set android:noHistory="true" to the activity entry at the AndroidManifest.xml. That will prevent the activity from being saved on the stack.
Call finish() on the activity after you call startActivity() to start the next one.
There is a manifest parameter that lets you permanently disable activity stack history for particular activities.
See: Removing an activity from the history stack
Closing a Activity on new Activity
I have 2 activities. Activity A and Activity B. Both calling each other though intent. Activity A calls Activity B. Activity B accesses database and sends it back to activity A through putExtra() and getExtra().
Now my activity A is in declared like this android:launchMode="singleTask"
When I go back to activity A I want my this activity A to be updated or refreshed automatically. But to my wonder what I understood on debugging that if I declare an activity as launchMode="singleTask" then it just brings forth the screen to top from the stack. It does not actually go inside the code.
Is the concept what I understood correct ?
The solution I see is have a refresh button and on click of that access the code and update screen. But I do not want to do that. Do you think there is any other alternative ? I do not want to change launchMode="singleTask"
Thanks in Advance.
Try startActivityForResult(intent); and while you have finished in B setResult(RESULT_OK); and finish(); the B activity, and in A onActivityResult(int,int,intent); catch the result code if it is RESULT_OK update your A.
No matter which launchMode, when you switch from Activity B to Activity A, the onResume() method must be invoked. You can put your refresh code there to get your activity A updated.
put your refresh part in the onResume method .Once Activity B completed your onResume method will be invoked in Activity A.