您的位置:首页 > 移动开发 > Android开发

Android:Back button 和 Action Bar UP button 不完全解析

2015-07-21 21:41 701 查看
工作中遇到了一个问题,为什么Back button 跟 Up button 表现的不一致。 因此就花了很长时间去分析(小白不易啊 :(  )

为什么Back button 跟 Up button 表现的不一致, 具体来说我遇到的问题是从一个activity到另一个fragment再到一个fragment, 点击Back button能一个个返回, 而点击Up button却直接回到了activity (这些fragment应该是在一个SubActivity里面,反正就是这个意思)。 当时我思考的是Back button在下面,所以会一个个返回, 而Up button在上面,两个fragment在一个Activity里面, 所以返回上个activity。
不能说这不对, 但是还是说服力。 因此我就百度了一下。

Bing里输入 what the difference between Back button and Up button?  第一个就是答案(在这里我要吐槽一下百度, 搜的时候页面基本找不到相关内容。所以建议大家也要多尝试其他的工具) 在 http://stackoverflow.com/questions/27282112/difference-between-actionbar-back-button-and-android-back-button 里面已经提到了不同之处 

“The ActionBar "back" button is actually an "Up" button and it should take you to higher level on your app's navigation hierarchy. The back button takes you to the last place you were looking at.

Another great tip to better undertand this is that the "Up" button should always take you to a place on your app, while the back button might take you to another app.”   但是感觉写的不太清楚, 所幸他给了一个官方链接 http://developer.android.com/design/patterns/navigation.html 在这里面已经讲的很清楚了  “In
these cases, following each link does create history, causing the Back button to step through each previously viewed screen. Up should continue to bypass these related screens and navigate to the most recently viewed container screen.”



(一图胜千言) 我们已经很明显看出了Up button跟 Back Button的区别, Up button 会 “   bypass
these related screens and navigate to the most recently viewed container screen.” 而 Back button 会 “step through each previously viewed screen.” 这完美解决了我的问题,当然它们还有其它不同,
比如Back button能回退到其他app而Up button只能退到当前app 等等,如果有兴趣了请继续看文档。当然如果就到这我也没必要写这篇文章了。Talk is cheap, show me code

当我在看framework里面的activity.java 时发现有个函数onMenuItemSelected(int featureId, MenuItem item) 它里面会调用 onOptionsItemSelected(MenuItem item)  onMenuItemSelected
会判断option 跟context上下文。 当是optional时, 会调用onOptionsItemSelected, 该函数源码如下:

public boolean onMenuItemSelected(int featureId, MenuItem item) {
CharSequence titleCondensed = item.getTitleCondensed();

switch (featureId) {
case Window.FEATURE_OPTIONS_PANEL:
// Put event logging here so it gets called even if subclass
// doesn't call through to superclass's implmeentation of each
// of these methods below
if(titleCondensed != null) {
EventLog.writeEvent(50000, 0, titleCondensed.toString());
}
if (onOptionsItemSelected(item)) {
return true;
}
if (mFragments.dispatchOptionsItemSelected(item)) {
return true;
}
if (item.getItemId() == android.R.id.home && mActionBar != null &&
(mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
if (mParent == null) {
return onNavigateUp();
} else {
return mParent.onNavigateUpFromChild(this);
}
}
return false;

case Window.FEATURE_CONTEXT_MENU:
if(titleCondensed != null) {
EventLog.writeEvent(50000, 1, titleCondensed.toString());
}
if (onContextItemSelected(item)) {
return true;
}
return mFragments.dispatchContextItemSelected(item);

default:
return false;
}
}


自己写了一个小app 打了个Log, 代码大致如下(代码在公司,没办法全部贴出):

@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d("debug", "onOptionsItemSelected");
return super.onOptionsItemSelected(item);
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
Log.d("debug", "onMenuItemSelected");
return super.onMenuItemSelected(featureId, item);
}
 用abd logcat -s debug 看一下, 每当按下Up button 会先调用onMenuItemSelected ,然后调用onOptionsItemSelected
。(当然首先应在onCreate里面 setHomeButtonEnabled 跟 setDisplayHomeAsUpEnabled
设置成true) 而Back button 会调用onBackPressed。

现在总结如下:

Action Bar Up button 会返回到父activity, 而Back button会回溯栈,从屏幕历史一个个返回。

Action Bar 有两种处理返回方式, 一是重写onOptionsItemSelected
处理android.R.id.home。 二是设置parent activity, 在AndroidManifest.xml 对应的activity里面加上
android:parentActivityName="parentActivity"


而onMenuItemSelected判断有mParent时
就会默认处理返回, 从上面onMenuItemSelected的代码我们可以看出
而Back button是调用onBackPressed 当希望Up button 跟Back button行为一致时 可以这样:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
default:
return super.onOptionsItemSelected(item);
}
}


PS: 基本算是第一次写正式的博客, 写的很乱。 还请大家多多指正, 欢迎交流 :)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  androidaction barUp