AppCompatDelegate.setDefaultNightMode doesn't work on physical device

1.2k views Asked by At

Changing night-light mode perfectly works in my emulator, but when i try to do the same on physical device(xiaomi) I get next exception:

E/ActivityInjector: get life cycle exception
    java.lang.ClassCastException: android.os.BinderProxy cannot be cast to android.app.servertransaction.ClientTransaction
        at android.app.ActivityInjector.checkAccessControl(ActivityInjector.java:24)
        at android.app.Activity.onResume(Activity.java:1859)
        at androidx.fragment.app.FragmentActivity.onResume(FragmentActivity.java:456)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1454)
        at android.app.Activity.performResume(Activity.java:8050)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4269)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4311)
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ClientTransactionHandler.executeTransaction(ClientTransactionHandler.java:57)
        at android.app.ActivityThread.handleRelaunchActivityLocally(ActivityThread.java:5353)

Here is my code in activity and presenter where i change mode of my my application:

public class MoviesListActivity extends AppCompatActivity implements MovieListView {

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.darkMode) {
            presenter.changeToDarkMode();
        }
        else if(item.getItemId() == R.id.lightMode) {
            presenter.changeToLightMode();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.movie_list);

        presenter =  new MoviesListPresenter(this, getApplicationContext());

        if (presenter.getCurrentMode() == 0) {
            presenter.changeToLightMode();
        } else {
            presenter.changeToDarkMode();
        }
    }

    //Other code...
}

Presenter:

public class MoviesListPresenter {
 
    public void changeToLightMode() {
        currentMode = context.getSharedPreferences(Constants.MODE, Context.MODE_PRIVATE);
        currentMode.edit().putInt(Constants.MODE, 0).apply();
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    }

    public void changeToDarkMode() {
        currentMode = context.getSharedPreferences(Constants.MODE, Context.MODE_PRIVATE);
        currentMode.edit().putInt(Constants.MODE, 1).apply();
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }

    public int getCurrentMode() {
        currentMode = context.getSharedPreferences(Constants.MODE, Context.MODE_PRIVATE);
        return currentMode.getInt(Constants.MODE, 0);
    }
}

I will be thankful for any help!

1

There are 1 answers

0
Akexorcist On

This seem caused by calling Activity.recreate() in Xiaomi's firmware. This problem also happen to my library which recreate the activity to apply the language changing.

See https://github.com/akexorcist/Localization/issues/89

In my case, no app crashing and code working properly. So I skipped this problem.

Related questions