I have a simple activity with an image view and a simple menuItem which is used for came back in the previus activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I just realized that if I press the back button for coming back in the previous activity, android do it really fast, but pressing the menu item above, it waste a second. why?
The default way of doing a return back is
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
return super.onOptionsItemSelected(item);
}
}
By calling finish() you schedule the current activity for destruction, i.e. to call onDestroy(). This does some clean-up and thus produces an overhead and you experience therewith some latency. So, if you don't really have to use finish() there (e.g. to close some dialogs or cursors), just omit it and use the provided default solution.
Hope this helps!
Related
Pls I want to show an alert dialog after the up navigation arrow(up button) is clicked. But I don't want it to go back if certain conditions are not met.
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case android.R.id.home:
if(!(editGroceryName.equals(oldGroceryName))){
showWarningAlert();
return false;
} else{return true;}
default:
return super.onOptionsItemSelected(item);
}
}
I added the alert dialog code within android.R.id.home on the onOptionsItemSelected() method but the dialog shows briefly then closes with the hosting activity then navigates back to MainActivity which was initially on back stack. Pls here is my code from SecondActivity
Pls what can I add to the code so it doesn't go back or so that only the alert dialog positive OK button can close the activity
Thanks, This fixed it for me. Returning true means you're handling things, so fragment doesn't close. While false tells the system you have no objection and that it should proceed in closing the fragment/Activity.
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case android.R.id.home:
if(!(editGroceryName.equals(oldGroceryName))){ // Name is different
showExitWarningAlert();
return true; // Handled - Fragment do not close
} else{return false;}
default: return super.onOptionsItemSelected(item);
}
}
I'm doing an app for Android. In the DetailActivity, there is the ActionBar with the Up Button. When I press it, it would return in the previous activity. But the DetailActivity can be launched from different activity.
Instead of using NavUtils.navigateUpTo(this, upIntent) , you can just use finish() or onBackPressed(). This would finish the activity, and go back to the previous one. So handling of the Up button being pressed would look like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
I'm using
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
backPressed();
break;
}
return super.onOptionsItemSelected(item);
}
backPressed() creates a dialog, and should wait for button click. If 'ok' is pressed, finish the activity, else, don't finish the activity.
At the moment the dialog is created, but then the activity finishes and the dialog disappears.
Is it possible to achieve that?
Unless you return true from onOptionsItemSelected(), you're basically telling Android "I didn't handle this, do whatever you are supposed to do with it".
In this case, since you want to intercept the home action, just return true after calling backPressed().
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
backPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
Activity A ===click button===> Activity B
When press back button, Activity A is not recreated.
When press home as up button, Activity A is recreated.
So I save state when A.onSaveInstanceState(Bundle outState)
, and use state when A.onRestoreInstanceState(Bundle savedInstanceState).
Saving and Using works fine (except home as up button)
.
However,
When pressed home as up button,
system recreate Activity A, and savedInstanceState is gone.
How can I use Saved Instance State?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// I do not want this...
// Home as up button is to navigate to Home-Activity not previous acitivity
super.onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
In the onCreate() enable the home button.
#Override
public void onCreate(Bundle savedInstanceState) {
...
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
In the onOptionItemSelected() method do this.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
This should enable Up navigation. If you want the parent activity to be restored with a savedInstanceState. You should set launchMode="singleTop" in the parent activity in Manifest file.
For more info check out http://developer.android.com/: Providing Up Navigation
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
I used finish() insteed of NavUtils;
As #Joachim mentioned in a comment, there is a difference between R.id.home and android.R.id.home. In my case, I have been using R.id.home, which did not work, and android.R.id.home did work.
I am seeing the up caret but nothing happens when I click it. Where do i tell it to goto home and set which activity is Home?
You need the line:
getActionBar().setDisplayHomeAsUpEnabled(true);
put that in onCreate();
That will make it clickable. The you will need to handle the click event. This is done by overriding onOptionsItemSelected()
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
default:
return super.onOptionsItemSelected(item);
}
}
If you want to use NavUtils, (which I have seen used by the ADT plugin when it makes Activities) you can replace change the implementation like so:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Don't forget to
import android.support.v4.app.NavUtils;
Also, you can add this to your Activity node:
<activity
android:name=".SomeActivity"
android:label="#string/activity_label" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mypackage.MainActivity" />
</activity>
NavUtils is usually the better approach. For more information, see the official Android Guidelines.
In your onCreate you have:
getActionBar().setDisplayHomeAsUpEnabled(true);
And then use:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Updated to address comments below, which correctly point out my original answer was not correct for all cases. The "home/up" button have are supposed to take you the Home Activity or the Parent Activity, respectively. From the ActionBar documents --
By default, your application icon appears in the action bar on the
left side. If you'd like, you can enable the icon to behave as an
action item. In response to user action on the icon, your application
should do one of two things:
Go to the application "home" activity, or
Navigate "up" the application's structural hierarchy
Implementing "Home" is straightforward. Launch an intent to the home activity (usually your main launch activity) of your app, clearing the backstack to the existing instance if one exists--
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
"up" is similar, except you should use
getActionBar().setDisplayHomeAsUpEnabled(true);
in your onCreate to get the icon to show as "up" instead of "home", and launch an instance of the parent activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent parentActivityIntent = new Intent(this, MyParentActivity.class);
parentActivityIntent.addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(parentActivityIntent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
In both case, for Activities that can be launched from outside your application, you should add FLAG_ACTIVITY_NEW_TASK to launch into a new task instead of the callers task.
"Up" can get more complicated if your Activity can be launched from another app, and want to build a back stack for your application to get back to the root. See for this guide for more info.
My original answer, shown below, is considered bad form in the guide because it it treats the home/up button as 'back'. It will take you to the activity that invoked this activity, which is not necessarily the home or parent. It works well as 'up' for apps that have strict tree hierarchy of activities because an given activity was always launched by its parent.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}