Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使home按钮返回到常用活动?_Android_Android Actionbar_Actionbarsherlock_Back Stack_Android Homebutton - Fatal编程技术网

Android 如何使home按钮返回到常用活动?

Android 如何使home按钮返回到常用活动?,android,android-actionbar,actionbarsherlock,back-stack,android-homebutton,Android,Android Actionbar,Actionbarsherlock,Back Stack,Android Homebutton,我使用并希望启用主页按钮… 因此,我在基本活动中调用setHomeButtonEnabled(true) public class BaseFragmentActivity extends SherlockFragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { setTheme(R.style.Theme_Sherlock); super.

我使用并希望启用主页按钮…
因此,我在基本活动中调用
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)
startactivityifneedd(intent,0)
时,每次都会重新创建
HomeActivity
(该活动渲染地图并重新创建它很烦人)

  • 我不想这样做,因为这只会让我在应用程序层次结构中后退一步。相反,我总是想回到
    家庭活动
  • 此外,如果该行为可以在
    AndroidManifest.xml
    中配置,这将是一件好事,因为它是为
  • 当我返回到
    家庭活动时,清除后退也可能是常识。你对此有什么看法

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

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);
        }
    }
}
AndroidManifest.xml

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

仅允许运行此活动的一个实例。这 活动获得一个唯一的任务,其中只有它自己在运行;如果是 如果以同样的意图再次启动,那么该任务将 已提出并调用其Activity.onNewIntent()方法。如果这 活动尝试启动一个新活动,该新活动将 在单独的任务中启动。有关详细信息,请参阅任务和后堆栈文档 有关任务的更多详细信息


在Android文档中,我找到了我要搜索的内容:您可以设置。如果与前面提到的标志结合使用,则为第二个

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;
}

需要使用。

将您的家庭活动设置为singleInstance,并使用用户startActivity(intent),它将把您的旧家庭活动实例放在前面