简体   繁体   中英

Action bar title disappears after activity recreate

I have three activities. Main Activity, Settings Activity and Setup activity

Main opens Settings which opens Setup.

In setup activity I let the user change language which I store in SharedPreferences

In Main and Settings I have a listener for SharedPreference changes.

on change i recreate these activities (Main and Settings). All text is in the new language but the action bar title has disappeared and only reappears (in the correct language) after device rotation. It doesn't work to set the title with setTitle and getTitle returns the correct text. It's just invisible.

Any ideas on how to force the ActionBar title to be shown without rotating the device?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    ctx = getBaseContext();

    Language.loadLocale(ctx);
    setContentView(R.layout.activity_main);
    act = this;
    // Set up the action bar.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);


    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
            activeTab = position;
        }
    });


    actionBar.addTab(
            actionBar.newTab()
                    .setText(R.string.title_tab1_active_order_name)
                    .setTabListener(this));

    actionBar.addTab(
            actionBar.newTab()
                    .setText(R.string.title_tab2_history_name)
                    .setTabListener(this));

    actionBar.addTab(
            actionBar.newTab()
                    .setText(R.string.title_tab3_tools_name)
                    .setTabListener(this));

    listenOnLangChange();


}

private void listenOnLangChange(){
    //handles language changes
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
        public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
            // Implementation
            if (key.equals("setLanguageKey")){
                Configuration config = Language.loadLocale(ctx);
                onConfigurationChanged(config);
            }
        }
    };

    prefs.registerOnSharedPreferenceChangeListener(listener);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    act.recreate();
}

From Language class:

public static Configuration changeLang(String lang, Context ctx)
{

    String languageToLoad  = lang; // your language
    Locale locale = new Locale(languageToLoad);

    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;

    ctx.getResources().updateConfiguration(config,
            ctx.getResources().getDisplayMetrics());

    Log.d("CHANGE", languageToLoad);

    return config;
}


public static Configuration loadLocale(Context ctx)
{
    String langPref = "setLanguageKey";
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    String language = prefs.getString(langPref, "");
    Log.d("LOAD", language);

    return changeLang(language, ctx);
}

I had to solve it with a custom view added to the Action bar. It works but perhaps not the best solution.

public static void setActionbarTitle(Activity act, String title){
    ActionBar actionBar = act.getActionBar();

    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);

    LayoutInflater inflator = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflator.inflate(R.layout.actionbar_title_layout, null);

    TextView tv = (TextView) v.findViewById(R.id.tv_actionbar_title);
    tv.setText(title);
    tv.setTextColor(Color.WHITE);

    actionBar.setCustomView(v);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM