PKC
PKC

Reputation: 25

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!

Upvotes: 2

Views: 3714

Answers (3)

Kaustubh Kadam
Kaustubh Kadam

Reputation: 182

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

}

Upvotes: 3

PEHLAJ
PEHLAJ

Reputation: 10126

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.

Upvotes: 0

JBJ
JBJ

Reputation: 288

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.

Upvotes: 3

Related Questions