简体   繁体   English

如何启用主页按钮以返回普通活动?

[英]How to enable the home button to return to a common activity?

I use ActionbarSherlock and would like to enable the home button ... 我使用ActionbarSherlock,并希望启用主页按钮...
Therefore I call setHomeButtonEnabled(true) in my base activity. 因此,我在基本活动中调用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);
        }
    }
}

When I use startActivity(intent) or startActivityIfNeeded(intent, 0) the HomeActivity is recreated everytime (the activity renders a map and recreating it is annoying). 当我使用startActivity(intent)startActivityIfNeeded(intent, 0)HomeActivity都会重新创建HomeActivity (活动会渲染地图,并且重新创建它很烦人)。

  • I do not want to call finish() since it just takes me back one step in the app hierarchy. 我不想调用finish()因为它仅使我回到了应用程序层次结构中的一步。 Instead I always want to return to the HomeActivity . 相反,我总是想回到HomeActivity
  • Further it would be nice if the behavior could be configured in AndroidManifest.xml as it is described for the ActionBar and setDisplayHomeAsUpEnabled() . 此外,如果可以在AndroidManifest.xml配置行为(如针对ActionBar和setDisplayHomeAsUpEnabled()所述) ,那就更好了。
  • It might also be common sense to clear the backstack when I return to the HomeActivity . 这也可能是常识清除堆栈中 ,当我回到HomeActivity What is your opinion on that? 您对此有何看法?

I'd consider using the single instance launchmode for that activity. 我会考虑对该活动使用单实例启动模式。

AndroidManifest.xml AndroidManifest.xml

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

Reference 参考

Only allow one instance of this activity to ever be running. 仅允许运行此活动的一个实例。 This activity gets a unique task with only itself running in it; 此活动获得一个唯一的任务,其中仅运行自身。 if it is ever launched again with the same Intent, then that task will be brought forward and its Activity.onNewIntent() method called. 如果使用相同的Intent再次启动该任务,则该任务将被提出并调用其Activity.onNewIntent()方法。 If this activity tries to start a new activity, that new activity will be launched in a separate task. 如果此活动尝试启动新活动,则该新活动将在单独的任务中启动。 See the Tasks and Back Stack document for more details about tasks. 有关任务的更多详细信息,请参阅“任务和后退堆栈”文档。

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 . 在Android文档中,我找到了要搜索的内容:可以设置标志FLAG_ACTIVITY_CLEAR_TOP清除后堆栈 The second flag FLAG_ACTIVITY_SINGLE_TOP avoid restarting the activity if used in combination with the flag mentioned before. 如果第二个标志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;
}

The intent needs to be passed using startActivityIfNeeded() . 需要使用startActivityIfNeeded()传递意图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM