I use the recently provided Google Support Library to add a navigation box to my application. In the header of the navigation box, I define a CircleImageView to set the pic user profile, its name under it.

All this information is first determined by the user on the first launch of the application, so when mainActivity starts, the image and name are loaded into the header. But I give the user the opportunity to change them inside the application. If the user goes to the profile fragment and selects a new image as the pic profile or if he changes the name, these parameters will not be updated in the header of the navigation box until the application is closed and loaded again.
This is how I defined it in mainActivity:
public class MainActivity extends AppCompatActivity implements DrawerLayout.DrawerListener { private NavigationView navView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); .... navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { menuItem.setChecked(true); displayFragment(menuItem.getItemId()); drawerLayout.closeDrawers(); return true; } }); }
There is no need to implement DrawerLayout.DrawerListener, but as you can see at the top, I did this to try to update the title in real time. They implement the following methods:
@Override public void onDrawerSlide(View drawerView, float slideOffset) { } @Override public void onDrawerOpened(View drawerView) { try { ContextWrapper cw = new ContextWrapper(getApplicationContext()); File directory = cw.getDir("profile", Context.MODE_PRIVATE); File mypath = new File(directory, "thumbnail.png"); Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(mypath)); thumbview.setImageBitmap(bitmap); username = mPreferences.getString("NAME", null); nameview.setText(username); } catch (FileNotFoundException e) { Log.e("LOAD_IMAGE", e.getMessage(), e); thumbview.setImageResource(R.drawable.default_thumbnail); } } @Override public void onDrawerClosed(View drawerView) { } @Override public void onDrawerStateChanged(int newState) { }
In the onDrawerOpened () method, I used the same code that I use in onCreate to load the pic profile and name. This should update this parameter every time the box is opened, but it doesnβt do this, still does not update the header until I close and open the application.
source share