I have two different application called App A and App B.
App A : ActivityA with launch mode -> "singleTask"
App B : ActivityB with launch mode -> "singleInstance"
Steps:
Open App A which will open ActivityA, ActivityStack : ActivityA
Starting App B with startActivityForResult(), ActivityStack : ActivityA > ActivityB
Open ActivityA via Intent from Activity B , ActivityStack : ActivityA > ActivityB > ActivityA
As you can see the activity stack, It creates new instance of the Activity A instead of returning to the same instance of the ActivityA by calling onNewIntent() method.
I am not sure, is this because of Activity started for result.? Please help to provide proper solution in this scenario. Thank you.
You cannot start a singleTask Activity using startActivityForResult(). If you launch an Activity and expect a result returned, then the target Activity must be launched in the same task. Due to this, Android is ignoring the singleTask launch mode when you call startActivityForResult().
Related
I have 2 activity (Activity A and Activity B)
Activity A is singleTask and Activity B has standard launch mode.
When application started with Activity A and launch mode is singleTask, now from activity A start new activity B on button click.
So Stack is Activity A, B
Now wanted to reorder Activity A which is single task from Activity B but it's finishing Activity B first and then create new Activity A.
So stack at the momemnt is only Activity A.
What I've tried from Activity B.
val intent = Intent(this, ActivityA::class.java)
intent.flags = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
startActivity(intent)
Problem: Without finishing Activity B required to reorder Activity A with launch mode singleTask.
Appreciated in advance.
You cannot reorder an Activity with singleTask launch mode to the front of the stack. An Activitywith singleTask launch mode is by definition the root Activity in a task.
Why would you want to do this? If you've configured the Activity as singleTask, you have told Android that you want this Activity to be launched as the root of a task. Why would you want to reorder this Activity to the front of the task? If the Activity can exist in the task somewhere other than the root, you shouldn't define it as singleTask!
I have 4 activities(ActivityA, ActivityB, ActivityC, ActivityD).
I Moved from ActivityA -> ActivityB -> ActivityC -> ActivityD.
Now I want to know what I do in fallowing 2 conditions.
When go to ActivityB from ActivityD by skip ActivityC
When go to ActivityA from ActivityD by skip ActivityB & ActivityC
Note: Here i could not want to call finishaffinity method to finish all activity and launch desired Activity.
Declare your activity Activity A and Activity B in manifest with the SingleTask launch mode.
The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one. It will also clear all the activities which are over that Activity. For eg- If you start Activity B from Activity D, then initially your stack would be A->B->C->D which will then be changed to A->B.
Check this out for the complete official documentation
Hope it helps.
I want to start my MainActivity with a new Intent in my other Activity. The two Activities are in the same app, and the second Activity is actually started from the MainActivity. So the scenario is like this:
MainActivity is created with an Intent
MainActivity starts SecondActivity (but MainActivity is not destroyed yet. It is just stopped)
SecondActivity starts MainActivity with a new Intent (SecondActivity is not closed)
The MainActivity is not flagged. I mean, the Activity's launch mode in the manifest is not set (so, it's default).
I want to know what happens to MainActivity's lifecycle and intent.
Is the Activity re-created? Is onCreate() called? Then is onCreate() called twice, without onDestory()? Or the new MainActivity is newly created and there will be two MainActivities? Will the Intent from getIntent() overwritten?
I know Activity.onNewIntent() is called for singleTop Activities. Then in my situation onNewIntent() is not called?
Thanks in advance.
Is the Activity re-created? Is onCreate() called? Then is onCreate()
called twice,
Yes, yes, and yes, because the default launchMode of an activity is "standard". Activity with standard launchmode will create a new instance how many times you want.
Will the Intent from getIntent() overwritten?
AFAIK, It's still the same Intent.
If you call startActivity() for an Activity with default launch mode(i.e, you didn't mention any launch mode in either in manifest or in Intent) a new instance of the activity is created.
For example, A launched B and again B launched A then Activity stack would be A - B - A. Pressing back key at this point would take you to B then A.
Your can refer to Tasks and BackStack documentation from Android.
I have a widget for my application, which need to be somewhat independent from the app.
The activity workflow should be like this:
Widget -> Activity acting as receiver
Receiver -> LoginPage or Activity A (depending on login status)
LoginPage -> Activity A
Activity A onKeyDown -> Activity B
Activity B onKeyDown -> Home Screen.
I have no problem until Activity B, which sends back to Activity A when I press onKeyDown. I'm using FLAG_ACTIVITY_CLEAR_TOP flag and finishing the Activity when starting the activity B.
When I move from ActivityA to ActivityB using the CLEAR_TOP flag, I supposed that Activity stack is cleared, then in ActivityB I finish the Activity on the onKeyDown() method, assuming that the App will be closed, but it doesnt. Why?
I'm also trying to use FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK in the receiver but I dont understand the mechanism pretty much. Any idea about this?
In fact the FLAG_ACTIVITY_CLEAR_TOP, start your activity B if its not started or it came back as the second activity on the BackStack. To finish Activity A, you can call finish() after starting Activity B or add no history flag, when starting A.
#JesusS: I doubt if u can finish ur activity in that fashion during a forward transition.
Consider a scenario of moving from Activity A to Activity B. Now if u want to kill Activity A and want to move to Activity B then call the startActivity(intent);
(where ur moving from activity A to B)
without any flags on the intent followed by the finish() on activity A.
As per my understanding u can use Intent.FLAG_ACTIVITY_CLEAR_TOP only during backward transition i.e when u already have that activity on the stack.
Consider the following scenario:
A --> B --> C --> D
Now if u want to move back from activity D to Activity A by clearing the activities u can go for Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP.
The result is that the Activities D, C, B(LIFO) will be removed from the stack and the activity A resumes by calling the onResume() of Activity A.
Example: I have an android app with 3 activities that has the following behaviors:
A (Home) -> B -> C
Activity A launches Activity B
Activity B launches Activity C
When user is on Activity B and they hit the Back button, it takes them Activity A
When user is on Activity C and they hit the Back button, it takes them Activity B
What I would like is when user is on Activity C, if they hit the "My Root Activity" button, it will take them to Activity A without adding a new instance of Activity A to the back stack.
So I don't want to have:
1) A
2) A-B
3) A-B-C
4) A-B-C-A
What I would like is:
1) A
2) A-B
3) A-B-C
4) A
How can I do this?
Check out the intent stack machinery!
http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html
EDIT:
You can add "singleTaks" or "singleInstance" to your activity in the manifest file and implement the onNewIntent() method.
"singleTaks"
The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.
"singleInstance"
Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task.
Check out this link
I have each child activity extend a base activity. In the base activity, I defined an onActivityResult() which will impose a finish() if the activity at the top of the stack set a specific resultCode. So if they hit the "My Root Activity" button on Activity C, it will recursively roll up to Activity A. The Back button maintains it's functionality.
This is a rare scenario as I would have used "singleTask" but launching Activity A involves reloading it's dependencies which I don't want to couple of Activity C.