I have 2 activities declared as singleTask in my application. The reason for this is, with standard mode, pressing "HOME" in one activity (say A) and launching another (say B), still shows activity A.
However, a new problem arises because of this.
A -> "HOME" -> B (Result : B, Expected Result: B) -> "BACK" -> (Result:A, Expected Result: Home Screen)
Any ideas why?
One thing that doesn't make sense:
pressing "HOME" in one activity (say
A) and launching another (say B),
still shows activity A.
However, a new problem arises because
of this.
A -> "HOME" -> B (Result : B, Expected
Result: B)
If pressing home in A and launching B still shows A is your goal with two singleTasks, then how can A -> HOME -> B have an expected result B for you?
Now I'll try and be helpful: Which Activity, A or B, handles the Launcher intent?
A -> HOME -> B (Result: B) would make me guess that B is that Activity that does, and if that is the case, your being at A before pressing HOME would mean that your stack would be something like A above B (A displayed), so pressing Home and launching B would make your stack B above A (B displayed).
Then if A and B are in a stack, pressing BACK will move you down from B to A (A now being displayed). This sounds like your problem.
If you want to take them to the home screen instead of moving down the stack, you will need to override the standard back button handler of the system, and whenever you need to override a system mechanic, it means you should double check what your doing is really the right way to achieve your goal.
If you post more information I will update my answer to help you sort this out.
Related
I have an Activity A and I call Activity B. The problem is if I navigate away from B and come back to B again, after this once activity B is finished, I am redirected to the Android Home screen rather then the original activity A. I need to find a way to get back to A. Here is the complete flow:
A -> B -> (Navigate from B to some activity or home screen) -> (come back to B) -> (B finished after calling finish()) -> (Android Home screen is shown)
Expected result:
A -> B -> (Navigate from B to some activity or home screen) -> (come back to B) -> (B finished after calling finish()) -> (A should be shown)
I understand this is due to the way in which Activities are stacked and since I navigate from B the Android Home screen becomes the top of the stack and that is why A is not shown. However is there a way in which I can manipulate the top of stack to come back to A. I tried Intent flags and its combination but it did not work for me. I may have missed something if you can help or is there any other way to do it?
It seems like finish() is called for Activity A, that's why it's not displayed when you want it. First step is to check if you're accidentally calling finish() yourself.
Also keep in mind that it's possible that the system has killed (finished) Activity A. This can happen if there's big need for memory ... for example if your Activity B is super heavy (e.g. doing lots of Bitmap transformations) it could be just that.
Lastly, if possible you can specify parentActivity of B to be A, by adding this in your manifest android:parentActivityName="....activity.A". This way Android will open A when you back from B.
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.
I have this issue.
I have an actvity A that starts other activity B (by onclick - button).
In B I have one back button to come back to activity A. I press it.
Now that I'm in A, I press again button to go to B.
If I use android back button (I'm in B) I come back to A first and then to B.
But now, if i press android back button again, I don't go to previus activity of A or it exit from app. I come back to B !!!
How can I prevent this behavior ?
The back button of activity B should not start activity A, but close activity B with finish:
http://developer.android.com/reference/android/app/Activity.html#finish%28%29
Note that it is probably bad user interface design to have a "back" button on the interface. This official Android page says "Don't use labeled back buttons"
http://developer.android.com/design/patterns/pure-android.html
if you wish your custom back button to swap between the acivities A and B, you should simply launch intents, that will do.
I believe your problem is with the android back button. If you do not want go to the previous activity with this action, you can write in the manifest file under the <activity> tags for both A and B activities - android:noHistory="true". Doing this will exit from the app. as the activity stack was storing none of the activities, but such a requirement is quite confusing UI approach.
I have a problem with activities. I have a 4 activities. First is menu, second and thirth are other menus and fourth is screen with help. In every for 3 menu screens I have a button which start activity help. In help activity I have a info about application and button. I want to this button when user use it he back to last active activity. For better understanding: when I am in first menu and click help button, activity "help" is starting and when I click button "back", activity "menu1" is starting. Algorithm must be the same for other menus. Can you help me?
Normally, if you start activity Y from activity X, when activity Y finishes, the control automatically returns to activity X.
Thus, let's say you have three menu activities A, B, C and help activity D. Then, if you do A -> D, then when D finishes, you'll be back on A. If your flow is A -> B -> D, then after D finishes, you should end up back on B. Lastly, if your flow is A -> B -> C -> D, then upon finishing of D, you should come back to C. This is something Android should do for you automatically without you having to code anything special.
Or did I misunderstood your requirements?
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