Nikita .
Nikita .

Reputation: 35

Using Glide when starting new activity

I have RecyclerView with images in CardView, images are loaded using glide. In onClick() event i start a new Activity:

 override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val text = layout!!.findViewById<View>(R.id.item_text) as TextView
    val card = layout!!.findViewById<View>(R.id.item_card) as CardView
    val img = layout!!.findViewById<View>(R.id.item_image) as ImageView
    val requestOptions = RequestOptions()
    requestOptions.placeholder(R.drawable.placeholder)
    Glide.with(this.activity.applicationContext).setDefaultRequestOptions(requestOptions).load(mDataset[position].urlToImage).into(img)
    text.text = mDataset[position].title
    card.setOnClickListener {
        val intent = Intent(activity, ArticleActivity::class.java)
        intent.putExtra("Title", mDataset[position].title)
        intent.putExtra("Author", mDataset[position].source.name)
        intent.putExtra("Text", mDataset[position].description)
        img.transitionName = "item_image"
        intent.putExtra("urlToImage", mDataset[position].urlToImage)
        val options = ActivityOptionsCompat.makeSceneTransitionAnimation(this.activity, img as View, "item_image")
        this.activity.startActivity(intent, options.toBundle())
    }
}

But if the image does not have time to load, then when you go back to this activity, this ImageView will be empty. In manifest:

<activity
        android:name=".MainActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:label="@string/title_activity_main"
        android:theme="@style/AppTheme"
        android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".ArticleActivity"
        android:label="@string/title_activity_article"
        android:parentActivityName=".MainActivity"
        android:theme="@style/AppTheme.AppBarOverlay.TransparentStatusBar"/>

This happens when you press the hardware back button, and when you click toolbar back button, too.

Solved:

    val requestOptions = RequestOptions()
    requestOptions.placeholder(R.drawable.placeholder)
    requestOptions.dontAnimate()
    requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL)
    Glide.with(this.activity.applicationContext).setDefaultRequestOptions(requestOptions).load(mDataset[position].urlToImage).into(img)

Upvotes: 2

Views: 2042

Answers (2)

AskNilesh
AskNilesh

Reputation: 69724

You can use .diskCacheStrategy(DiskCacheStrategy.ALL) of glide to load image from Glide cache

val requestOptions = RequestOptions()
requestOptions.placeholder(R.drawable.placeholder)
requestOptions.dontAnimate()
requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL)

Glide.with(this.activity.applicationContext)
     .setDefaultRequestOptions(requestOptions)
    .load(mDataset[position].urlToImage)
    .into(img)

Upvotes: 2

Muhammad Usman Ghani
Muhammad Usman Ghani

Reputation: 1279

Glide load line / GlideModule (if any) / list Adapter code (if any):

This is one of the config I used during my trial and error run using this library. I've tried

.skipMemoryCache(true), .diskCacheStategy

make it work

Glide.with(itemView.getContext())
    .load(groupPhotos.getImg_url()) // simple url with an image
    .asBitmap()
    .placeholder(R.drawable.jaya_grocer)
    .error(R.drawable.cold_storage)
    .dontAnimate()
    .into(iv_image);

Layout XML:

<ImageView
android:id="@+id/iv_list_image"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="@+id/tv_list_members"
android:layout_marginTop="@dimen/std_padding"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/default_group_5" />

Upvotes: 1

Related Questions