Back stack issue with widget - android

I have a widget that can launch two different activities, Activity A and Activity B, depending on which hot spot is clicked. What I would like to have is to reset the back stack when launching each activity. The problem is the following:
I click hot spot 1 and launch Activity A
I press the Home button
I click hot spot 2 and launch Activity B
I press the back button and instead of going back to the home
screen, I go back to Activity A
The way I launch the activities from the widget is the following:
Intent intent = new Intent(getBaseContext(), ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Is there a solution to this?

Try this;
Create a global Variables that changes when each Hot spot is clicked.
Override Back Button
inside the Back Button method, create a switch e.g.
CASE 1: // go back to home menu etc.

I have solved the problem by adding the Intent.FLAG_ACTIVITY_CLEAR_TASK to both activities (it was only used in once of them). That fixed the problem easily.

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.

Android: Make tab launch new activity

I have a homescreen in my app that doesn't have any tabs. It just has a series of buttons. Clicking a button launches the new activity that contains a tabbar at the top. This functions normally. I can click through all the tabs just fine. What I'd like to do though is add another tab that doesn't really have content but instead, when clicked, will take the user back to the homescreen. Is this possible, and if so how would I go about doing this?
What about just closing the "tabbar at the top"- Activity with finish(). Your homescreen is right under if you did not finish() it. This is IMHO the most basic way to navigate in Android.
Another way is from your "tabbar at the top"- Activity you could do this
Intent intent = new Intent(this,homescreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This should kill your "tabbar at the top"- Activity and start homescreen activity if it's not started. If it's started it will pop up
Check this How do you use Intent.FLAG_ACTIVITY_CLEAR_TOP

Home button within app

I want to create a "home" button that will bring the user back to the "Home" in my app from whatever activity they are in at the time. I have overridden the action bar so I won't be able to use one. My problem is that when
Home → A → B
I want to press the home button to return to Home. Originally I had the home button simply finish() the activity but in this case that will return me to A. Is there a way to do what I'm attempting?
Just to clarify this is an ImageButton on screen, not the hardware home button on the device.
You can use the Intent-Flag FLAG_ACTIVITY_CLEAR_TOP
If you don't have many Activities then you can simply create an onClick in each that is something like
Intent intent = new Intent(CurrentActivity.this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this flag will take you back to the "Home" Activity and clear all others off of the stack
If you have many Activities then it may be easier to create a base Activity from which to extend all others and include the home button functionality inside that base Activity
You can simply have your Image Button create an intent to start your home screen activity, assuming this is what you were looking for. within the OnClick of the button, generate an intent, and startActivity(intent)

Using FLAG_ACTIVITY_CLEAR_TOP to instatiate an activity that was already ended with finish();

I have a login screen, which on successfully login, is taken to LandingPage, and from LandingPage, I navigate to LastActivity.
My navigation need me to come back to LandingPage if I press backbutton on LastActivity. So i did not use finish() while firing intenet to start LastActivity.
Clicking logOut button on LastActivity should take me to LoginPage, and it does, but if I press back button on LoginPage, it takes me back to LandingPage. Which is not desired behavior.
This is code I used to navigate to activity -
Intent intent = new Intent(context, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(intent);
am I using this flag wrong?
Edit -
flow of activity is -
LoginActivity -> LandingPage -> lastActivity
on logging out from last activity, it should go to loginActivity.
What you have is this (if I understand correctly):
LandingPage -> LastActivity -> LoginPage
FLAG_ACTIVITY_CLEAR_TOP is helpful if you want to go back to a previous activity and clear the top of the back stack. However, what you're doing is accidentally allowing yourself to go back to a point at the bottom of your back stack.
Edit: Have you attempted to use FLAG_ACTIVITY_CLEAR_TASK when logging out? This should clear all other activities from the stack. Note this should be used in conjunction with the FLAG_ACTIVITY_NEW_TASK flag.

Android reuse activity from activity stack and clear it

I have application with main activity and some more.
On each other activity there is the logo of the application.
When user presses the logo button, I want to get back to the main activity.
I do not want to create new intent, since activity aready on the activity stack.
How can I get it - use the one on the stack?
How can I clear the whole activity stack, so back button will actually exit from the application instead of getting back to the previous activity?
Yoav
I do not want to create new intent, since activity aready on the activity stack.
If you start an activity (via intents or any other way) which was already started and is on the stack , then Android just takes that same instance of the activity and places it on top of the stack. A new instance is not created. Ofcourse this happens if you did not manually kill the activity (by calling finish() in it).
How can I clear the whole activity stack, so back button will actually exit from the application instead of getting back to the previous activity?
Its not recommended to override the back button to quit the application in every activity(Unless your app has strong reasons to do so). generally the app should let the user go back to the previous activity when he presses the back button (which is what a user might be expecting).
If you still would like to quit with the back button then you can override the back button function and launch the intent that leads to the home screen:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I faced a somewhat similar problem. The following link might be helpful for you:
clearing stack of activities with just one press
Its very simple.
Don't call finish() on Home/Main activity.
For Ex: Say you have 4 activities.. If your requirement is like this .. Act1-->Act2-->Act3-->Act4-->Act1 . So, don't call finish() on Act1. But call finish() on Act2, Act3 while you are going to other activity. So when you click on logo in Act4, just call finish(). So, automatically you will come back to Act1 which is your Main activity.
If you have logo in Act2, Act3 also then call finish() on click of logo to go back to Main. But remember to call finish() on Act2 while you are going from Act2 to Act3

Categories

Resources