Handle Up Navigation From Action Bar Like Back Navigation. How?
Solution 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:
@OverridepublicbooleanonOptionsItemSelected(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 truereturntrue;
}
returnsuper.onOptionsItemSelected(item);
}
Solution 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:
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
// use getActionBar() if API is higher than 11ActionBaractionBar= 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) --><activityandroid:name="com.example.myfirstapp.MainActivity"...>
...
</activity><!-- A child of the main activity --><activityandroid: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-dataandroid: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:
Post a Comment for "Handle Up Navigation From Action Bar Like Back Navigation. How?"