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.
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 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.
Imagine you have the following sequence of activities:
Activity A -> Activity B -> Activity C
When you are on Activity C, pressing the native back button, takes you to Activity B. Now what is the state of Activity C? Is it still in memory or it has been finished?
If it is still in the memory, is there a way to resume the activity? Other than starting another instance of this activity...
I should add that this is the standard case where you do not use any flags including: FLAG_ACTIVITY_CLEAR_TOP
Default behavior is that when you press hardware "back" button, current activity will be removed from the backstack and activity "destroy" sequence will be initiated. From that moment you should not rely on the fact that it might be somewhere around - it is all up to Android to decide when does it actually kill this activity.
What my previous investigations show is that victim's onDestroy() will be called only when new activity is done loading and is idle.
You can specify android:launchMode="singleInstance" for your activity in Manifest. This will ensure that only one instance of activity is created at the time
You might want to consider reading the official docs.
More specifically the part that answers your question:
When the user presses the Back button, the current activity is popped from the top of the stack (the activity is destroyed) and the previous activity resumes (the previous state of its UI is restored).
Now for your second question… you can keep reading the same page…
when you start an activity, you want to bring forward an existing instance of it (instead of creating a new instance on top of the back stack)
So if you read that… you will find…
You can do these things and more, with attributes in the
manifest element and with flags in the intent that you pass to
startActivity().
In this regard, the principal attributes you can use are:
taskAffinity
launchMode
allowTaskReparenting
clearTaskOnLaunch
alwaysRetainTaskState
finishOnTaskLaunch
And the principal intent flags you can use are:
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
No, it is not in memory. It gets finished when you press the back button. You should use android:launchMode="singleTask" in the androidmanifest.xml for that particular activity for which you want no new instances to be created if an instance is already there. For further information this link will be helpful.
In the following sequence...
Activity A -> Activity B -> Activity C
When you will press back button in Activity C then onBackPressed() method will be called. The default behavior of onBackPressed() is to finish the current activity until you Override the functionality.
So, in normal case, after back press the current Activity will be destroyed and you can't find it in the Activity Stack, so you can't resume it.
You will find more information from Android Developer's doc in below link...
Tasks and Back Stack
What is the difference between Intent.FLAG_ACTIVITY_CLEAR_TOP and finish() in Android?
The differerence between these two are as follows:
1.finish() you can use to end the activity in which you are right now present and also it will end one activity at one time.
2.In case of FLAG_ACTIVITY_CLEAR_TOP,It will end all the activities those are on top of the current activities inside the stack.There may be more than one activity.
suppose you are starting activities one after another in the order
A-->B-->C-->D,ie activity B started from activity A,activity C started from activity B and so on.
Now calling startactivity(A) from activity D with intent flag FLAG_ACTIVITY_CLEAR_TOP finishes all activities in between (here B and C) and starts A.
calling Finish() from your activity closes current activity
finish() android uses to end the activity by calling it in program.
(Note, you can also use onDestroy()).
FLAG_ACTIVITY_CLEAR_TOP clears all the activities that are top of the current activities inside the Activity 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.