I have 2 activities (A and B) and they have 2 buttons to switch between.
A oncreate
B oncreate
A oncreate
A onresume
what I wanted to do is after sending intent from B to A oncreate should not be called but at this point it does. To overcome that I found FLAG_ACTIVITY_REORDER_TO_FRONT (from here) and thought it could called only onresume but it didn't.
FLAG_ACTIVITY_REORDER_TO_FRONT does exactly what you think it should do. However, if you start ActivityA and then ActivityA starts ActivityB and calls finish() on itself, then when ActivityB starts ActivityA with an Intent that has FLAG_ACTIVITY_REORDER_TO_FRONT there will be no instance of ActivityA to bring to the front. In this case Android will simply create a new one. I can only assume that is what you are seeing.
FLAG_ACTIVITY_REORDER_TO_FRONT changes activity history. If the requested activity is found in the history of previously visited activities (in a task), the older history record for this activity is cleared. So, while pressing back button, user will not encounter this activity in a task.
This flag won't affect the call to onCreate(), If activity does not exists in the task (not loaded or destroyed), onCreate() will be called to create it.
You can't just cancel onCreate. If B is full screen activity android can kill A activity and will recreate it when you try to restart it with FLAG_ACTIVITY_REORDER_TO_FRONT flag and call it's onCreate method. If Activity A will be still alive at the monent when you try to bring it to front, onCreate method should not be called.
Maybe in your case you should try to use fragments?
Related
I have created an activity , set it's filter as a Home activity and set launchMode with singleTask. When I press home ,the activity will be in onpause state then onStop . What confuse me is why the activity will be re-launched when back from icon displayed on "home pick" dialog ? It calls onCreate() again without invoked onDestroy().
I know there is a way to solve this problem that is to set launcherMode as singleIstance, but this way is not good enough, because it will cause an other issue that the activity will not be able to process onActivityResult.
You may want to tell what you wish to achieve. Because that's how singleTask is. It delivers existing intent to onNewIntent() of the existing instance of activity. So activity will not be destroyed but onCreate() will be called, as it will reCreate activity with existing intent.
Quoting the documentation
The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.
I wrote an Android app with several Activities and a Main Activity. When I go from the Main Activity to lets say Activity B, I want to "pause" the Main Activity, when the back button is pressed it should go back and reactivate the Main Activity instead of calling onCreate().
This shall work with all Activities, so if I click on a button to start Activity B, it shall also reactivate the old Activity B instead of creating a new state with onCreate().
How can I realize this?
PS: I already tried it with parcelable, but this do only work, if I close the application or something unexpected happens.
It's always possible that the first activity may be destroyed when you start another activity. It will be recreated when you go back to it. Every app should be written with this possibility in mind. To make sure your activities can handle being destroyed and recreated, turn on the "don't keep activities" developer option.
It is by default in Android. If you Start Activity B from Activity A then activity A goes in stopped state. Below methods will be called of Activity A
onPause()
onStop()
When you tap on Back key on Activity B. Below methods of Activity A will be called.
onRestart()
onStart()
onResume()
For reactivating Activity B, you can set Activity B launch mode as singleInstance. This will make sure that only single instance of Activity B will be created. onCreate will be not be called again. onNewIntent will be called when that activity is reactivated.
Refer: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
Activities live in stack order. Each activity has its life cycle. When you close an activity (Activity B in your example) it eventually reaches onDestroy() method and removed from the stack order. It is by default in Android and there's nothing you can do about it.
What you can do is rewrite the onStop() method in Activity B and save some activity data (like text in EditText, for example) in SharedPreferences - read here. Then in your onCreate() method you can call for SharedPreferences, check if it's not empty and restore the text in the EditText by pulling appropriate value by key.
In my app, there is a activity A, which it's a main activity, also there are several fragments inside A. When you click the images in one of fragments, it will start a new Activity B. When you click back button, i will call finish() to finish the activity and return to Activity A. But when returning to the Activity A, onCreate() of A is called again. Why onCreate() is called each time? As i know, it should be just called once, and then onStart() should be called.
From segment to the Activity B is as below:
Intent i = new Intent(_scontext, ProductListing.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_scontext.startActivity(i);
getActivity().overridePendingTransition(R.anim.push_out_to_left,R.anim.push_out_to_right);
When click back button in the Activity B, the code snippet is as following:
Intent _gobck = new Intent(_ctx,ProductDisplay.class);
startActivity(_gobck);
finish();
overridePendingTransition(R.anim.push_out_to_left,
R.anim.push_out_to_right);
What's wrong with the code? Am i missing anything?
You are starting the activity again. Remove the following code and it will work.
Intent _gobck = new Intent(_ctx,ProductDisplay.class);
startActivity(_gobck);
Since you already got your answer by #Rajitha Siriwardena but i just want to clear some of the points here,
As i know, it should be just called once, and then onStart() should
be called.
Above sentence is not a true first of all .
There is possibility to for your ActivityA to go in OnCreate even if you finish your ActivityB. If your ActivityB stay in foreground for a long time ,of-course your ActivityA will be in background in that case , so ultimately your Activity in onStop (remember not in onPause) and android Activity life cycle doc says, after onStop if your app want reach your Activity then it will goes in onCreate
So finish() ActivityB would work but there is no guarantee to your ActivityA called onCreate when you do so .
if you remove finish() from your backPress Activity will not be created and you don't need to write Intent it will manage it's back stack it self.
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.
I need to launch the same instance of an activity even if the user goes back and forth with the navigation. The user navigates in a stack of different activies (A, B, C), but when he goes to one of these activities it will show the same instance of that activity (like a static activity) calling only onResume.
You can't force your Activity to start up in onResume(). What you can do, however, is save your Activity's state to a Bundle in onPause() and onStop(). Then, in onStart() read this Bundle to get your Activity's state.
The closest you can get to this is to use FLAG_ACTIVITY_REORDER_TO_FRONT in the Intent you use with startActivity().
However, this will call more than onResume(). At minimum, your activities will be called with onRestart() and onStart(), assuming that whatever was in the foreground took over the whole screen.