Handle Up navigation from Action Bar like Back navigation. How?

為{幸葍}努か 提交于 2019-12-14 03:13:52

问题


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:

  1. Using onOptionsItemSelected() method like nitzanj's answer above.
  2. Using onBackPressed() and finish() method in your Activity like your question (not effective).

  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!