I am implementing up navigation in my action bar with the 'back' arrow.
I have been doing this as follows:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
and declaring the parentactivity in the manifest manually. The problem is, in my app for some activities there maybe many routes to the destination activity.
I have been researching this http://developer.android.com/guide/topics/ui/actionbar.html#Home
and to quote
Or, override getSupportParentActivityIntent() and
onCreateSupportNavigateUpTaskStack() in your activity. This is
appropriate when the parent activity may be different depending on how
the user arrived at the current screen. That is, if there are many
paths that the user could have taken to reach the current screen, the
Up button should navigate backward along the path the user actually
followed to get there.
The system calls getSupportParentActivityIntent() when the user
presses the Up button while navigating your app (within your app's own
task). If the activity that should open upon up navigation differs
depending on how the user arrived at the current location, then you
should override this method to return the Intent that starts the
appropriate parent activity.
The only problem is I don't know what to override it with to get the current parent activity. Has anyone used this previously? I'd appreciate advice
If anyone could help me on how to do this I'd greatly appreciate it, I'm not as suggested using it as a back button. I'm just trying to overwrite the up button.
Oh...for anyone who might need a more helpful answer than those provided...
here is what I have done
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()){
case android.R.id.home:
finish();
overridePendingTransition(R.animator.close_slide_in, R.animator.close_slide_out);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
so in the case of android.r.id.home that's where you put your required action. In my case I just wanted to finish the current activity
I hope that's of some use to somebody out there
According to the Android guidelines, you should not make the up button behave in different ways everytime. That is the way the back button should work.
Related
I have 3 activities:
MainActivity (start activity with grid view)
FragmentActivity (full screen image slider accessed from grid view)
InfoActivity (blank activity opened from menu in either Main or Fragment)
When I go from MainActivity to InfoActivity:
startActivity(new Intent(MainActivity.this, InfoActivity.class));
and press the "up" button I get back to main activity.
When I go from FragmentActivity to InfoActivity
startActivity(new Intent(this, InfoActivity.class));
and press the "up" I STILL get back to MainActivity.
I know it's because my MainActivity is the parent of Info.
But how do I make the "up" behave like the "back" button, so that I can go from InfoActivity to FragmentActivity?
Any help will be much appreciated!
You have to override onOptionsItemSelected in the InfoActivity class to intercept the "up" button and call onBackPressed from there. Like so:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item != null && item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
The answer from #Firoze Rakib will work but I would like to provide you better understanding of the problem and solution advised by Google.
First of all you most probably have defined parent in your AndroidManifest.xml, you should remove it since your InfoActivity can have different parents.
Secondly, there is a small difference between BACK BUTTON(which is programatically called in #Firoze answer) and UP BUTTON. First one simply destroy current activity and show activity that was previously shown, theoretically it does not have to be any of your parents. The UP BUTTON starts new instance of the parent Activity and clear the activity stack in the current task.
If this difference matters for you then you should follow instructions from Android Developers page. Take a look at second point that starts with this sentence:
Or, override getSupportParentActivityIntent() and
onCreateSupportNavigateUpTaskStack() in your activity.
how do i make the "up" behave like the "back" button...
I believe another answer has the technical details if you are determined to do this (which you should not).
The purpose of my answer is to point out that making up behave like back will confuse users by violating the Android design guidelines.
Up navigation is an alternative to using the back key with a different purpose: it gives the user a way to navigate back to the top of an app when they are "lost" deep in a hierarchy and they might have to press the back key many times to escape. In this situation some users will panic and start mashing the back key, and up nav is meant to give them an alternative.
Try this
public boolean onOptionsItemSelected(MenuItem item) {
if(item != null )
if( && item.getItemId() == android.R.id.home) {
// your code
return true;
}
return super.onOptionsItemSelected(item);
}
I have not seen a specific answer to this question so please do flag this as a duplicate if you can find the duplicate.
I am wondering how does one make the back button of the Action Bar button act as the system back button. So when an action bar is displayed and there is the application icon in the top left with the back arrow, how does one make that act as the system (hardware sometimes) back button?
Here is an example of the button I am talking about.
So in order to do this you will need to on your activitys UI setup call these.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
and then if you do not want text, like that example you need to call
getSupportActionBar().setDisplayShowTitleEnabled(false);
Then in your activity you need to create a method that looks like this.
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int theId = item.getItemId();
if (theId == android.R.id.home) {
callCleanupActivityMethod();
finish();
}
return true;
}
That is a callback listener that is immediately put on the activity when an action bar has been put on the activity. That function then grabs the id of the item that was clicked, checks to see if it was the home button, then you need to clean any objects or processes that need to be stopped then call finish() which will make the activity finish. Finally return true.
When looking at the comments you can probably see that a member suggested just calling onBackPressed, that may work for some applications but not all hence the call to finish. I do recommend trying it first though.
Cheers
On my apps home page I have an option to add items to a ListView using adapter.add(string). I also have another Activity that my app goes to using startActivity(intent). When I am in that second Activity and I press the back button all of the data that was added to the list is still there, however when I press the icon at the top left all the data is gone. Is there a way to make it so that the button for the icon preserves the data in my list. I think I should note that I don't want the data to be preserved when the app is closed, only when navigating through the open app.
This is my current method that handles the home button action:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
A quick solution would be to call onBackPressed() instead of NavUtils.navigateUpFromSameTask(this). This, however, isn't recommended and you should try to implement proper navigation logic. Take a look at the answers to this question as well as these tips.
I'm developing an app which has a navigation drawer. Although I don't have any previous experience with fragments I could implement navigation drawer successfully. Now my problem is, I want to have an Action Bar back button. I have tried this before for activities. But now the problem is I have a fragment which has a click event inside it. When the user clicks the button it goes to an activity which has list view inside it. Now I want to go back to the previous fragment using action bar up button. How can I do it? It's working properly when I press back button on my phone.
I have implemented onOptionsItemSelected as below in my list view activity. When I press action bar up button it goes to the login window. Not where I wanted it to go.
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
Intent upIntent = NavUtils.getParentActivityIntent(this);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
// This activity is NOT part of this app's task, so create a new task
// when navigating up, with a synthesized back stack.
TaskStackBuilder.create(this)
// Add all of this activity's parents to the back stack
.addNextIntentWithParentStack(upIntent)
// Navigate up to the closest parent
.startActivities();
} else {
// This activity is part of this app's task, so simply
// navigate up to the logical parent activity.
NavUtils.navigateUpTo(this, upIntent);
}
return true;
}
return super.onOptionsItemSelected(item);
}
Can anybody provide some help on the issue?
I think in this post: Using ActionBar Up Navigation in master/detail fragments you can find you're answer for this problem.
I think you must working with the FragmentManager instead of the Activity.
I am developing an android app. I want my app to have a home button. So I decide to use a clickable ImageView for that, which I am placing in the title bar. Whenever a user clicks on the home image, the app will navigate to the home screen: which I accomplish by having the image dispatch an intent to start the HomeActivity.
My concern is that the whole approach feels like poor design (I may be wrong). Basically all activities follow the HomeActivity. And since I don't normally call finish() on my activities (so to allow back-button navigation) would I end up in a loop? As in, say a user goes from HomeActivity to FooActivity then to BarActivity then clicks on the home image hence going to HomeActivity. I know I can rely on android to reclaim memory when it needs it. But I don't want my app to tax the system so much.
So how do android designers normally design home icons/buttons so to avoid this looping problem? Or is android aware that I am returning to HomeActivity and thus may finish the previous instances of the other activities I just walked through?
Using the App Icon for Navigation for this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}