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.
Related
I have activities A and B. Activity B is started by a service while the application is closed and activity A is not on the stack. Activity A has members that B requires access to. I would also like the home button to open activity A from Activity B even if activity B is started from a service. Is there a way to inject activity A back into the stack?
In your Service start ActivityA not ActivityB and pass some parameter to ActivityA.. and in ActivityA's OnCraete method launch the ActivityB.. now you have both in the stack.
You can declare the logical parent of each activity in your manifest file, using the android:parentActivityName attribute (and corresponding element),for home button working.
And to add a activity back to stack
Check this link for more details.
That won't be a good decision, better use the members in the B Activity and Once Activity B is launched and it's about to leave just give intent to Activity A.
Also you can check if Activity A is already in the stack. If already in the stack, it will popup by itself, if not found, can pro grammatically trigger it.
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.
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
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
I have an activity than can be called from my parent activity and from other application via intent filter (i.e. ACTION_VIEW).
When I call finish() inside my activity, how to return to the correct caller?
i.e:
other application -> my activity -> finish() -> other application
currently if my main activity is still running, the finish() will return to my main activity, although it was called from other application.
If you start one activity with startActivityForResult(Intent) then you can get the second activity which started the first one with getCallingActivity().
If you call the finish method on your activity, you will be returned to the topmost activity in the history stack. By default, it is the previous activity that you accessed.
For example:
Main > A > B
If finish is called on Activity B, you will return to Activity A.
If finish is called on Activity A, but Activity B is still alive, you will stay on Activity B since it is the topmost activity in your history stack.
If finish is called on Activity B, but for some reason Activity A is not in your history stack (most probably, if you specified that Activity A must not be saved in the history stack), you will return to Activity Main instead of Activity B.
Read up on Tasks - by calling the activity from a different task (/application), you're bringing the existing Task to the foreground, which might include other Activities in the back stack that you don't want. I would suggest specifying the android:taskAffinity of the Activity that can be started from other Applications. Since this will ensure that this Activity will be the only one in the Task, it doesn't matter if it is started from within it's own app, or another.
EDIT: Figure 4 in the Tasks link shows your situation.
Use startactivityforresult() when you want to call the child activity. You'll return to the good one.