如何启用home键,返回到一个共同的活动? [英] How to enable the home button to return to a common activity?

查看:207
本文介绍了如何启用home键,返回到一个共同的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 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
  • 另外这将是很好,如果行为可能在的Andr​​oidManifest.xml ,因为它描述的的动作条和setDisplayHomeAsUpEnabled()
  • 这也可能是常识的清除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 the HomeActivity.
  • 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_CLEAR_TOP 清除后退堆栈。第二标志 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;
}

这样做的目的需要使用传递<一href="http://developer.android.com/reference/android/app/Activity.html#startActivityIfNeeded%28android.content.Intent,%20int,%20android.os.Bundle%29"相对=nofollow> startActivityIfNeeded()

这篇关于如何启用home键,返回到一个共同的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆