Faisal Shaikh
Faisal Shaikh

Reputation: 4138

How to recreate activity from another activity when enable/disable Night mode?

I have found many questions related to this but my problem is a little bit different.

From SettingActivity, I enable/disable the nigh mode through the following code:

ApplicationClass.setNightMode();
ApplicationClass.setDayMode();

Actuall code written in application class.

But when I return back to MainActivity from SettingActivity the color does not change of layout because of onCreate() method do not call.

How can I recreate the MainActivity from SettingActivity when I enable/disable night mode.

My Application code from where I enable and disable night mode:

    public static void setNightMode(){
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }

    public static void setDayMode(){
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    }

Upvotes: 0

Views: 965

Answers (3)

apksherlock
apksherlock

Reputation: 8371

Your AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); should be on the Application level. And than it won't be a problem when turning back or going forward to any activity between your app. Don't forget to register it to the manifest.

Example:

public class TestApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        //give a start value AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }

    public static void setNightMode(){
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }

    public static void setDayModeOrSth(){
        //AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }
}

And in your manifest file:

<application
            android:name="package.name.TestApplication"

And than on your Acivity/Fragments

Testapplication.setDayModeOrSth();

or

Testapplication.setNightMode();

Exuse my stupid naming and example :) But it would be something like that. Good luck.

Upvotes: 1

Sanjay Bhalani
Sanjay Bhalani

Reputation: 2464

You can overwrite the onConfigurationChanged method of activity.

public abstract void onConfigurationChanged (Configuration newConfig)

Upvotes: 0

medyas
medyas

Reputation: 1286

if you recreating the MainActivity you will lose it's current state so you can try overriding onResume and check whether you need to enable/disable night mode:

public class MainAcitivy extends ... {

    .....

    @Override
    public void onResume(){
        super.onResume();
        if(nightModeOn) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        }
}

The other solution is to start a new MainActivity by overriding the onBackPressed in SettingActivity and start MainActivity:

public class SettingsActivity extends .. {

    .....

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

}

Upvotes: 0

Related Questions