重新创建()增加内存占用

时间:2016-09-27 19:38:03

标签: android android-activity memory-leaks locale

我的活动包含两个带标题的单选按钮:HindiEnglish

为了刷新UI,根据用户选择的语言,我执行以下步骤:

  1. 使用语言代码更新用户首选项
  2. 调用recreate()方法
  3. onCreate()中从首选项中获取语言代码
  4. 基于获取的语言代码更新语言环境
  5. 最后设置ContentView
  6. 问题是 -

      

    当我不断在这两种语言之间切换时   线性增加应用程序的内存占用量   ' Android Monitor'。

    以下是代码:

    //region Activity Life Cycle methods
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            refreshLocaleAsPerPreferences();
            setContentView(R.layout.activity_welcome);
            markRadioButtonSelectedAsPerPreferences();
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            previousLanguage = null;
        }
    
        //endregion
    
        //region Locale refresh related methods
        private void updateLocale(String languageIsoCode) {
            if(languageIsoCode != null && languageIsoCode.equalsIgnoreCase("")){
                return;
            }
            Locale locale = new Locale(languageIsoCode);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getResources().updateConfiguration(config,getResources().getDisplayMetrics());
        }
    
        private void refreshLocaleAsPerPreferences() {
            // Obtaining language from shared preferences
            String languageIsoCode = getLangFromPreferences();
    
            // Check for null
            if(languageIsoCode != null && languageIsoCode.equalsIgnoreCase("")){
                // No language found
                // Obtaining language from device preferences
                String devicePreferencesLanguage = Locale.getDefault().getLanguage();
                if (devicePreferencesLanguage.equalsIgnoreCase(HindiISOCode)
                        || devicePreferencesLanguage.equalsIgnoreCase(EnglishISOCode)) {
                    // Device preferences language is one of the default languages
                    // Hence update the same
                    languageIsoCode = devicePreferencesLanguage;
                }
                else {
                    // Device language is different than the default languages
                    // Hence default to english
                    languageIsoCode = EnglishISOCode;
                }
    
                // update language in preferences
                updateLangIsoCodeInPreferences(languageIsoCode);
            }
    
            // Updating locale
            updateLocale(languageIsoCode);
            previousLanguage = languageIsoCode;
        }
        //endregion
    
        //region radio button selection related methods
        private  void markRadioButtonSelectedAsPerPreferences() {
            String languageInPreferences = getLangFromPreferences();
            markRadioButtonSelected(languageInPreferences);
        }
    
        private  void markRadioButtonSelected(String languageCode) {
            RadioButton radioButton = getRadioButton(languageCode);
            radioButton.setChecked(true);
        }
    
        private RadioButton getRadioButton(String tag) {
            switch (tag) {
                case HindiISOCode: return (RadioButton)findViewById(R.id.radio_hindi);
                case EnglishISOCode : return (RadioButton)findViewById(R.id.radio_english);
                default:
                    // if any other language is selected
                    // select english
                    return (RadioButton)findViewById(R.id.radio_english);
            }
        }
    
        private void refreshUI(String lang) {
            updateLangIsoCodeInPreferences(lang);
            recreate();
        }
        //endregion
    
        //region User Actions
        public void onLanguageSelection(View view) {
            RadioButton selectedRadioButton = (RadioButton)view;
            String tag = selectedRadioButton.getTag().toString();
    
            // Refresh UI only if current language is different than the previous one
            if (!tag.equalsIgnoreCase(previousLanguage)) {
                refreshUI(tag);
                previousLanguage = tag;
            }
        }
        //endregion
    
        //region Shared preferences get-set methods
        private void updateLangIsoCodeInPreferences(String languageIsoCode) {
            SharedPreferences sharedPreferences = getSharedPreferences("CommonPreferences",this.getApplicationContext().MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("currentAppLang",languageIsoCode);
            editor.commit();
        }
    
        private String getLangFromPreferences() {
            SharedPreferences sharedPreferences = getSharedPreferences("CommonPreferences",this.getApplicationContext().MODE_PRIVATE);
            String languageIsoCode = sharedPreferences.getString("currentAppLang", "");
            return  languageIsoCode;
        }
        //endregion
    

    如何在规定的场景中优化内存占用?请建议。

0 个答案:

没有答案