首页 技术 正文
技术 2022年11月14日
0 收藏 923 点赞 4,551 浏览 4133 个字

Navigating Up with the App Icon


  Enabling the app icon as an Up button allows the user to navigate your app based on the hierarchical relationships between screens. For instance, if screen A displays a list of items, and selecting an item leads to screen B, then screen B should include the Up button, which returns to screen A.

ActionBar官方教程(6)把图标变成一个返回到上级的按钮,同一个app间,不同app间,不同fragment间

Figure 4. The Up button in Gmail.

  Note: Up navigation is distinct from the back navigation provided by the system Back button. The Back button is used to navigate in reverse chronological order through the history of screens the user has recently worked with. It is generally based on the temporal relationships between screens, rather than the app’s hierarchy structure (which is the basis for up navigation).

  注意:向上返回与向后返回不同,前者是上下级关系,后者是同级前后关系.

  To enable the app icon as an Up button, call setDisplayHomeAsUpEnabled(). For example:

 @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_details);     ActionBar actionBar = getSupportActionBar();     actionBar.setDisplayHomeAsUpEnabled(true);     ... }

  Now the icon in the action bar appears with the Up caret (as shown in figure 4). 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.(同一个app下)

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

  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 (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 API level 7+ -->         <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.(不同app间)

  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.

  The system calls getSupportParentActivityIntent() when the user presses the Up button while navigating your app (within your app’s own task). If the activity that should open upon up navigation differs depending on how the user arrived at the current location, then you should override this method to return the Intent that starts the appropriate parent activity.

  The system calls onCreateSupportNavigateUpTaskStack() for your activity when the user presses the Up button while your activity is running in a task that does not belong to your app. Thus, you must use the TaskStackBuilder passed to this method to construct the appropriate back stack that should be synthesized when the user navigates up.

  Even if you override getSupportParentActivityIntent() to specify up navigation as the user navigates your app, you can avoid the need to implement onCreateSupportNavigateUpTaskStack() by declaring “default” parent activities in the manifest file as shown above. Then the default implementation ofonCreateSupportNavigateUpTaskStack() will synthesize a back stack based on the parent activities declared in the manifest.

3,多个fragment间如何向上返回

  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, override onSupportNavigateUp() to perform the appropriate fragment transaction—usually by popping the current fragment from the back stack by calling popBackStack().

  For more information about implementing Up navigation, read Providing Up Navigation.

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902