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.
Related
I encountered strange problem, lets say I have two activities A and B, app starts with Activity A, I proceed to activity B press Android Home Button, return to app which brings me back to Activity B. Then I press Back button (either hardware on in toolbar) and this closes app, but it should return me to Activity A. Activity B has no override of onBackPressed and has Activity A stated as PARENT_ACTIVITY in manifest. I'm starting it with Intent with no flags. Any idea why this happens ? Thanks
The behaviour of back buttons depends on system version. There is support for providing back navigation in older Android versions, described here:
https://developer.android.com/training/implementing-navigation/ancestral.html
<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
android:name="com.example.myfirstapp.MainActivity" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
The best and most convenient way to debug back stack issues is to enable 'Don't keep activities' option in developer options.
That's my best guess. Good luck!
In order to run a new activity without destroying the old one, you have to add the flag FLAG_ACTIVITY_NEW_TASK to the intent that will run the activity:
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
as when setting this flag:
this activity will become the start of a new task on this history
stack. A task (from the activity that started it to the next task
activity) defines an atomic group of activities that the user can move
to. Tasks can be moved to the foreground and background; all of the
activities inside of a particular task always remain in the same order.
so the activity which started it will remain in the stack, and hence you can call it again, and hence it also can be called automatically again when pressing BACK_BUTTON even if you pressed the HOME_BUTTON earlier.
and you have to combine #gduh answer with mine, as for sure you must make sure that you are not calling finish(); in ActivityA while calling the ActivityB.
thanks for help, problem was caused by this flag for activity in manifest android:launchMode=singleinstance (it's not originally my project so I missed that, I just hope I didn't screw something else up by removing it)
In your activity A when you call your activity B, maybe you have the following command :
finish();
If yes, you should remove this line. Then when you press back key from your activity B you should return A.
If not, maybe try to share your code.
I want to completely remove an Activity from the Task Stack, but finish() only seems to minimize the Task. If I click on the button on my phone that browses all the open Tasks, it's still there. I would need to swipe it away to get rid of it.
How do I actually get rid of an Activity completely? Currently I have Activity_A that launches Activity_B. When I press back, Activity_B minimizes and Activity_A is brought to the front. How do I make it simply get rid of Activity_B and return to Activity_A?
EDIT:
I found the reason, Activity_B had Activity_A as the parent callback Activity. If you do not launch a new activity when A calls B, then it works properly (killing B kills the whole thing, A doesn't resurface).
Check is this what u need?
<activity android:name=".MyActivity"
android:noHistory="true">
</activity>
Have you tried:
intent = new Intent(view.getContext(),MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
What I ended up doing was that when Activity_A calls up Activity_B, it doesn't create a new Activity but instead just replaces it. So then when I get rid of B (by swiping it away, for example), it doesn't go back to A.
Otherwise, B will have a callback to A then basically brings A back to the front when you get rid of B.
i have custom activity which is called from background by receiver.
After on button click i want to close activity, so cannot be opened again if user holds menu button and list of active apps is displayed.
How can i do it? I tried use finish() method and kill process, but without luck..i can always display activity from list of apps again.
Thanks for any help.
Edit:
I Just added
<activity android:noHistory="true"
But after finish the activity i am able to get back into the activity (See image below).
Activity is started from receiver with following flags (maybe problem is here?)
intentOne.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
Try add android:launchMode="singleInstance" android:excludeFromRecents="true" android:noHistory="true" and in AndroidMainfest
just add android:noHistory="true" to the activity manifest.
http://developer.android.com/guide/topics/manifest/activity-element.html#nohist
android:noHistory
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.
This attribute was introduced in API Level 3.
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...
So here is my activity.
<activity
android:name=".MainActivity_Hard"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/title_activity_main_activity__hard"
android:noHistory="true"
android:screenOrientation="portrait" >
</activity>
I marked it as android:noHistory="true" because I did not want the screen to be added to the activity stack.
But however when I lock the screen and unlock it. The activity disappears and the previous screen appears. This should not happen. Could anyone tell me why.
Note: It happens in ICS (Samsung Galaxy Note), but not with previous devices (Gingerbread,etc.)
Setting the noHistory attribute in your activity means that as soon as the user navigates away from that activity--bringing up the lock screen included--its finish() function is called automatically, thus destroying it and going back to the previous activity that spawned it.
This is typically used for splash-screen-type activities which, for example, acts only as a launcher and you don't want the user to be able to go back to the splash screen from the child.
If this is not your intention, then you should consider removing the noHistory attribute and maybe managing the activity lifecycle yourself. (For example, if you don't want your activity to be in the stack, then just call finish() just after it starts another activity.)
android:noHistory
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.
I don't know why. But if you don't want to add this activity to current stack. You can set android:launchMode="singleTask". When start this activity it will be added to it own stack.
You can see more here enter link description here
Hope it help :)
In AndroidManifest.xml put this permission tag above application tag.
<uses-permission android:name="android.permission.WAKE_LOCK" />