So I have three activities A, B, and C (A>B>C). Before I startactivity(C) in B, I clear task and set new task to make C become a new root. Everything works as expected. When user clicks the back button in C, they go back to the main screen, but when the user clicks the app again, it redirects them to A. My thought is that there is nothing in the stack, so it just shows the default activity? How can fix this? so that when user clicks on the app again, then it will show C activity?
Which Activity is going to launch through clicking is defined in AndroidManifest.xml, see this official tutorial
If all you need is to stop user from going back to Activiy A and B, maybe you can just override the onBackPressed() and don't clear tasks. In this way you can still have user navigated to C when the icon is clicked.
Related
I have been writing a multiple accounts manage module. What I want to do is to lead user to Accounts list activity when signing out. The problems is when user press back button, it resumed the background activity and user still can use the app even signed out. And it doesn`t work either when I cleared account info in database and SharedPreference.
For better understanding I describe the problem again.
For example, I have three activities, A, B and C. A works as main activity with a list, B works as the settings menu activity and C works as the account list Activity.
When I navigate from A to B click "Sign Out" menu in B, then the process flow goes from B to C.
Because it asks user to choose (if has) or login an account.
Now the problem is when user press back button, it can go back from C to A rather than exit the app (go to home screen). See the screenshot blow.
Since user already signed out, I doesn`t make sense to navigate back from C to A. C should be the only visible Activity at this circumstance.
But I don`t know how to implement this, I already clear account info in local storage, so it should not be the settings problem.
So how to clear the background activity A?
Any comments will be greatly appreciated.
Maintain a flag suppose "isLoggedIn" in shared Preferences or database. In your launcher/first activity check this flag and decide whether to call login activity or direct xyz activity. And when you clear data again call your launcher/first activity with clear_top flag in intent.
try this code in Activity C
#Override
public void onBackPressed() {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startActivity(startMain);
}
I have an App with a Navigation Drawer design and some activities (I know working with Fragments would probably be easier here but that wasn't feasible).
When the user clicks a drawer item, the according Activity is started with FLAG_ACTIVITY_REORDER_TO_FRONT. The up button should always take the user back to the start activity A, so when the up button is pressed I also start the A activity with the same flag.
Consider the following (capital letters are the activities, trying to visualize the backstack here):
A
user starts B from the drawer
A -> B
user presses up button, brings A in front
B -> A
user presses back button finishing A going back to B
B
user presses back button again which exits the application, but imho the user should always see the start Activity A again before leaving the app.
How would / did you guys solve this?
EDIT: #Neil, with that is: If I have
A -> B
and from there via drawer go to C I have
A -> B -> C
If the user now presses the Up button and I just close C instead of bringing A to the top, the user will be back in B which would be wrong because C is (navigationwise) not under B but they are 'siblings'.
Try to make the launchMode of that Activity B to SingleTask in your manifest file like this :
android:lanchMode="singleTask"
Your activity will not be called again.
My application start foreground service witch keep connection to server. It's show notification with pendingIntent witch show MainActivity. When i (user) tap on application icon (on desktop or application list) it's show "task stack". I mean if was lunched MainActivity it shows it, if user go to activity B or C (or lunch some other activities) it shows it (i mean top activity from task stack). There is a problem - if user tap on notification he see again MainActivity (on the top of the stack) but i expect top of tack stack (activity B,C or other witch was lunched by user at the end).
Half solves when i set attribute for MainActivity "singleTask", now it's always root of task stack, BUT a'm loosing all activities (B, C and other wich user lunched). Solution like in Reuse Activity on Top of Stack simular, but i need only one activity at root.
Maybe my logic is wrong and i need some another way to resolve this problem. But i want to know how can i programmaticly show task stack (top activity) like application icon does?
The documentation Tasks and Back Stack describes how to handle navigation correctly.
In short, if users tap on your navigation and you take them to an Activity in your app, when they click Back they should up in your app's Activity hierarchy until they reach the Home screen. They should never go to the stack in another task. That is, if they're in your app in Activity C, and you send a notification which they click that takes them to Activity A, then clicking Back should take them to the parent of A, and not to C. If they want to go to C, they can use Recents.
On older platforms, Recents isn't available.
This is by design.
To construct the proper synthetic back stack, use TaskStackBuilder
My App starts with a splash screen loading background data from remote server. After completion of data load, An Activity(say B) launched.
This activity B (A photo Gallery of different Animals), when pressed back launches SplashScreen, so inorder to solve this, i prompt user if it really wants to exit the app, if clicked yes, exit closes.
private void exitApp(){
B.this.finish();
}
My issues comes here.
Since Activity B, when clicks on Particular Animals say DOG,
Intent intent = new Intent(B.this,C.class)
startActivity(Intent);
takes the user to Activity (C) reviews of particular Animals.
When back button is pressed on C, takes me to B, thats fine.
Since B has menu options, such as bookmark,Home.
i do Launch BOOKMARK as with
sartActivity(B.this, Bookmark.class);
Since BookMark has menu for HOME i.e Activity B.
And now when i pressed back, it prompts me to Exit but it does exit the app rather takes me to BOOKMARK.
Can i solve this issues? As i was reading the doc, i found
`finishActivityFromChild(Activity, requestCode);`
can this help me achieve?
Let say when a user is prompted to exit the application. I simply want to remove all the stack of activities.
Can it be done?
add below tag in your AndroidManifest.xml file under BookMark activity
android:excludeFromRecents="true"
and include below tag in your HOME i.e Activity B.
android:launchMode="singleTask"
and let me know if it worked for you.
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