Search code examples
androidthemesback-buttononcreate

What method is executed on Up button pressed


I implemented a PreferenceActivity and I allow the user to change the theme of the app. When the switch for that setting is flipped, initially nothing happens.

When the Up button is pressed, I am taken to the parent activity and it reloads the theme to use the new theme. However, if I'm still in the SettingsActivity and press the Back button, I get back to the MainActivity but the new theme is not applied. I suppose MainActivity doesn't reload onCreate() in that case.

I would like to override the Back button's behaviour in that SettingsActivity only and I know how to, but I don't know what method to execute in order to imitate the Up button pressed behaviour.

Any ideas?


Solution

  • If the parent activity is something you have control over, just override the back button press. You can do it like so:

    @Override
    public void onBackPressed() {
        // Replace "ParentActivity.class" with the name of your parent activity
        Intent parentIntent = new Intent(getApplicationContext(), ParentActivity.class);
        startActivity(parentIntent);
    }
    

    The above code should override the back button and give you the result you want.