• 6
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I found that the first running activity of the app after rewriting getResources() will also take effect for the activity that runs later, provided that your phone has a very large font.

E.g: Perform below steps

  1. Set the phone font to Big Mac
  2. Create two activities named A and B, where A activity overrides getResources(), and B does not perform any special operations.
  3. First enter B activity and find the font is enlarged
  4. Exit the B page and enter the A page. The A page font size is normal.
  5. Exit A, then enter B, and find that B's font size is normal.

Then I combed the source code, including the activity startup process, I guess where is the configuration cache, the following running activity can be used, but ultimately did not find

This is about A activity ?

@Override
    public Resources getResources() {
        Resources resources = super.getResources();
        try {
            if (resources != null) {
                Configuration newConfig = resources.getConfiguration();
                DisplayMetrics displayMetrics = resources.getDisplayMetrics();
                if (newConfig.fontScale != 1) {
                    newConfig.fontScale = 1;
                    if (Build.VERSION.SDK_INT >= 17) {
                        Context configurationContext = createConfigurationContext(newConfig);
                        resources = configurationContext.getResources();
                        displayMetrics.scaledDensity = displayMetrics.density * newConfig.fontScale;
                    } else {
                        resources.updateConfiguration(newConfig, displayMetrics);
                    }
                }
            }
            Log.d(TAG, "A-Activity fontScale :" + resources.getConfiguration().fontScale + " ");
            return resources;
        } catch (Exception e) {
        }
        return resources;
    }

and B activity does not perform any special operations.

@Override
    public Resources getResources() {
        Resources resources = super.getResources();
        Log.d(TAG, "B-Activity fontScale :" + resources.getConfiguration().fontScale + " ");
        return super.getResources();
    }

and the result after run app, about the Logcat:

2019-01-28 10:46:22.006  B-Activity fontScale :1.3 
2019-01-28 10:46:22.007  B-Activity fontScale :1.3 
2019-01-28 10:46:26.675  A-Activity fontScale :1.0 
2019-01-28 10:46:26.675  A-Activity fontScale :1.0 
2019-01-28 10:46:29.818  B-Activity fontScale :1.0 
2019-01-28 10:46:29.818  B-Activity fontScale :1.0 

So I am very confused, how does android work internally, let the setting of getResources() of the previous activity take effect for the next one, personal guess is handled in ActivityStack, but can't be sure, I hope to give pointers, thank you very much.

    • I'd imagine that Android passes the current Resources onto the next Activity, when calling startActivity(). Try getApplicationContext().startActivity() instead. Is the font still changed?

Activities in the same application will share the same Configuration by default.

If you debug your app you will find A.getResources().getConfiguration() return the same Configuration instance as B.getResources().getConfiguration(). Thus after you modify the Configuration in A, it will also influence the Configuration in B.

Try to replace Configuration newConfig = resources.getConfiguration(); with Configuration newConfig = new Configuration(resources.getConfiguration());

For more details see source code ResourcesManager.updateResourcesForActivity.

  • 1
Reply Report