Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Resources.UpdateConfiguration(Configuration, DisplayMetrics) deprecated

EDIT 2

sample

EDIT

Override attachBaseContext method to update the context in activity

protected override void AttachBaseContext(Context @base)
{
    base.AttachBaseContext(@base);
}

end edit

In Android API 25 Resources.UpdateConfiguration(Configuration, DisplayMetrics) was deprecated and it's advised to use Context context = CreateConfigurationContext(Configuration); instead.

Current implementation

public override Resources Resources
{
    get
    {
        Resources res = base.Resources;
        Configuration config = new Configuration();
        config.SetToDefaults();

        res.UpdateConfiguration(config, res.DisplayMetrics);
        return res;
    }
}

Referencing Android context.getResources.updateConfiguration() deprecated as a guide, tried the following:

public override Resources Resources
{
    get
    {
        Configuration overrideConfiguration = base.Resources.Configuration;
        overrideConfiguration.SetToDefaults();
        Context context = CreateConfigurationContext(overrideConfiguration);
        Resources res = context.Resources;
        return res;
    }
}

However this produces anomalous errors..

Android.Views.InflateException: Error inflating class
com.android.internal.widget.DialogTitle

How to correctly implement Context context = CreateConfigurationContext(Configuration)?

Note 'Current implementation' works perfectly fine but as encouraged to not use deprecated code want to the replacement working

like image 438
jtth Avatar asked Mar 13 '26 19:03

jtth


1 Answers

Not sure if it's your need,you could change the attachBaseContext method like this,it could work:

protected override void AttachBaseContext(Context @base)
    {
        Configuration overrideConfiguration = new Configuration();
        overrideConfiguration = @base.Resources.Configuration;
        overrideConfiguration.SetToDefaults();
        Context context = @base.CreateConfigurationContext(overrideConfiguration);
        base.AttachBaseContext(context);
    }
like image 132
Leo Zhu - MSFT Avatar answered Mar 16 '26 11:03

Leo Zhu - MSFT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!