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.
Related
I have an application that starts activity A. The user can then start service S from activity A. When a certain event happens, service S starts activity B. Activity B only has one button, and when pushed, should return to activity A. Everything works fine, except for when activity A was closed out using the back key. When the back key is pressed, instead of onPause, onDestroy is called. So when activity B is dismissed, I get sent back to the homepage instead of Activity A. So my question is, how can I make sure that Activity B reopens activity A if activity A is onPause, and also reopens the activity A when it's onDestroy has been called. I'm thinking to use intent filters, but I can't figure out to get the right combination. I do not want multiple instances of Activity A. Thanks for the help.
on activity A:
public void onBackPressed() {
moveTaskToBack(true);
}
or on Activity B, force it to start activity A,
public void onBackPressed() {
Intent i = new Intent(B.this, A.class);
startactivity(i);
}
but since you implement the first one,
you dont need this last code.
I Hope it helps
Since you don't need to save A state,
you can override onBackPressed() method in B to start A again.
And if you don't want multiple instances of A, use FLAG_ACTIVITY_CLEAR_TOP when you start B.
Hope this helps
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 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?
I have some Activities, let's say Activity A, B and C.
In the Activity A I call the B through a Menu with an onOptionsItemSelected:
Intent main = new Intent (this, MainActivity.class);
this.startActivity(main);
Now, when I'm in the B activity, I can call the A one back in the same way (with Intent and startactivity): how can i handle it to call the OnResume or the OnRestart method of A instead of the OnCreate one?
I'm logging it and anytime I move from an activity to another one, it always call the OnCreate method: what can I do?
Configure your Activity A as "singleTask" or "singleInstance" in the manifest.xml. Then Acitivity A's onResume() will be fire instead of onCreate() when you call Activity A from Activity B (assuming Activity A was already instantiated like you describe). There are drawbacks to this kind of configuration so read this.
example manifest:
<activity android:name=".YourActivityA"
android:configChanges="keyboardHidden|orientation"
android:launchMode="singleTask">
You cannot control whether the OnCreate/OnResume/OnRestart methods are called directly. It is based on whether the previous activity is still in memory or not.
http://developer.android.com/guide/components/activities.html
http://developer.android.com/images/activity_lifecycle.png
I'm logging it and anytime I move from an activity to another one, it always call the OnCreate method: what can I do?
To return to Activity A from Activity B, if you use:
startActivity(), then A's onCreate() will always be called.
finish(), then A's onCreate() may or may not be called. (It depends on whether A was destroyed by you or the OS.)
suppose that you have two activities A(which is the main activity) and B
when you initially start the application the onCreate of A is being called since that's the main activity
now from A you can go again to B
if you go to B then onCreate of B will be called
if you go back to A what will be called? again onCreate or something else?
now suppose that I want to start a service that will update an arraylist every 10 seconds. The values of this arraylist will be shown in the B activity.
So I will probably start this service in the onCreate of A activity. When the onCreate of A is being called, the service starts doing its job
now, then if I want to view the contents of the arraylist, I will go to the B activity. In the B activity initially the onCreate function will be called, so I can just show the values of the arraylist in this onCreate function, but if I leave this activity, and go to A, and then again back to B, will onCreate be called so that the NEW values of the arraylist will be displayed correctly and not the old ones?
thanks in advance
If you return to Activity A from Activity B, the onRestart or onResume method will be called. Take a look at the Activity Lifecycle.
Anytime you navigate from an Activity and then return to it, it does not call onCreate again.