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.
Related
I have 2 activities: A and B
In B, I have a back arrow to go back to A by calling "finish()" method.
It works fine, except when the following is done:
I go to B
I put my app in background
I restore my app from "recent apps"
I press the back arrow, and then, the app is finished instead of going back to activity A.
Any help please ?
I figured out how to solve this:
As #Karthikeyan mentioned in its comment, setting launchMode to "singleInstance" is cause of the problem. I changed it to "singleTask" and it worked fine.
In fact, according to the google doc stated in https://developer.android.com/guide/components/activities/tasks-and-back-stack,
"singleInstance".
Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance.
So logically, with "singleInstance", the activity when restored from the background had no other activity in the same back stack, and thus, calling finish() would simply finish the activity without restoring whatsoever (the very first activity is in the background and in another separate task)
Do not keep activities make sure that this option is not enabled in your device's developer settings.
Set flag in onStop () method to know and write condition onBackPress () condition to navigate to A according to the Flag value changes
Make Activity A as parent Activity of Activity B in your manifest file. Works for you.
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.
My project has 4 activities and users from activity A go to B after that to C and D. I need to create a button in activity D to close program directly because if user has to close all activities ( D ->C -> B -> A-> close) it would be unfriendly.
Register a broadcast receiver in each of the activities, listening for the "close all action", when the button in the last activity is pressed, send that broadcast, so all the activities register will execute their "onReceive" method on the broadcastreceiver, and there all them will be finished as long as they are registered.
This will definitely do the trick, although to be honest is quiet a poor implementation, chances that you are doing something wrong in the navigation are high, maybe fragments or a tab would be better suited for what you are trying, in stead of creating such a stack of activities...
Hope this helps...
Regards!
I think onActivityResult could be the better option.You could finish the activity if required task is being completed otherwise just backtrack on previous activity
You should override onBackPressed() from each Activity and call finish().
Assume the first activity in your application is named ActivityMain. Presumably it will be the oldest one on the stack.
Create an intent to start ActivityMain using the flag FLAG_ACTIVITY_CLEAR_TOP. Set an extra in that intent to indicate this is an application exit, and call startActivity() with that intent. This will clear the stack and get you back to Activity main.
In ActivityMain call getIntent() then check for the exiting application Extra value. If it is set, call finish().
A not so elegant solution:
Instead of calling startActivity, call startActivityForResult, from A to D.
On Activity D, when your button is pressed, set any result (let's say Activity.RESULT_OK) and call finish().
On each Activity (from A to C), override the method onActivityResult to check for the result. If the result is Activity.RESULT_OK, then you set the same result and call finish() again.
If you want, instead of just setting the result, add an Intent with some flag to tell the previous Activities to finish themselves.
Simply do one thing . In your activities add an overriden method onPause
onPause(){
finish();
}
This will close all your activities once you press back from any activity.
How can I restart An Activity on click?
For exemple: I have got in my AndroidManifest.xml 2 activities the activity A and B and they start when application starts...
But what I want is when I click in a button that is on Activity A it must restart activity B.
when you are in activity A, and proceeding to activity B, then your activity B automatically starts/re-starts
Why do you need to start both Activities on starting your app? When you say "restart", do you actually need to stop Activity B and start Activity B again? Or do you just want to show it? To start an Activity from another Activity, you could call something like this:
startActivity(new (Intent(this, ActivityB.class)));
The Android documentation gives plenty of detail. However, I think you should consider why you are starting two activities at once, and whether you might want to use a Service instead (not knowing any details of your app, I can't say).
Intent intent = new Intent(CurrentActivity.this, ActivityToLaunch.class);
startActivity(intent);
call above piece of code on onClick of view method.
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.