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.)
Related
I am in an Activity A and make a call to another Activity B using the
startActivityIfNeeded() method with the REORDER_TO_FRONT flag because the Activity B is inside the activities stack but I don't know what life cycle method this startActivityIfNeeded() method makes the call.
Intent mIntent = HomeActivity.callingIntent(CartActivity.this, bundle, false, true);
mIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivityIfNeeded(mIntent, 0,
ActivityOptions.makeSceneTransitionAnimation(CartActivity.this, llTransitionBar, "navigationBarTransition").
toBundle());
Question: What method of the life cycle of an Activity does the StartActivityIfNeeded() call with the REORDER_TO_FRONT in case the Activity I want to call if it is within the Activity Stack ?, 'cause I want to implement input transitions in the new Activity
First of all, if ActivityA is launching ActivityB, then you don't ever need to use startActivityIfNeeded(), because this call would never return false, it would always actually launch an instance of ActivityB and return true. Instead you should just use startActivityForResult().
If you use startActivityForResult() with FLAG_ACTIVITY_REORDER_TO_FRONT and there is already an instance of ActivityB in the stack, you should see the following lifecycle calls:
ActivityA.onPause()
ActivityB.onNewIntent()
ActivityB.onResume()
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.
I want to start my MainActivity with a new Intent in my other Activity. The two Activities are in the same app, and the second Activity is actually started from the MainActivity. So the scenario is like this:
MainActivity is created with an Intent
MainActivity starts SecondActivity (but MainActivity is not destroyed yet. It is just stopped)
SecondActivity starts MainActivity with a new Intent (SecondActivity is not closed)
The MainActivity is not flagged. I mean, the Activity's launch mode in the manifest is not set (so, it's default).
I want to know what happens to MainActivity's lifecycle and intent.
Is the Activity re-created? Is onCreate() called? Then is onCreate() called twice, without onDestory()? Or the new MainActivity is newly created and there will be two MainActivities? Will the Intent from getIntent() overwritten?
I know Activity.onNewIntent() is called for singleTop Activities. Then in my situation onNewIntent() is not called?
Thanks in advance.
Is the Activity re-created? Is onCreate() called? Then is onCreate()
called twice,
Yes, yes, and yes, because the default launchMode of an activity is "standard". Activity with standard launchmode will create a new instance how many times you want.
Will the Intent from getIntent() overwritten?
AFAIK, It's still the same Intent.
If you call startActivity() for an Activity with default launch mode(i.e, you didn't mention any launch mode in either in manifest or in Intent) a new instance of the activity is created.
For example, A launched B and again B launched A then Activity stack would be A - B - A. Pressing back key at this point would take you to B then A.
Your can refer to Tasks and BackStack documentation from Android.
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 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.