Krishna Ch
Krishna Ch

Reputation: 1531

Update profile picture in navigation drawer when picture is changed from Edit Profile Fragment

i have an android app which is having 4 fragments with navigation drawer with profile pic, but the thing is when i Change/Update the profile pic in Edit Profile Fragment the profile pic is updating in this fragment and also im saving the pic URL in Shared Preferences(also uploading to server), but when i come back to Home Fragment the profile picture in navigation drawer is not updating with the latest picture. I am using Glide lib. for loading the profile pic, this image is loading the URL which is stored in Shared preferences. But when i close my app and opens the profile picture in navigation drawer update with the latest picture taken. I can't ask the user to close the app and open whenever he changes his profile pic in Edit Profile Fragment. Is there any way to solve this problem?

 //getting image URL from Shared Preferences.
       imgUrl = Prefrences.getProfile_picture(HomeActivity.this);
       loadImageUrl(imgUrl);

//loading image in navigation drawer with glide in Main Activity
private void loadImageUrl(String imgUrl) {
    if (imgUrl != null && !imgUrl.isEmpty()) {
        Glide.with(this).load(imgUrl).placeholder(R.drawable.avatar)
                .crossFade()
                .thumbnail(0.5f)
                .bitmapTransform(new CircleTransform(this))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(img_profile);
    } else { 
       getUserDetails();  <-- this method is called when imgUrl is Null,
    }

}

Upvotes: 3

Views: 3302

Answers (2)

Vishal Kumar
Vishal Kumar

Reputation: 4627

You can call the navigation view to refresh the data when you create your Navigation Activity....

Separate the data binding part and also Call like this from the onCreate()

private void updateNavHeaderView(){
View headerView = navigationView.inflateHeaderView(R.layout.nav_header);
        ImageView ivUserProfilePhoto = (ImageView) headerView.findViewById(R.id.ivUserProfilePhoto);
        TextView userName = (TextView) headerView.findViewById(R.id.userName);

        if (user != null && user.getUser_image() != null && !user.getUser_image().equals("")) {
            Picasso.with(MainActivity.this)
                    .load(Config.BASE_IMAGE_URL+"/"+user.getUser_image())
                    .placeholder(R.drawable.user_profile)
                    .resize(avatarSize, avatarSize)
                    .centerCrop()
                    .transform(new CircleTransformation())
                    .into(ivUserProfilePhoto);
        } else {
            ivUserProfilePhoto.setImageDrawable(getResources().getDrawable(R.drawable.user_profile));
        }
        }

Upvotes: 1

Gustavo Conde
Gustavo Conde

Reputation: 977

Probably your image is being cached by Glide.

Check the top 2 answers here Remove image from cache in Glide library

Upvotes: 0

Related Questions