Android: How to update title and ActionBar and DrawerLayout items at runtime?

0

I need to update the ActionBar title and each of the Drawer menu items with the language change at runtime. I use the following code to change the language of my application but I can not change the title and items of the actionbar (their names). I already tried using supportInvalidateOptionsMenu() and it did not work. I'm using the Support Library. The remaining items are only updated with Actionbar and Drawer .

Fragment currentFragment = getFragmentManager().findFragmentById(getId());
FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();
fragTransaction.detach(currentFragment);
fragTransaction.attach(currentFragment);
fragTransaction.commit();
    
asked by anonymous 27.05.2014 / 23:05

1 answer

1

I'm going to kick you into using the% Library of Support Library v4% and Support Library v7%, but I have two suggestions for solving this problem:

Simple mode: restart DrawerLayout , believing that the ActionBar you use in Activity and Strings will be correct soon after, this can be done as follows:

    if (Build.VERSION.SDK_INT < 11) {
        Intent intent = activity.getIntent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

        activity.finish();
        activity.overridePendingTransition(0, 0);
        activity.startActivity(intent);
    } else {
        activity.recreate();
    }

This mode is the easiest, but there is a risk of data loss and it has all the overhead of re-creating activity.

The other way, perhaps better depending on the case is to recover Drawer from ActionBar and use the ListView or DrawerLayout method in notifyDataSetChanged , forcing the update of the menu items. And with regard to notifyDataSetInvalidated , just update Adapter .

The ActionBar can be updated this way:

ActionBar ab = getSupportActionBar();

ab.setTitle(...); // Setar o title novamente, com a lingua correta.

As ActionBar , you need to either save a reference in your Activity or retrieve it from title , thus:

// Se ja tiver a referência, não precisa fazer isso..
ListView lv = findViewById(R.id.drawer_list); // resId do ListView
ListAdapter adapter = lv.getAdapter();

adapter.notifyDataSetInvalidate();
// ou dependendo da necessidade
adapter.notifyDataSetChanged();
    
28.05.2014 / 01:59