I am using relativelayout to set an image.Why I hadn\'t using imageview means, inside relativelayout image, I am setting icons.
I dont know what is the issue exactly
In activity I use Glide.with(getApplicatonContext())
and
in adapter I use Glide.with(myContext.getApplicatonContext())
//It works for me fine.
//for adapter
Context myContext;
//also initilize in `oncreateViewHolder`
@Override
public MessagesHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_messages_layout, parent, false);
myContext = parent.getContext();
return new MessagesHolder(view);
}
You can simply check the context is destroyed or not manually as;
if (context == null) {
return
} else if (context !is Application) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
if (context is FragmentActivity) {
if ((context as FragmentActivity).isDestroyed) {
return
}
} else if (context is Activity) {
if ((context as Activity).isDestroyed) {
return
}
}
}
}
This can also be represented as a Kotlin extension function:
/**
* Return true if this [Context] is available.
* Availability is defined as the following:
* + [Context] is not null
* + [Context] is not destroyed (tested with [FragmentActivity.isDestroyed] or [Activity.isDestroyed])
*/
fun Context?.isAvailable(): Boolean {
if (this == null) {
return false
} else if (this !is Application) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
if (this is FragmentActivity) {
return !this.isDestroyed
} else if (this is Activity) {
return !this.isDestroyed
}
}
}
return true
}
Inspired from a GitHub thread, I am using this before loading any image
final Context context = getContext();
if (isValidContextForGlide(context) {
// Load image via Glide lib using context
}
public static boolean isValidContextForGlide(final Context context) {
if (context == null) {
return false;
}
if (context instanceof Activity) {
final Activity activity = (Activity) context;
if (activity.isDestroyed() || activity.isFinishing()) {
return false;
}
}
return true;
}
hi in view model for frament The activity context is kept even when you press the back button and exit the program. And go back to the program, you do not need to re-value the context because the context is static
@Override
public void onBindViewHolder(@NonNull final viewholder viewholder, final int i) {
final Model_Post model_post = list.get(i);
Log.e(TAG, "onBindViewHolder: "+model_post.getImageurl());
Glide.with(MainActivity.activity)
.load(Uri.parse(model_post.getImageurl()))
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(viewholder.itemsPostBinding.imgvItempost);
}
You must do this in main activity :
public static AppCompatActivity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = MainActivity.this;
}
I have got the same issue before few days.I have solved this to passing the Application context memory behalf of current Class context memory.
May be it will help you :-
use this code
Glide.with(getApplicationContext())
.load(coverIMGurl)
.asBitmap()
.signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
.into(new SimpleTarget<Bitmap>(500, 500) {....}
Even you are getting this issue then read this article carefully "https://github.com/bumptech/glide/issues/1097"
overview for this issue : This is an issue of Glide library.
Use:
Glide.with(getApplicationContext()).load(...)
Instead of:
Glide.with(TabMorePagesDetailActivity.this).load(...)
Hope it will solve your problem~