I have two activities A and B. in A I start B activity. In B acitivity when user press back button I need to update A's layout values. So I need to know
In acitivity A which function will be called when return back to activity A.
try and override onResume() function in A. works for me.
Take a look at the Android life cycle: http://developer.android.com/reference/android/app/Activity.html
You will see that when you return to Activity A, function onResume() will be called.
A key point when learning Android API is the Activity Lifecycle.
When an activity is displayed : it's onResume() method is called. So it will be called:
when the Activity is displayed for the first time (after onCreate(...))
every time the activity came to front
after activity re-created (for instance, after screen rotation)
For more details read this : activity pausing-resuming and more global overview of the lifecycle.
The Activity javadoc is also very good resource
Related
I have 2 activities which one activity leads to the other activity.
The first activity present a listview and the items click leads to the second activity.
When I click the back button I get back to the first activity but the list reload and scroll up to the first item. I want the list to stay at its place after I get back to it.
If you call finish() in activity A when openig the new Activity B on back press you will call onCreate() of activity A to avoid this avoid calling finish() in activity A in your onItemClickListner()of activity A and record tge position of tge click in Activity A, in which case calling the back press in Activity B will cll for tge onResume() in activity A where in you could call For a direct scroll:
getListView().setSelection(<position>);
Or For a smooth scroll:
getListView().smoothScrollToPosition(<position>);
when you press back you call oncreate() on that activity so everything reload
you could use :
getListView().smoothScrollToPosition(yourpostion);
and think about using fragments for another way around it .
and recyclerview is advised .
If you don't call finish() on Activity A, even if you go to Activity B and come back, Activity A should not call the whole onCreate() again.
If you take a look at the life cycle of Activities, it will put Activity A in onPause() and probably onStop() depending on what you are doing on Activity B and how you defined launchMode in AndroidManifest.xml.
So when you are calling startActivity(..), don't call finish(). Otherwise it will load everything again to draw the Activity A.
Another possible way is using
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
..
}
OR you can use SharedPreference.
Once fetching data from Parse.com is over (like onPostExecute() of AsyncTask), you can read the data passed from Activity B to relocate the user to the list where they were.
EDIT:
Read this article about how to "come back" to the activity you were in, too.
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 Android, from Activity A, if I start new Activity B, Android will automatically save all the state of Activity A. So when I click back or call finish() from Activity B, Activity A will be restored to the state when I start Activity B (for example: position of a ScrollView in Activity A, or the value of ProgressBar). In Activity A I have a ProgressBar showing progress of some task, then if I come back from Activity B, the ProgressBar is restored and keep running.
Now what I want is, from Activity A, I will call finish(). How can I save all the state of Activity A just like Android did (include the ProgressBar as I describe above), so when I start Activity A again, I can restore everything like before calling finish(), and my ProgressBar keep running?
Can I push Activity A to something like stack in Android and then pop it out? Anyone know how to achieve this?
Do not call finish on clicking the back button.
Instead call the method "moveTaskToBack(false)". This will 'minimize' your application.
Next time you open the application the application opens with the previous state.
The whole point of finish() is to say that your Activity is "done and should be closed". If you aren't done, just don't call finish().
When you go from Activity A to Activity B, Android does not kill Activity A. Activity A still lives in the background. It can however be killed at any moment if Android needs the memory.
To save the state of an activity you have to do it manually.
Here's a similar question with a good answer: Saving Android Activity state using Save Instance State
If you want to finish your activity and then start it again just to load some new configurations, You can call activity's recreate() method instead of calling finish(). Calling recreate() will call onSaveInstanceState() and onRestoreInstanceState(), helping you to restore your previous configurations..
You can look at this answer for more detail : How do I restart an Android Activity
What activity state is called when another activity, in the same application, is launched, and then the backpress button is clicked to navigate back to it?
What lifecylce method is called during the proccess of going back to the previous activity?
onPause() is called in Activity A when it launches Activity B. After the back button is called in Activity B, onResume() is called in Activity A.
What lifecylce method is called during the proccess of going back to
the previous activity?
According to the docs: onPause(), onStop(), and possibly onDestroy(). In addition to the lifecycle documentation, you might want to read the docs on Tasks and Back Stack.
I have an activity say A from which I am starting an activity say B through startActivity().
How do I pause/resume activity B from activity A?
How do I pause/resume activity B from activity A?
You can't, sorry. Most likely, no code from Activity A is even running while Activity B is on-screen.
It's a little difficult to determine what you're asking for. If you simply want to end Activity A and go back to Activity B, then you would call finish() to kill Activity A, or even just use the Back button to end it and go back...
onPause() and onResume are part of an Activity's life cycle. They are called by the Android framework and should never be called explicitly.