简体   繁体   中英

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. 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.

Add this to your onCreate method of the activity

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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