Use of actionbar in Android support V7 package (3) add up navigation function for actionbar

Source: Internet
Author: User

See Use of actionbar in Android support V7 package (1) import dependency Projects

In many applications, we can see the "<" icon as shown in. After clicking it, we can return to the previous page. Today, we will learn how to achieve this effect.

I. Application scenarios

There is a list in interface A. Click an item to go to interface B. Then interface B can add the up navigation function shown in

Ii. Differences from pressing the back key

The back key is used to display the previous screen in strict chronological order of user clicks.

The up navigation function has nothing to do with time, and is only related to the hierarchical relationship of the program, that is, you can decide to click up to reach the interface.

Iii. Implementation Method:

1. Call the following method on the interface for implementing the up navigation function:

Actionbar = getsupportactionbar ();

Actionbar. setdisplayhomeasupenabled (true );

After this method is called, the "<" symbol will appear on the left side of the icon on the interface, but it will not be effective when you click it.

2. Implement the up navigation function

2.1> implemented through the manifest File

This method is recommended when all parent actitvity values are the same.

When the API is greater than or equal to 16, you can directly add the parentactivityname attribute to the activity node.

When the API is <16, you also need to add the <meta-data> node

Sample Code:

<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 API level 7+ -->        <meta-data            android:name="android.support.PARENT_ACTIVITY"            android:value="com.example.myfirstapp.MainActivity" />    </activity>

After this setting, the specified activity will be returned when you click the "<" character.

2.2> code implementation

This method is applicable when multiple interfaces can reach the same interface, that is, the parent activity is different.

The getsupportparentactivityintent () and oncreatesupportnavigateuptaskstack () methods are rewritten.

When a sub-activity runs in the task stack of its own program, when the user clicks the "<" character, the system will call the getsupportparentactivityintent () method. We need to override this method to return the appropriate intent.

When a sub-activity runs on the task stack of another program, When you click the "<" character, the system will call the oncreatesupportnavigateuptaskstack () method, in this method, taskstackbuilder is used to pass the activity to be returned for it.

If you do not want to override the oncreatesupportnavigateuptaskstack () method, you can add a parent activity in the manifest file as described in method 2.1.

In addition, if the interface in the program uses fragement instead of activity, the above two methods will be invalid, and the onsupportnavigateup () method and popbackstack () method need to be rewritten.


For the English version 2.2, I have a limited understanding of the English language. I hereby attach the original English text for your reference.

  • Or, overridegetSupportParentActivityIntent()AndonCreateSupportNavigateUpTaskStack()In
    Your Activity
    .

    This is appropriate whenThe parent activity may be differentDepending on how the user arrived at the current screen. That is, if there are already paths that the user cocould have taken to reach
    Current screen,UpButton shoshould navigate backward along the path the user actually followed to get there.

    The system CILSgetSupportParentActivityIntent()When
    The user pressesUpButton while navigating your app (within your app's own task ). if the activity that shocould open upon up navigation differs depending on how the user arrived at the current location, then you shoshould override this method to return
    TheIntentThat starts the appropriate
    Parent activity.

    The system CILSonCreateSupportNavigateUpTaskStack()For
    Your activity when the user pressesUpButton while your activity is running in a task that doesNotBelong to your app. Thus, you must useTaskStackBuilderPassed
    To this method to construct the appropriate back stack that shoshould be synthesized when the user navigates up.

    Even if you overridegetSupportParentActivityIntent()To
    Specify up navigation as the user navigates your app, you can avoid the need to implementonCreateSupportNavigateUpTaskStack()By
    Declaring "default" parent activities in the manifest file as shown above. Then the default ImplementationonCreateSupportNavigateUpTaskStack()Will
    Synthesize a back Stack Based on the parent activities declared in the manifest.

NOTE: If
You 've built your app hierarchy using a series of fragments instead of multiple activities, then neither of the above options will work. Instead, to navigate up through your fragments, overrideonSupportNavigateUp()To
Perform the appropriate fragment transaction-usually by popping the current fragment from the back stack by callingpopBackStack().


Download Code:

Http://download.csdn.net/detail/hkg1pek/6018563



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.