问题描述
我用 ActionbarSherlock 并想使home键...
因此,我呼吁 setHomeButtonEnabled(真)
在我的基地活动。
I use ActionbarSherlock and would like to enable the home button ...
Therefore I call setHomeButtonEnabled(true)
in my base activity.
public class BaseFragmentActivity extends SherlockFragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_Sherlock);
super.onCreate(savedInstanceState);
getSupportActionBar().setHomeButtonEnabled(true); // Here
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: {
Intent intent = new Intent(this, HomeActivity.class);
// startActivity(intent);
// startActivityIfNeeded(intent, 0);
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
}
当我使用 startActivity(意向)
或 startActivityIfNeeded(意向,0)
的 HomeActivity
重新创建每次(活动呈现一个地图并重新创建它很讨厌)。
When I use startActivity(intent)
or startActivityIfNeeded(intent, 0)
the HomeActivity
is recreated everytime (the activity renders a map and recreating it is annoying).
- 我不想叫
完成()
自它只是把我带回到了应用层次中的一个步骤。相反,我一直想重返HomeActivity
。 - 另外这将是很好,如果行为可能在
的AndroidManifest.xml
,因为它描述的的。 - 这也可能是常识的清除backstack 当我回到
HomeActivity
。什么是对你的看法?
- I do not want to call
finish()
since it just takes me back one step in the app hierarchy. Instead I always want to return to theHomeActivity
. - Further it would be nice if the behavior could be configured in
AndroidManifest.xml
as it is described for the ActionBar and setDisplayHomeAsUpEnabled(). - It might also be common sense to clear the backstack when I return to the
HomeActivity
. What is your opinion on that?
推荐答案
在Android的文档,我发现我正在寻找:您可以设置的。第二标志 FLAG_ACTIVITY_SINGLE_TOP
避免重新启动活动的,如果组合使用之前提到的标志
In the Android documentation I found what I was searching for: You can set the flag FLAG_ACTIVITY_CLEAR_TOP
to clear the back stack. The second flag FLAG_ACTIVITY_SINGLE_TOP
avoid restarting the activity if used in combination with the flag mentioned before.
case android.R.id.home: {
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivityIfNeeded(intent, 0);
return true;
}
这样做的目的需要使用传递 startActivityIfNeeded()
。
这篇关于如何启用home键,返回到一个共同的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!