gpt4 book ai didi

android - 按后退按钮转到 android 主页

转载 作者:行者123 更新时间:2023-11-29 17:00:01 26 4
gpt4 key购买 nike

public void onBackPressed() {

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
/*finish();
System.exit(0);*/
return;
}

在 Samsung note 4 上按后退按钮会显示选择 touchwiz 启动器或简单模式启动器的选项。我想要做的就是返回默认启动器。请帮忙!

最佳答案

你可以试试这个

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.main, menu);
return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

您需要为此启用操作栏(默认情况下它实际上是启用的,除非手动切换 Apptheme 以禁用它)并且它会根据您的要求返回主屏幕。

将此添加到 Activity 的 onCreate 方法中

ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.back_arr);
actionBar.setDisplayShowHomeEnabled(true);

R.drawable.back_arr 基本上是一个可绘制的箭头图像,按下时可以返回,您可以在此处使用自己的图像。

关于android - 按后退按钮转到 android 主页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43364763/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com