如何将图片加载至非活跃Activity中的ImageView?
Hey there! Let me start by answering your core question first: it's not feasible to directly load an image into an ImageView that belongs to an inactive Activity—and here's why, plus some solid alternative solutions to achieve your goal.
Why direct loading isn't possible
Android's Activity lifecycle tightly controls the state of UI components like ImageView. An inactive Activity (either not yet launched, or in onStop/onDestroy state) doesn't have an inflated view hierarchy yet, or its views have already been destroyed and garbage-collected. Trying to reference an ImageView from such an Activity will either result in a NullPointerException (if the Activity hasn't started) or cause memory leaks (if you hold onto a stale reference).
Alternative solutions to preload images for later use
1. Preload images to cache using a library (recommended)
Use popular image-loading libraries like Glide, Picasso, or Coil—they handle caching (memory + disk) out of the box, which is the simplest and most efficient way to preload images for later use in another Activity.
- On your home screen Activity, preload the image to cache:
// Using Glide as an example Glide.with(this) .load("your_image_url_here") .diskCacheStrategy(DiskCacheStrategy.ALL) // Cache to memory and disk .preload()
- When the target Activity is launched later, load the image from the cache directly into the
ImageView—it'll load instantly:
Glide.with(this) .load("your_image_url_here") .into(your_target_image_view)
The library will automatically use the cached image instead of re-downloading it, so users won't notice any delay.
2. Store the loaded Bitmap in a shared ViewModel or singleton (manual approach)
If you prefer not to use a library, you can load the image as a Bitmap on the home screen and store it in a shared container, then retrieve it when the target Activity starts. Note: Be careful with memory management here—Bitmaps are memory-heavy, so remember to clear references when no longer needed.
- Create a singleton class to hold the Bitmap:
object ImageCacheManager { private var cachedBitmap: Bitmap? = null fun setCachedBitmap(bitmap: Bitmap?) { cachedBitmap?.recycle() // Recycle old bitmap to free memory cachedBitmap = bitmap } fun getCachedBitmap(): Bitmap? = cachedBitmap fun clearCache() { cachedBitmap?.recycle() cachedBitmap = null } }
- On the home screen, load the image and store it (make sure to run this on a background thread!):
val imageUrl = URL("your_image_url_here") val bitmap = BitmapFactory.decodeStream(imageUrl.openStream()) ImageCacheManager.setCachedBitmap(bitmap)
- In the target Activity, retrieve and display the Bitmap:
val bitmap = ImageCacheManager.getCachedBitmap() bitmap?.let { your_target_image_view.setImageBitmap(it) } // Don't forget to clear the cache when you're done to avoid memory leaks // ImageCacheManager.clearCache()
3. Pass the image URL to the target Activity and load on launch (with preload)
This is similar to the first approach but explicitly passes the URL to the target Activity. The home screen still preloads the image to cache, so when the target Activity loads it, it uses the cached version.
- When launching the target Activity, pass the URL via Intent:
val intent = Intent(this, TargetActivity::class.java) intent.putExtra("IMAGE_URL", "your_image_url_here") startActivity(intent)
- Preload the image on the home screen (same as approach 1), then in the target Activity:
val imageUrl = intent.getStringExtra("IMAGE_URL") imageUrl?.let { Glide.with(this) .load(it) .into(your_target_image_view) }
Key notes to avoid issues
- Never hold a reference to an inactive Activity's
ImageView—this will cause memory leaks. - Using image-loading libraries is highly recommended because they handle memory management, cache eviction, and background threading automatically.
- If you use manual Bitmap storage, always recycle unused Bitmaps and clear references to prevent OutOfMemoryErrors.
内容的提问来源于stack exchange,提问作者Tushar Jain

