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)
Related
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.
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.
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
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
I have two activities A and B. What I want is to show activity A as an intro, the user will be unable to navigate back to it. Is there some flag I can set to activity A to do this? Can I block the back button for one activity only? Activity A is of course my main activity which automatically starts activity B after some "hard work".
Best regards.
you do not need to block the back button, but just call finish() on your A activity after firing an intent to start B. Back button pops the previous activity from activity stack and it won't be able to pop A if it is already finished.
For this you don't need to block the Back button. Simply, start the second Activity and quit the first one. And now if user presses the Back, they will be taken to the Android home screen not on your apps home screen.
Updates: By the way if you want to intercept the Back button for any reason, simply override the onBackPressed() method of Activity class. See this for details.
Never override the functionality of a hardware button.
You should call finish() in Activity A right after starting Activity B (calling the Intent).
it works but the application terminates and i'm redirected to android's applications screen. I would like to stay in activity B if back button is pressed, i don't want to exit the app. here's is what i got :
public void startProgram(Context context){
Intent intent = new Intent(context, ActivityB.class);
startActivity(intent);
finish();
}