And again about Android activities back stack magic. So I have two activities A→B (playlist A and player B), and I want the app should always close when Back pressed in A. But it doesn't.
Both activities launches as singleTop, B also has android:excludeFromRecents="true" and defined A as parent. B goes back to A with following flags Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK.
Everything works fine except the next case:
A→B,
Home pressed,
Launch app again from desk and got A opened,
Press Back button,
Here app should be closed but B shown again.
Any suggestions? Thanks!
When you are using the Intent.FLAG_ACTIVITY_NEW_TASK , it means a new instance of the activity is launched. You may have to use Intent.FLAG_ACTIVITY_SINGLE_TOP flag with single task option in manifest.
Related
I have three activities namely A, B, and C. A is launcher activity while B and C are singleInstance activities. The sequence of movement between activities I want is A(press button) -> B and then B <-> C (i.e, B and C keeps alternating as many times as they want). When in B, pressing navigation up button takes me to activity A.
Now the problem arises. I am in A now and want to go to B(by pressing button) like I did during start of my App(i.e, I want it to work as if the app has been launched). But it tries to go to activity B and suddenly with a flash it returns back to A. The funny part now is that when I try again(i.e in activity A I press the button again) it goes to B without any problem and the flow works as expected i.e, the flow of activities are same as it was on launch time.
What I want finally?
Once I reach launcher activity which is activity A, I want the app to behave as if it was just launched. i.e all activity including singleInstance activities should be cleared from the stack.
I have tried this in onSupportNavigationUp() method in activity B:
Intent mainIntent = new Intent(B.this, A.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mainIntent);
B.this.finish();
I even tried adding Intent.FLAG_ACTIVITY_CLEAR_TOP also.
But this doesn't help. The problem remains unsolved.
Any kind of suggestion will be greatly appreciated.
I have an app that runs a service (audio player) in the background and displays an ongoing notification while it is run. When the user clicks the notification, I'd like to open the player activity without it showing up in the recents list and without bringing the rest of the app to front, so when the user presses the back button they go directly to the app they were using previously, regardless of everything. I tried three different ways and none of them worked well.
Start an intent with FLAG_ACTIVITY_NEW_TASK. If there is an existing task, it's brought to the front. When the user presses the back button, the player activity finishes and reveals the previous activity in the said task instead of going back to the app that had been in foreground the moment the notification was clicked.
Start an intent with the following flags: FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_MULTIPLE_TASK | FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS. The existing task isn't brought to the front and the back button works as expected. However, after this activity finishes, the app disappears from the recents list completely. The only way to get it back is to start it from the launcher icon which brings that existing task to the foreground as if opened from recents.
Start an intent with the following flags: FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_MULTIPLE_TASK | FLAG_ACTIVITY_NO_HISTORY. The activity starts in a new task, but said task replaces the main one in the recents list. Adding FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS makes it behave exactly as in previous case.
Is there any proper way to do such navigation, or should I give up trying and just make it bring an entire existing task to the front with the player activity launched on top of it, as in case 1?
Is it convenience for u to declare the launchMode to "singleInstance" inside this Activity Tag in the Manifest File? I think that may help u.but i am not sure.U can try
Maybe you can try launchmode as a tag for your notificationActivity in your manifest;
android:launchMode="singleInstance"
you should try new document-centric API:
Adding Tasks to the Recents Screen
I actually figured out the proper way to do this: add the taskAffinity attribute to your <activity> in the manifest. This way, it won't interfere at all with how the rest of the app displays in recents, exactly what I was trying to achieve.
In my application there are 4 activities.
1-LoginOrSignupActivity(Main)
2-SignupActivity
3-LoginActivity
4-MainFeedActivity
Problem is that when i am logged in and i am in MainFeedActivity if i press back but it takes me back to loginOrSignupActivity. I want my application to go on onPause State when back button is pressed within MainFeedActivity.
Flow is like LoginOrSignupActivity->LoginActivity->MainFeedActivity
other flow is like LoginOrSignupActivity->SignupActivity->MainFeedActivity
i used this Flag but still it is not working
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
If you first go to LoginOrSignup, then LoginActivity, and then open MainActivity, you need to call finish() on your passed activities when you move on. Or else the activity will remain in the back stack. Alternatively you can set a flag on your activity in the manifest.
Relevant reading: http://developer.android.com/guide/components/tasks-and-back-stack.html
For those cases where you don't want the user to be able to return to an activity, set the element's finishOnTaskLaunch to "true" (see Clearing the stack).
I have a really annoying problem and I keep playing with all the flags/launchMode combinations without any luck. Hope somebody can help me :-)
In my android application I have to activities, A and B.
for the flow
A -> B -> press Home -> returns to app
Activity B is displayed when the user returns which is just what I expect.
Now, I have a notification that would open Activity A no matter what the activities stack was before.
For that purpose, when invoking the intent from a notification I use
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
So now for the flow
A -> B -> press Home -> Receive a notification -> open it
Activity A is displayed, which is also what I expect.
The problem is that from this point forward (until I kill the application), if I return the first flow, activity A is always displayed even if activity B was top of the stack when I pressed Home...
I guess that since I gave Activity A the flag Intent.FLAG_ACTIVITY_CLEAR_TOP, it keeps clearing the stack every time the application returns.
Is there any way to undo this flag setting or something, and keep both flows working as I expect?
Thanks,
Maxim
In my situation, there is one case in which I need to make sure the activity only runs one at a time.
I found if I set the LauchMode of the activity, I can reach the single instance aim, but it won't update the view of the activity.
This activity is launched by startActivityForResult, and we send the URI with the intent to the activity.
Let's discuss with this certain case:
gallery - lauch this activity with imageA.
camera - lauch this activity with imageB.
My request is not to destroy the old activity, but the activity that just received the new intent infomation should refresh the view.
I found a new method, onNewIntent.
This method can refresh the intent before resume. I will try it.
You can have an Activity with a manifest attribute of singleInstance.
As soon as the activity is relaunched , onResume gets called. You can update the view with the new image and invalidate the older View.
<activity ..
android:launchMode= "singleInstance" />
In your manifest, use android:launchMode="singleTask" instead of android:launchMode="singleInstance". Using singleInstance only returns to the existing instance if it is at the top of the stack. singleTask, on the other hand, return to the existing instance of the activity, as it is always at the root of the task.
Then, when your instance is launched, override onNewIntent to update your UI according to the new intent.
Refer to the Andorid documentation for details.
Be careful when using android:launchMode="singleInstance" in multiple activities application.
Here is my test on Pixel 4XL (Android 10)
Example, we have 3 activities (without declare taskAffinity)
A (entry activity)
B (start from A)
C (singleInstance, start from B)
If we start A->B->C. Now A,B in a task and C in a different task.
From C, press back -> will see B, press back -> will see A, press back -> app close. Everything working efine.
However, if we start A->B->C then press Home -> app go to to background.
Now there is 2 situations
User open app again from Recent app.
User will see C screen. Press back -> app will close (don't go to B)
User open app again by click on app icon
User will see B screen. Press back -> will see A, press back -> app will close (don't go to C)
The behaviour after open application is different between both case. But in both case, the application is always available in Recent app and you never able to remove application from Recent app (after remove it will appear again). Only able to remove it by Force stop in Setting.
If you run adb shell dumpsys activity activities, you will see that activities still exist in Stack, but you can not go back to it.
Another case
If we start A->B->C->A. Now A,B,A in a task and C in a different task (A is current visible to user).
From A, press back -> will see B (not C), press back-> will see A, press back -> will see C, press back -> app will close. (at least here we can go back to C)
Now if, we start A->B->C->A then press Home. In both case, user open app again by Recent or click on app icon, user will see A screen. Press back -> will see B, press back -> will see A, press back -> app will close (never see C screen)
Another note
The above example, although application run on 2 tasks but in Recent apps, it only show 1 task.
If you set the taskAffinity, in the Recent apps, you will see 2 tasks. In that case, you can choose the task which contain activity that you want to display but when you press back, it just go back to the activity in same task or close app (not able to go back to activity in another task).
Also, if you click on app icon to open app again, we have the same behavior like above demo.
I think singleTask and singleTop is less complex than singleInstance, so if it is enough for solve your problem, consider to use them instead of singleInstance