0

I implemented night mode in my app.User can change the night mode settings in profile activity.The order of activities are as follows.

TabbedActivity>>DisplayActivity,ProfileActivity

The changed settings are applied only in current activity.(i.e profile activity).If the user presses back button,changes are not applied to that activities.Anyone help me to apply changes to all the activites.When we close the app and open again.changes are applied.But back press doesn't work.

This is the code I am using.

 @Override
protected void onCreate(Bundle savedInstanceState) {
    final SharedPreferences sharedPreferences = 
getSharedPreferences("NIGHT_MODE", MODE_PRIVATE);
    int result=sharedPreferences.getInt("NIGHT_MODE_OPTION",0);

    if (result==2){
        setTheme(R.style.darkTheme);
    }else setTheme(R.style.AppTheme);
    loadLocale();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
final SharedPreferences.Editor editor = getSharedPreferences("NIGHT_MODE", MODE_PRIVATE).edit();



    if (result==2){
        night.setChecked(true);
    }
    night.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                editor.putInt("NIGHT_MODE_OPTION",AppCompatDelegate.MODE_NIGHT_YES);
                editor.apply();
                startActivity(getIntent());
            }else {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                editor.putInt("NIGHT_MODE_OPTION",AppCompatDelegate.MODE_NIGHT_NO);
                editor.apply();
               startActivity(getIntent());

            }
        }
    });

}

Thrishool MSM
  • 337
  • 3
  • 11

2 Answers2

1

Since you are not considering Android lifecycle. You configure everything inside onCreate. However, while switching between activities current activity's lifecycle change accordingly. Here is lifecycle is explained very well

Solution:

When you come back to the previous activity, onResume is called. You should apply all changes inside this method

override fun onResume() {
   super.onResume()

   //Read your settings from SharedPrefs then apply, here
}
Jasurbek
  • 2,946
  • 3
  • 20
  • 37
0

'i think the better practice would to restart/reset app everytime the theme is changed'

Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
startActivity(i); 
finish();
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Muhmmad Umair
  • 140
  • 1
  • 7
  • CAn you please explain the first line of the code.I did not understand.What is MyApp and A.I mean what should i write in my case.I am updating in profile activity – Thrishool MSM Jun 10 '19 at 10:07
  • I applied your code.But when i press back button.Changes are not applied.If i close and open the app again,then the changes are applied – Thrishool MSM Jun 10 '19 at 10:12
  • Intent i = getBaseContext().getPackageManager() .getLaunchIntentForPackage( getBaseContext().getPackageName() ); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(i); finish(); try this – Muhmmad Umair Jun 10 '19 at 10:18
  • I need to write this code in which activity I am making changes? – Thrishool MSM Jun 10 '19 at 10:20
  • you need to write this code in clicklistener where you are changing theme – Muhmmad Umair Jun 10 '19 at 10:22
  • Thank you bro.This worked.This restarted the whole app.Please edit the answer. – Thrishool MSM Jun 10 '19 at 10:22
  • Is there any other way bro without restarting the whole app and apply changes to whole app. – Thrishool MSM Jun 10 '19 at 10:29
  • 1
    yeah you have to use onResume() in each activity to implement changes. whish is not a good practice. – Muhmmad Umair Jun 10 '19 at 10:32