问题
How can I handle a Up navigation like a Back navigation?
Is there a method for the Up navigation like for the Back navigation (onBackPressed()
)?
Or can I programm the finish()
method, when I press the Up navigation?
回答1:
Assuming by 'up navigation' you mean a tap on the app icon, you need to override onOptionsItemSelected()
in your activity and handle it like so:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// 'home' is the id for the icon click in the action bar (i.e. up/back).
if (item.getItemId() == android.R.id.home) {
// do your thing and return true
return true;
}
return super.onOptionsItemSelected(item);
}
回答2:
There was many ways to do that:
- Using
onOptionsItemSelected()
method like nitzanj's answer above. Using
onBackPressed()
andfinish()
method in yourActivity
like your question (not effective).Using
setDisplayHomeAsUpEnabled()
method.
How to use setDisplayHomeAsUpEnabled()
method?
Firstly, in your onCreate
method, add:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
// use getActionBar() if API is higher than 11
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
...
}
Now the icon in the action bar appears with the Up caret (as shown in figure). However, it won't do anything by default. To specify the activity to open when the user presses Up button, you have two options:
1. Specify the parent activity in the manifest file.
This is the best option when the parent activity is always the same. By declaring in the manifest which activity is the parent, the action bar automatically performs the correct action when the user presses the Up button.
Beginning in Android 4.1 (API level 16), you can declare the parent with the parentActivityName
attribute in the <activity>
element.
To support older devices with the support library, also include a <meta-data>
element that specifies the parent activity as the value for android.support.PARENT_ACTIVITY
. For example:
<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
android:name="com.example.myfirstapp.MainActivity" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
Once the parent activity is specified in the manifest like this and you enable the Up button with setDisplayHomeAsUpEnabled()
, your work is done and the action bar properly navigates up.
2. 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. Read more
Source:
Providing Up Navigation
Navigating Up with the App Icon
来源:https://stackoverflow.com/questions/27675492/handle-up-navigation-from-action-bar-like-back-navigation-how