ActionBar官方教程(6)把图标变成一个返回到上级的按钮,同一个app间,不同app间,不同fragment间
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.
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.
ActionBar官方教程(6)把图标变成一个返回到上级的按钮,同一个app间,不同app间,不同fragment间的更多相关文章
- Asp.Net MVC4.0 官方教程 入门指南之四--添加一个模型
Asp.Net MVC4.0 官方教程 入门指南之四--添加一个模型 在这一节中,你将添加用于管理数据库中电影的类.这些类是ASP.NET MVC应用程序的模型部分. 你将使用.NET Framewo ...
- Asp.Net MVC4.0 官方教程 入门指南之三--添加一个视图
Asp.Net MVC4.0 官方教程 入门指南之三--添加一个视图 在本节中,您需要修改HelloWorldController类,从而使用视图模板文件,干净优雅的封装生成返回到客户端浏览器HTML ...
- ActionBar官方教程(5)ActionBar的分裂模式(底部tab样式),隐藏标题,隐藏图标
Using split action bar Split action bar provides a separate bar at the bottom of the screen to displ ...
- ActionBar官方教程(9)ActionBar的顶部tab模式(注意,已经被弃用)
This interface is deprecated.Action bar navigation modes are deprecated and not supported by inline ...
- Android官方教程翻译(1)——创建第一个Android应用
转载请注明出处:http://blog.csdn.net/dawanganban/article/details/9822431 Building Your First App GETSTARTED ...
- ActionBar官方教程(11)自定义ActionBar的样式(含重要的样式属性表及练习示例)
Styling the Action Bar If you want to implement a visual design that represents your app's brand, th ...
- ActionBar官方教程(10)ActionBar的下拉列表模式
Adding Drop-down Navigation As another mode of navigation (or filtering) for your activity, the acti ...
- ActionBar官方教程(8)ShareActionProvider与自定义操作项提供器
Adding an Action Provider Similar to an action view, an action provider replaces an action button wi ...
- ActionBar官方教程(4)给ActionBar添加操作项及它们的事件处理
Adding Action Items The action bar provides users access to the most important action items relating ...
随机推荐
- HDU 4430 Yukari's Birthday(二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4430 题目大意:给定n个蜡烛,围绕蛋糕的中心插同心圆,从里往外分别是第1圈.第2圈....第r圈,第 ...
- 学习C++ Primer 的个人理解(十)
标准库没有给每个容器都定义成员函数来实现 查找,替换等操作.而是定义了一组泛型算法,他们可以用于不同类型的元素或多种容器类型. 迭代器令算法不依赖与容器 算法永远不会执行容器的操作 算法本身不会执行容 ...
- 带你初识Angular中MVC模型
简介 MVC是一种使用 MVC(Model View Controller 模型-视图-控制器)设计模式,该模型的理念也被许多框架所吸纳,比如,后端框架(Struts.Spring MVC等).前端框 ...
- ThinkPHP3.2 加载过程(三)
上次回顾: IS_CGI ,IS_WIN,IS_CLI,MAGIC_QUOTES_GPC干嘛用 IS_WIN 看了一下后面的代码 基本上就是为了保证在不同环境下运行时,由于有些操作系统会对文件路径大 ...
- Web前端新人笔记之CSS结构和层叠
上一篇文章介绍了如何利用CSS选择器为元素应用各种丰富的样式,每个合法的文档都会生成一个结构树,了解这一点,就能根据元素的祖先.属性.兄弟等元素穿件选择器选择元素. 本篇文章将讨论3中机制之间的关系: ...
- 要做一款APP-解放双手
对方打字或发语音,我可以选择看屏幕或者听. 我说话,能够转化为文字.不需要点击开始按钮的那种.
- jquery 100%全屏自适应宽可点击左右和焦点的自动切换幻灯片特效
http://www.divcss5.com/css-texiao/texiao717.shtml http://d.divcss5.com/divcss5/down/2014062201.zip
- WPF中实现根据拼音查找汉字
1.WPF的一个触摸屏项目,需要输入姓名,但是屏幕不支持汉字输入,使用虚拟键盘不稳定,为了解决该问题特此进行处理. 2.新建一个类转换类,里面初始化一个数组,数组包含拼音,以及拼音下的常用的汉字. 3 ...
- CSS 神器 compass
compass 官网 compass 算是我在做 前端工程师 中遇到的用的最爽的工具.当我第一次使用它之后就不能自拔.下面对 compass 做一个比较系统的介绍,也可以做一个简单的入门教程.
- 关于NRW算法(Quorum算法)
在分布式系统中,冗余数据是保证可靠性的手段,因此冗余数据的一致性维护就非常重要.一般而言,一个写操作必须要对所有的冗余数据都更新完成了,才能称为成功结束.比如一份数据在5台设备上有冗余,因为不知道读数 ...