How to skip and call an activity in the back stack - android

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?

Related

Why is onSaveInstanceState called when starting new activity?

Why is onSaveInstanceState called when starting new activity with startActivity(intent), but when user presses back or home from new activity, onRestoreInstanceState is not called?
When you navigate from Activity A to Activity B, Activity A will have its onSaveInstanceState() method invoked in case Activity A is destroyed (because the system needs to free up resources).
When you navigate back from Activity B to Activity A, there are a few different paths the code can take. If Activity A is still up and running, it will simply resume (and you'll see onResume() called). If instead Activity A was destroyed by the system while you were on Activity B, Activity A will need to be recreated. You'll see onCreate() called by the system (and its param savedInstanceState will be non-null and populated with what you put into it in onSaveInstanceState()) as well as any other lifecycle methods that happen "after" onCreate().

Resume Parent Activity

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.

Activity starting another activity. Guarantee that the first activity's onPause will be called before second's onResume

I have an Activity A and Activity B. Activity A starts Activity B.
Is there a guarantee that A's onPause() will always be called before calling B's onResume()?
This is related to this entry
Found the answer here:
When activity B is launched in front of activity A, this callback will
be invoked on A. B will not be created until A's onPause() returns, so
be sure to not do anything lengthy here.
onPause ()
When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here
http://developer.android.com/reference/android/app/Activity.html#onPause()
Yes. Activity A's onPause() will be called before passing to Activity B's onResume() if the navigation is not the first time. If navigating to Activity B is for the first time, then Activity B's onCreate() will be called after Activity A's onPause(). Take a look at Activity's Lifecycle for clear understanding.

android: when does onCreate get called when navigating through activities and how to use a service with them?

suppose that you have two activities A(which is the main activity) and B
when you initially start the application the onCreate of A is being called since that's the main activity
now from A you can go again to B
if you go to B then onCreate of B will be called
if you go back to A what will be called? again onCreate or something else?
now suppose that I want to start a service that will update an arraylist every 10 seconds. The values of this arraylist will be shown in the B activity.
So I will probably start this service in the onCreate of A activity. When the onCreate of A is being called, the service starts doing its job
now, then if I want to view the contents of the arraylist, I will go to the B activity. In the B activity initially the onCreate function will be called, so I can just show the values of the arraylist in this onCreate function, but if I leave this activity, and go to A, and then again back to B, will onCreate be called so that the NEW values of the arraylist will be displayed correctly and not the old ones?
thanks in advance
If you return to Activity A from Activity B, the onRestart or onResume method will be called. Take a look at the Activity Lifecycle.
Anytime you navigate from an Activity and then return to it, it does not call onCreate again.

where will android finish() return to

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.

Categories

Resources