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
Related
I am new to android so don't know what can be the solution to this problem.
I have created an app with two activities in it named these as first and second activity.
First activity launch mode is singleTask and second activity launch mode is standard.
Now when i switch from second activity to first activity i don't want to destroy the second activity.
But in this case it is getting destroyed.
So can anyone help me in suggesting how can i achieve this scenerio of switching from second activity to first without
destroying second activity.
This is because activity one is single task activity.
Read here. According to this
"singleTask" and "singleInstance" activities can only begin a task. They are always at the root of the activity stack. Moreover, the device can hold only one instance of the activity at a time — only one such task.
To avoid this scenario, change launch mode of activity one to standard.
No need to destroy any activity for this, i think so. Just implement this:
Intent intent=new Intent(ActivityOne.this,ActivityTwo.class);
startActvity(intent);
in your oncreate() method of actvity
You can open new activity that you want to open, without finish() your current / previous activity by using finish().
Once you open multiple activity without finish() previous ones, the OS will automatically maintain the activity stack.
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...
I have an application with two activities A and B. On clicking on list item in activity A it takes me to the activity B.
For the first time the activity works fine.
Now I have a broadcast reciever which throws me a notification , on clicking on that notification it takes me to the activity B.
The problem is when i am allready on activity B and the new notification comes and when i click on it it relaunches the same activity.When i press back the activity is not destroyed properly as i am getting logs of previous Activity B when i am currently on Activity B.
I have tried out allmost all suitable flags like Intent.Flag_Clear top etc...
But it isnt helping me out.
My activity sometimes works fine and sometimes doesnt work properely.
Please help me out stuck on this problem since a week
I think the launchmode attribute in your Manifest will do the trick. See http://developer.android.com/guide/topics/manifest/activity-element.html and scroll down to the launchmode.
singleTask or singleInstance sounds like it is what you want
You can change the launch mode of your activity on the manifest file. See this activity attribute: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
in AndroidManifest.xml use android:noHistory="true" in your activity. This will destroy your activity B destroyed whenever you leave it.
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 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.