I have a navigation drawer in my Application and want to change the TextView
in the Menu from Login (when no user is logged in) to My Account (when a user logs in). I use attach()
and detach()
methods to reload the fragment. However, the view doesn't seem to get updated. Using logs, I have verified that the piece of code executes correctly.
Here is the onClick
action that happens when I click logout, which seems to successfully logout my user. SharedPrefs
is a class used to manage SharedPreferences
. The deletePrefs()
method works good, because if I press logout and restart the application, the user is successfully logged out.
case 2:
SharedPrefs.deletePrefs(getContext());
closeDrawer();
NavigationDrawerFragment mNavigationDrawerFragment = MainActivity.getNavigationDrawerFragment();
mNavigationDrawerFragment.setUserData("", "", BitmapFactory.decodeResource(getResources(), R.drawable.avatar));
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
Log.d("Fragment", "detach, attach");
break;
Here is the method that builds the Menu. It is called in the onCreateView()
method of my Fragment.
public List<NavigationItem> getMenu() {
items = new ArrayList<NavigationItem>();
items.add(new NavigationItem("Home", getResources().getDrawable(R.drawable.ic_home)));
if (!SharedPrefs.isLogin(getContext()))
items.add(new NavigationItem("Login", getResources().getDrawable(R.drawable.ic_login)));
else
items.add(new NavigationItem("My Account", getResources().getDrawable(R.drawable.ic_my_account)));
items.add(new NavigationItem("Logout", getResources().getDrawable(R.drawable.ic_logout)));
return items;
}
When detaching and attaching, isn't onCreateView()
executed again? Thus the Menu should get rebuilt and the TextView
should change, right?
EDIT: So using logs, I see that the onCreateView()
method is not called again when detaching and reattaching. Is this normal? Or am I detaching and reattaching the wrong way?