I have 2 Activity A and B, A calls B so, I want to Resume parent Activity (A) when B calls finish() on its Activity. Any advice will be useful.
UPDATE:
Maybe I should mention that I use fragments, each Activity has its own fragment, I call finish() from fragment hosted inside B activity and I expect to receive Resume on fragment belongs to A.
When you call Activity B, Activity A will go to background. Unless you finish Activity A while starting Activity B, it will automatically resume when Activity B finishes.
If you are calling this,
startActivity(ActivityB.class, this);
finish();
Just remove the finish(). It should work as expected.
Related
Assume I have 2 activities A and B. A is the main activity and set as singleTask so only one instance will be created.
Activity B can be instantiated many times. In onPause() in Activity B, I am calling activity A.
If I have 2 instances of activity B, then the first instance will call its onPause(), so the second instance will close quickly, because the first instance of activity B will call the instance of A and it will clear all activities and come to front.
I want to avoid that flow, instead the last instance of activity B should call instance of activity A.
How to do that?
I have an application that have three activities, lets call they A, B and C for convention.
A calls B with StartActivity.
When user hit the back/cancel button, I have to call Activity C, so I implemented in OnPause of Activity B to call Activity C and I need return from activity C, so I called Activity C with startActivityForResult and implemented the method onActivityResult in Activity B to get the return.
Everything is working fine, but when activity C finishes, the application is getting back to Activity A, and I need Activity B.
I have to call Activity B explicitly again or I made something wrong?
I'm really not sure what you want to achieve with this behaviour. Anyway, you get back from C to A, because you pressed the Back key, and didn't override it's behaviour in onBackPressed(). So the current Activity (B) just got finished, hence onPause() was called, so C started. But by the time C becomes active, you'll only have A and C on the Activity stack.
You need to override onBackPressed() in Activity B, and call C from there, forget onPause().
You shouldn't call another activity on hitting back button. Back button will pop activity(B) out of the stack. When you clicked back from Activity B Android will finish that activity and kill it. This is a standard workflow which better do not mess up. Place some button in activity B and call C for result from there, then you will be able to get a result in B activity.
When you get to the onPause() in activity B, it is already shutting down, so when you return to it, it will be gone.
You can either, as you say, start B from C when done, or start C in B's onBackPressed() (and then return from that method without calling the super method). This overrides the default action to shut down the 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.
I'm trying to finish the Activity A since Activity B when it will finish too. The Activity B was launches since Actvity A.
So I've paused the Activity A. How can I do it?
You may try using startActivityForResult() on the Activity A to invoke the Activity B.
And then, before calling finish() on the Activity B, you should call setResult() and call finish() on the Activity A in onActivityResult()
Example here
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.