androidback-buttonandroid-themeonresumeup-button

Android - Back button doesn't call onResume()


I'm a beginner at this, so bear with me.

In my app, I want the theme of the app to update based upon user preference. I have a preferences page with a theme section, and here is the code for my MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String userTheme = preferences.getString("theme_setting", "Blue");
    if (userTheme.equals("Blue"))
        setTheme(R.style.BlueTheme);
    else if (userTheme.equals("Red"))
        setTheme(R.style.RedTheme);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

    //Initiate buttons
    quadButton = (Button) findViewById(R.id.quadButton);
    sssButton = (Button) findViewById(R.id.sssButton);
    ssaButton = (Button) findViewById(R.id.ssaButton);
    areaButton = (Button) findViewById(R.id.areaButton);
    volumeButton = (Button) findViewById(R.id.volumeButton);
    conversionButton = (Button) findViewById(R.id.conversionButton);

    buttonListeners();
}

@Override
protected void onResume() {

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String userTheme = preferences.getString("theme_setting", "Blue");
    if (userTheme.equals("Blue"))
        setTheme(R.style.BlueTheme);
    else if (userTheme.equals("Red"))
        setTheme(R.style.RedTheme);
    super.onResume();
    setContentView(R.layout.activity_main);

    //Initiate buttons
    quadButton = (Button) findViewById(R.id.quadButton);
    sssButton = (Button) findViewById(R.id.sssButton);
    ssaButton = (Button) findViewById(R.id.ssaButton);
    areaButton = (Button) findViewById(R.id.areaButton);
    volumeButton = (Button) findViewById(R.id.volumeButton);
    conversionButton = (Button) findViewById(R.id.conversionButton);

    buttonListeners();
}

This works when the Up button is pressed to return to the MainActivity from the settings screen, but not when the system back button is pressed. I tried to solve that with the onResume() method but it seems to never actually call onResume during runtime. Also, in Android Studio, it says "Method onResume() is never used"

What is going wrong here? Any help would be appreciated, thanks!


Solution

  • onResume() does not take a parameter. You should also annotate with @Override to prevent such mistakes.

    Basically you have created a new method onResume() which will not be called as part of the Activity lifecycle.