Android navigation clear whole backstack - android

I currently have an Android application when in the process of browsing a hierarchy of objects creates a back stack like this.
BrowseActivity(Starting Instance) -> BrowseActivity(Instance B) -> BrowseActivity(Instance C) -> ViewObjectActivity
There is a menu button in the view object activity that I would like to have take the user back to BrowseActivity(Starting Instance) and destroy the rest of the browse activities.
Using Intent.FLAG_ACTIVITY_CLEAR_TOP only destroys the ViewObjectActivity and leaves the rest in the back stack.
Setting the BrowseActivity to "singleTop" in the manifest breaks the ability to create another instance for further navigation.
Any way to accomplish this that I am probably overlooking?

If you want the menu option to take the user back to the first activity, do just that - take the user back to the first activity.
Have your third activity exit with a specific result that tells the second activity to exit as well. You can extend this to how many activities you want - as long as all the activities except the first one exit, you're good.

You need to use this flag in a pair. The following has always worked for me
Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent.FLAG_ACTIVITY_NEW_TASK

Related

Keep one activity in the bottom of the backstack

I want to learn the proper way to manage the activity back stack with regards to my issue. Most of the time when a person uses my app, I want to keep an activity in the bottom of the stack, let's call this Activity A. This would be their "Home" activity. I have a navigation view which can take the user to a bunch of other activities, but I want to manage what displays when they tap back. I want Activity A to always be the last activity in the stack, so the stack can look like A -> B -> C-> D, and when the user is on Activity D and they want to go to Activity E, I want the stack to look like A -> E when they press it.
A possible solution I have found is by clearing all the activities in the current stack, launching Activity A, and sending an intent for Activity E in the intent I launch Activity A with, then that will just check it's intent extras and if it finds an intent in the extras it would just launch that intent. This results in the stack looking the way I want, Activity A -> Activity E. I just want to know if there is a better or simpler way.
I have tinkered with the activity properties in my manifest, but it seems like I can't do exactly what I would like to with those.
Any help would be appreciated :)
Lets assume you want to keep activity A in the back stack so that whenever a user presses back, you want to show activity A on top. Lets say you go to B from A and then C from B. So whenever you go from any activity(other than A) to any other activity just call finish() from the calling activity, this will remove the stack entry of the corresponding activity, ensuring that only activity is there in the back stack.

Activity back tracking in Activity stack - Android

I am having two activities, Activity A and activity B.
Activity A starts activity B.
So, the activity stack after some interactions will look like A -> B -> A -> B.
The problem:
I need to go the first activity A in the stack from activity B (last B in the stack). I am using FLAG_CLEAR_TOP as well as Intent.FLAG_ACTIVITY_NEW_TASK for achieving the same.
Right now, the activity A (stack pos 3) will be shown from activity B, but when I press back button, the activity B will be shown again (since activity B (stack pos 2) is already there in the stack).
How do I overcome this issue?
PS: I tried using launchMode singleInstance and singleTask for activities A and B, but that solution doesn't work for my app.
Thanks in advance.
Add finish (); after switching activites like
Intent intent = new Intent (activity_a.this, activity_b.class);
startActivity(intent);
finish ();
Avoid creating multiple instances of activities if possible. Android isn't designed to allow you to identify (and return to) a specific instance of an Activity in the stack, if you have multiple instances of the same Activity in the stack.
Depending on your application, there are different ways to solve the problem.
One way that may work for you is to use <activity-alias>, which allows you to reuse an existing Activity implementation by another name.
Another way would be to create another Activity class, that just inherits from the original, so that you have 2 classes with exactly the same code, but with different names.
The best way is to rearchitect your application so that you only ever have one instance of each Activity alive at any given time. You can do this by rearranging the task stack using Intent.FLAG_ACTIVITY_REORDER_TO_FRONT.

android, starting and exiting activities

I have not really understood the handling of activities and the stack.
I have 3 activities, A - a splashcreen, B- a menu and C another Activity. I start the splash and exits it after a while when the menu is started.
In code I handle them all like this:
startActivity(new Intent(this, ContactInfoMenu.class));
finish();
Now, if I start the app and goes A-B-C, when I hit "Back" in C screen I jump back to B-the menu. Another "Back" exits the application, just like I want.
BUT .. if I go A-B-C-B-C - the "Back" button in C screen exits the whole app instead of getting me back to the B screen?
Why is that? It does like that in all my "subscreens", I can only enter them once, if I enter them a second time the "Back" button exits the app. And I have not tried to catch the "Back" action anywhere? Shouldn't I always call "finish()" when I start a new activity?
Regards
Finish is good for leaving the current activity and going back to the previous one. Otherwise, try to avoid calling finish() if you can help it.
There are a set of flags that you can pass when you start an activity that do a better job of determining how that activity behaves on the stack. These include:
FLAG_ACTIVITY_NO_HISTORY - your activity will not remain on the stack after another activity covers it.
FLAG_ACTIVITY_CLEAR_TOP - a good way to pop off a bunch of activities when you need to "go back" to a certain activity.
Many of these flags can be set in the manifest. Reading up on them will give you a better idea about "The Android Way".
Basically, You don't need to call finish() every time you go to another activity. If system is low on memory it will close your activity instance by itself.
finish() is more often used when yor are inserting some information in one page and then moving on to some other page. In that case, you might need to fininsh your first activity.
But in case where you need to shuffle between views, you must not use a finish() function, because it will cause the whole application to finish.
Try using back button of your own in your views to shift between activities, where you can move to any other activity of your application or even to the Main Screen.

Launcher not returning to the last-used activity in my application

I'm using a custom Launcher application with Widgets that are used to launch (and provide status updates) for an application.
The application consists of a few Activities - let's call them A, B and C for simplicity.
A is the launched Activity. The user proceeds from A to B and then to C (never in any other order).
At any time the user can press the 'Home' button on the remote control and return to the launcher application.
If the user then presses the 'Back' button on the remote control they always return to the Activity they were last using (A, B, or C).
However, if they click on the widget (rather than pressing back) the Activity that is brought to the front seems inconsistent!
So, here is an example of what happens
From (custom) launcher, use widget to launch application
Activity A appears
User presses a button that launches Activity B
Activity B appears
User presses 'Home'
Launcher appears
From (custom) launcher, use widget to launch application
Activity A appears NOT B
Sometimes I get Activity B instead of Activity A - but I'm not sure under what circumstances. I want to get the Activity at the top of the stack to be displayed and never any other Activity (Activity B in the example above).
I've read Google's documentation about the Activity flags ("single-task", "single-instance", etc...) but nothing seemed to jump out as the solution to my problem.
In my case, Activities A, B, C only make sense when run in that order - and it never makes sense to see a previous activity unless you explicitly go back to it.
I'm not sure if the problem is the way the widget is launching the application or whether there is something I need to specify in my manifest or something else.
Any ideas?
Thanks,
Dan
Isn't that what's supposed to happen? Isn't your widget launching activity A? Would you expect it to launch activity B if you are asking it to launch activity A?
(Althought you say that you get B launched sometimes. Isn't this related to the app being forced out of the memory?)
I'm sorry, but this is not an answer, but rather some additional information related to that same question.
I have found a pattern for Activities when relaunched after home button is pressed.
See my question for all my research:
Android Activity Stack is not working as stated in the docs - last activity in task stack not shown
Hope this can be of help...
I have been reading that the suggested fix for this strange behavior is the use of a "Dispatcher" class as Main that will launch the activity based on the application state...or you can also keep track of all the activities opened that need to be restored...but this can become really cumbersome when having a complex UI application design.

How do you skip parts of an Activity stack when returning results in Android?

I'm making an app which has a flow roughly as below:
User starts on the main screen with an empty list, hits menu, and goes to "add item." (Activity A)
User is given a new activity which allows them to specify search criteria, then hits "go" to do a search. (Activity B)
User gets a list of results, and can click on one of them to view more details. (Activity C)
User sees details of item, and can use a menu item to save it to their list in Activity A. (Activity D)
Right now, I am having each Activity call each other Activity for results, and then it is passing the result all the way back up the stack as it returns to Activity A.
Is there a way to jump this, since all I want is for a result in Activity D to get to Activity A directly?
Note that a user should still be able to navigate backwards (using the back button) through each activity, but if they explicitly save the item in Activity D, I want it to jump straight to Activity A.
I recommend just invoking the activities (not using the *ForResult) calls, then having activity D invoke Activity A with an INTENT_ADD_ITEM with data, then have Activity A add the item.
Hope this helps...
Just so that people can benefit from what I learned later...
The key to solving this problem is using flags with Intent, in this case using FLAG_ACTIVITY_CLEAR_TOP. Other flags are useful as well in controlling the flow of your UI.
It is a bad idea to try to solve this problem by chaining startActivityForResult() through activities. It means that it's difficult to change the flow of your application.

Categories

Resources