How to go to first screen in Android Auto? - android

I'm trying to delete all screens from stack in android auto app with
screenManager.popToRoot().
But when I use it, it says that I have reached the 5 screen limit of the stack.
For example, if I'm doing this flow:
Screen A
Screen B
Screen C
Screen D
Screen E
When I click on a button I created on screen E, I want to go back to Screen A and remove all stack.
Someone can tell me how I can do this? Thanks!

Try to do it manually, instead of using
screenManager.popToRoot()
Use
startActivity(new Intent(currentActivity.this, firstActivity.class))
finishAffinity()
Note that finishAffinity is used to finish all activities associated with the task, including all activities in the task's back stack. When finishAffinity() is called, the task's root activity and all activities below it in the back stack are finished.

Related

Android managing backstack for Activities

So I would like to know how I can manage the back stack for activities that are launched from the NavigationDrawer. If I launch various activities via the NavigationDrawer by default Android will add them to the back stack and it would cause back button hell as user.
I imagine this should be a common problem so there must be a adequate solution.
But I need a solution to cater for the following 3 requirements.
Requirement 1)
I have 2 items in the navigation drawer (Activity1 and Activity2) which each launch different Activites. If I open the items via the navigation drawer a number of times when I press back I wish to go back to the initial starting activity and if I press back again I wish to exit the app
Requirement 2)
I launch Activity 1 from Nav then I launch Activity 2 and from within this activity I launch a new activity SubActivity. Now when I press back I would expect to be taken back to Activity 2 and then if I press back again I would expect to be taken to the initial Activity (not Activity 1), and then pressing back again would exit.
Requirement 3)
Same as above but actually the initial Activity is dynamic. So the landing Activity is defined by a user setting about what their first screen shall be.
As you can see I can not use NO_HISTORY flag because of (requirement 2)and I cant hardcode the parent of the Activities because of (requirement 3).
So other than overriding the back button is there any other way that i can manipulate the back stack ?
Thanks
Launch mode will not help. The answers lies in using TaskStackBuilder, its a very powerful api that allows you to define exactly what is to go into the backstack of the activity that you are about to launch. Here is how to use it.
Intent activityInBackstack = new Intent(this, ActivityA.class);
Intent activityToBeLaunched = new Intent(this, ActivityB.class);
TaskStackBuilder builder = TaskStackBuilder.create(this);
builder.addNextIntent(activityInBackstack);
builder.addNextIntent(activityToBeLaunched);
builder.startActivities();
So now if you are in ActivityB and you press back button you will always go to ActivityA. Pressing back on ActivityA would exit the app.

Calling movetasktoback method on an Activity on top of home screen does not bring Home screen to foreground

I have an activity with launch mode as Single Task.The activity is launched by with intent flags Intent.FLAG_ACTIVITY_NEW_TASK by a Broadcast receiver . If this activity is on top of home screen and if I call moveTasktoBack(true) from this activity, I expect the home screen to be shown however instead of home screen, Activity B from another task is brought in front. The sequence of operations is as follow.
Activity B in TASK B -> [Press Home Button] -> Home Screen -> Launch Activity A by BroadCast Receiver -> Activity A calls moveTaskToBack(true) -> Activity B in Task B comes to foreground.
I have checked the Task Affinity of Task A (with activity A) and Task B (with activity B) and they are different.
How can I make sure that in such a scenario, Home screen is shown when activity moves itself to back of stack.
Instead of moving your task to the background, just launch the HOME screen like this:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I have experienced myself problems when trying to do moveTaskToBack (true) with more Activities (from different apps) in the stack and in an automatic situation (that is, programmatically on receiving a broadcast event). I'm not sure what the exact cause is. But I solved them using the nuclear option: instead of trying to move the Activity / task to back, I just killed it using finish (). It works well, in all situations I suppose. Might slow things down a little bit so use with caution, but in my case it solved the problem. And, of course, you'd need to save all relevant state info and restore it. In my case that wasn't a big problem.

Android navigation clear whole backstack

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

Android : Activity Stacking Issue or System Launching Issue

The main/launcher activity in my app is a login page (Activity A). Once the user is authenticated, they are taken to the main area of the application, e.g. Activity B. So now the current activity stack of this task is A > B.
I then press the home button on the phone and am taken to the Android home screen. I re-launch my app via short cut key in HTC Desire Z(See Image after Space there are two Short Cuts 1 and 2), and I am taken to Activity A, instead of Activity B. Either the activity stack is now A > B > A, or there are now two separate tasks with activity stacks A > B, and A respectively. What I want is to be taken back to Activity B when I relaunch the app..
I followed this link
The above solution worked for 2.3.3 but in ICS 4.0.3 it has an issue that i am not taken to Activity B.
How do i resolve this,In ICS I am not able to see the what Intent flag system is using to launch activity when short Cut is Pressed,is this a System BUG?
Please Help
NITZ
The pattern that I tend to use for the login like this. I'll use A to mean Login and B to mean Main application.
I make B the launcher Activity, and in its onCreate() I check if a login is needed and if so, then I immediately launch Activity A. Once A is done, i finish() it, so that I'm back to B.
This way my activity stack stack never contains the Login activity, except when it's being used. ie, after a Login is done, then only B is on the stack.

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.

Categories

Resources