简体   繁体   English

按后退按钮进入android主页

[英]going to android home on back button press

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

pressing the back button on Samsung note 4 shows me options to select touchwiz launcher or easy mode launcher. 按三星音符4上的后退按钮显示选择touchwiz启动器或简易模式启动器的选项。 All i want to do is go back to default launcher. 我想做的就是回到默认启动器。 Please help! 请帮忙!

I found this here 我在这里找到了这个

public void onBackPressed() {
     Intent startMain = new Intent(Intent.ACTION_MAIN);
     startMain.addCategory(Intent.CATEGORY_HOME);
     startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     startActivity(startMain);

}

You can try using this 你可以试试这个

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

You need to have action bar enabled for this(its actually enabled by default unless manually switch the Apptheme to disable it) and it will take you to the home screen as per your requirement on back pressed. 您需要为此启用操作栏(默认情况下它实际启用,除非手动切换Apptheme以禁用它),它将根据您按下后的要求进入主屏幕。

Add this to your onCreate method of the activity 将其添加到活动的onCreate方法中

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

R.drawable.back_arr is basically a drawble image of arrow that when pressed takes you back, you can use your own image here. R.drawable.back_arr基本上是一个箭头的抽象图像,按下后会将你带回来,你可以在这里使用你自己的图像。

Try resetting app preferences 尝试重置应用偏好设置

Go to Settings-> (Applications | Applications Manager) -> Right Menu and then select 'Reset App Preferences', a dialog will be opened to confirm the action. 转到设置 - >(应用程序|应用程序管理器) - >右菜单,然后选择“重置应用程序首选项”,将打开一个对话框以确认操作。 Press YES/OK to complete the action. 按YES / OK完成操作。

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

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