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!
Related
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().
I have an app that has multiple activities, one activity opens the other and so on. I have a serious problem when it comes to returning to the previous activities. I want to return to the previous activity in the state I left it in(I do not want to recreate the activity). I was able to do so with three activities, but the fourth activity skips the third activity and returns to the second, for example:
Activity A -> Activity B -> Activity C -> Activity D
What I want when I press the back button and the Up button:
Activity A <- Activity B <- Activity C <- Activity D
I initiate Activity A as a "singleTask", then I launch the next three activities like this:
Intent intent = new Intent(context, ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
startActivity(intent);
This works perfectly with Activity B and C, but when I get to Activity D and try to return to Activity C, it takes me to Activity B instead of Activity C.
I have been through the internet and I just come seem to really understand the use of Intent flags and activity launch modes. Can someone please assist, pretty please?
Using the launch modes and flags IS the problem. Maybe you'll want to assign activity A "singleTop", but you don't need to use flags when launching B, C, or D (at least not in the situation you're describing). Then pressing back will behave as you expect it to (unless you're overriding it).
Activity A started B, now the Activity stack will be A,B (B at top).
Now from Activity B i started Activity A by using intent FLAG_ACTIVITY_REORDER_TO_FRONT , now the Activity stack will be B,A(A at top).
Again I tried starting Activity B from Activity A using intent FLAG_ACTIVITY_REORDER_TO_FRONT, which results in bringing Activity B to front but destroying activity A.
Remove launchMode="singleTask"from your manifest. This is causing your problem. Also get rid of clearTaskOnLaunch="false", this is the default setting you don't need to specify this.
There are two activities in my application Activity A ( Launcher Activity) and activity B. first i launch an application(A activity) . onBackpress(),i am moving A to backstack. I am launching other app and firing intent from there to launch activity B. Now activity A which is in background is getting launched first then B is coming top of it.
How to make activity A to be in backstack and opens only activity B on firing intent from other application?
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.