0

我使用ActionbarSherlock并想启用主页按钮......
因此我调用setHomeButtonEnabled(true)了我的基本活动。

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(intent)或每次都重新创建时(活动呈现地图并重新创建它很烦人)startActivityIfNeeded(intent, 0)HomeActivity

4

2 回答 2

0

我会考虑为该活动使用单实例启动模式。

AndroidManifest.xml

<activity  android:launchMode="singleInstance">
...
</activity>

参考

只允许此活动的一个实例一直在运行。这个活动得到一个独特的任务,只有它自己在里面运行;如果它再次以相同的 Intent 启动,则该任务将被提前并调用其 Activity.onNewIntent() 方法。如果此活动尝试启动新活动,则该新活动将在单独的任务中启动。有关任务的更多详细信息,请参阅任务和回栈文档。

于 2013-09-06T12:37:54.327 回答
0

在 Android 文档中,我找到了我要搜索的内容:您可以设置标志FLAG_ACTIVITY_CLEAR_TOP以清除后退堆栈。如果与前面提到的标志结合使用,第二个标志可FLAG_ACTIVITY_SINGLE_TOP避免重新启动活动。

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()

于 2013-09-06T12:55:01.317 回答