Language change issue after updating to androidx.a

2020-05-25 03:47发布

问题:

The link of the latest app-compat which is 1.1.0.

After upgrading my app to the latest app-compat my language settings stopped working for phones below API 24 (roughly, doesn't work on API 21 and below for sure).

For API 24 and above, I have used the ContextWrapper and set the locale hence works.

My question is if the androidx.appcompat:appcompat:1.1.0is the stable version why does it work for me in alpha and beta versions unlike the others here & the questions which I have tried.

  • After updating AppCompat library to appcompat:1.1.0-alpha03 Locale configuration is not working anymore
  • Change Locale not work after migrate to Androidx - Talks about the alpha and beta (I am using the latest stable build 1.1.0)

Should I wait for an official stable version again and downgrade to the last stable version or which is the efficient way to let google know if any(ofcourse, I know to file a bug)?

回答1:

Edit:

To continue using version 1.1.0 add this below your attachBaseContext:

Kotlin solution:

override fun applyOverrideConfiguration(overrideConfiguration: Configuration?) {
    if (overrideConfiguration != null) {
        val uiMode = overrideConfiguration.uiMode
        overrideConfiguration.setTo(baseContext.resources.configuration)
        overrideConfiguration.uiMode = uiMode
    }
    super.applyOverrideConfiguration(overrideConfiguration)
}

Java solution:

@Override
public void applyOverrideConfiguration(Configuration overrideConfiguration) {
    if (overrideConfiguration != null) {
        int uiMode = overrideConfiguration.uiMode;
        overrideConfiguration.setTo(getBaseContext().getResources().getConfiguration());
        overrideConfiguration.uiMode = uiMode;
    }
    super.applyOverrideConfiguration(overrideConfiguration);
}

If you don't need to upgrade to the latest appCompat then check the old answer. Else use the solution provided by @0101100101 here.

Old Answer:

After spending hours trying, got to know that it might be a bug.

Downgrade to the last stable version and it works flawlessly.

dependencies {
    implementation 'androidx.appcompat:appcompat:1.0.2'   //************ DO NOT UPGRADE LANGUAGE ISSUE on API 23 and below *******************//
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
....
}

Meanwhile, I have filed an issue with Google https://issuetracker.google.com/issues/140880275



回答2:

There is an issue in new app compat libraries related to night mode that is causing to override the configuration on android 21 to 25. This can be fixed by applying your configuration when this public function is called:

public void applyOverrideConfiguration(Configuration overrideConfiguration

For me, this little trick has worked by copying the settings from the overridden configuration to my configuration but you can do whatever you want. It is better to reapply your language logic to the new configuration to minimize errors

@Override
public void applyOverrideConfiguration(Configuration overrideConfiguration) {
if (Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT <= 25) {
        //Use you logic to update overrideConfiguration locale
        Locale locale = getLocale();//your own implementation here
        overrideConfiguration.setLocale(locale);
}
super.applyOverrideConfiguration(overrideConfiguration);
}


回答3:

Android x has supported this type of library ...

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation "com.google.android.material:material:1.1.0-alpha09"

}

Please always change androidx in design library or other som default android library.

And the compulsory change in layout code similar as ...

<androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_views"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:clipToPadding="false"
            android:padding="2dp" />


<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

so more other some type code change in layout...