Wolf87
Wolf87

Reputation: 540

How can I restart activity on back button pressed?

I'm going from main activity to another activity and then when I press back button I want to go back to the main activity and restart it. How can I do that?

Upvotes: 3

Views: 28940

Answers (5)

OGZCoder
OGZCoder

Reputation: 285

I was fiddling with this problem in the context of trying to reload my main activity after changing preferences in a custom PreferenceActivity and after some trial and error and reading other responses, this is what worked for me:

In my PreferenceActivity I use onBackPressed() to start my MainActivity (MainActivity.class is the activity I am returning to:

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

In my MainActivity I'm calling my ReferenceActivity (PreferenceSettings.class is the ReferenceActivity i'm going to through the function startSettings() through an onClick atrribute in my layout XML:

    public void startSettings(View view){
        Intent intent = new Intent(this, PreferenceSettings.class);
        startActivity(intent);
        finish();
    }

I use this to restart my MainActivity so it will run the onCreate() method again, reloading (refreshing) my views with any changes made in my PreferenceActivity. This causes my MainActivity to finish and unload when I start my ReferenceActivity, allowing it to be restarted through the back button being pressed from my PreferenceActivity. I am then refreshing the displayed values through the onCreate() method that loads the initial values from my SharedPreferences when the MainActivity is created.

Hope this helps somebody!

Upvotes: 3

Yaqub Ahmad
Yaqub Ahmad

Reputation: 27659

Assuming that you have not shutdown the MainActivity when you opened the current activity then this should work for you:

    @Override  
   public boolean onKeyDown(int keyCode, KeyEvent event)  
   {  
       if (keyCode == KeyEvent.KEYCODE_BACK) {
           finish();
           return true;
       }

       return super.onKeyDown(keyCode, event); 
}

OR:

@Override
public void onBackPressed() {
    finish();
 }

Upvotes: 1

Mark Mooibroek
Mark Mooibroek

Reputation: 7696

You should not have to override your onBackPressed. The method below works like a charm and is (imho) best practice.

in your MainActivity do this:

private static int CODE = 1; //declare as FIELD

startActivityForResult(new Intent(MainActivity.this, SecondActivity.class), CODE);

This will monitor if the user killed or quited the SecondActivity. You catch this by overriding onActivityResult in MainActivity

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         //SecondActivity closed
         if(requestCode == CODE){
              startActivity(new Intent(MainActivity.this, MainActivity.class)); //reload MainActivity
              finish();
         }
}

Upvotes: 2

dermatthias
dermatthias

Reputation: 1180

When you press the back button while in the second activity, then Android automatically invokes the previous activity on the back stack, which is in your case your main activity.

For more information, see the Android docs for Tasks and Back Stack.

Android calls the onResume() callback every time you return to an activity with the back button. Do all your "restart" work there.

Upvotes: 3

Dan
Dan

Reputation: 3884

You could just override the onBackButton pressed in your second activity to start the first one. Example:

@Override
public void onBackPressed() {
    startActivity(new Intent(this, FirstActivity.class));
}

Upvotes: 12

Related Questions