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.
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 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.
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 four activities A,B,C,D. I am passing an text from activity A to Activity B using bundle and Activity B is displaying it nicely. Then i move from Activity B to Activity C and then Activity D. After that i called Activity B from the Activity D with the help of intent and i seeing no text are there in Activity B.
Please suggest me the way to keep the text there, with some code example.
You would need to use either FLAG_ACTIVITY_CLEAR_TOP or FLAG_ACTIVITY_REORDER_TO_FRONT.
Without one of these you are creating a new instance of your activity instead of revealing the old one. You can set activity flags on the intent you start from activity D:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
before you call startActivity.
Support App starts activity A, then A starts activity B and finishes itself. After that activity B starts activity C.
Now the stack contains B and C, with C at the top.
Then I click a button in activity C, and want it to clear B and C and start activity A, i.e. I want activity A to be the only activity in the stack. How can I make it?
Edit: I made a test to use FLAG_ACTIVITY_CLEAR_TOP. But it didn't work in my case, because activity A is not running when button in activity C is clicked.
Set the FLAG_ACTIVITY_CLEAR_TOP flag on your intent to start activity A.
Edit: Is there a reason you can't leave A going? Then you could do as suggested.
Otherwise, another (more complicated) option:
In B start C forResult. When A is started from C, you could finish C with a result indicating to B to also exit.