【发布时间】:2021-07-21 08:40:11
【问题描述】:
我有两个 Activity,A 和 B。当用户按下 A 中的按钮时,B 会启动并使用 Picasso 加载图像。
当用户按下回然后再次按下 A 中的按钮时,B 显示相同的图像。
我希望 B 中的图像重新加载以显示不同的图像。
A to B image reloaded after pressing back
如何重新加载它?我正在考虑将B中的图像加载代码从onCreate()移动到onRestart()或onResume()。这是正确的想法吗?
按钮:
val goButton = findViewById<Button>(R.id.btn_go)
goButton.setOnClickListener {
val intent = Intent(this, WritingActivity::class.java)
startActivity(intent)
}
加载图片:
private const val RAND_IMAGE_URL:String = "https://images.unsplash.com/photo-1617386564901-be7cfcaa4c60?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=800&ixlib=rb-1.2.1&q=80&w=800"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_writing)
// Load the random image
val image = findViewById<ImageView>(R.id.iv_image)
Picasso.get().load(RAND_IMAGE_URL)
.error(R.drawable.ic_error_outline_72)
.into(image, object : Callback {
override fun onSuccess() {
// successfully loaded, start countdown timer
startTimer()
// hide progress bar
progressBar.visibility = View.GONE
}
override fun onError(e: Exception?) {
// display error message
Toast.makeText(this@WritingActivity, R.string.error_loading_image, Toast.LENGTH_LONG).show()
Log.e(TAG, "error loading image", e)
// hide progress bar
progressBar.visibility = View.GONE
}
})
}
【问题讨论】:
-
您正在加载常量 URL 并获取不同的图像,您需要生成不同的图像 URL。您需要先更改 RANDIMAGE_URL,然后再将其传递给 Picasso。
-
好的。我试图使用这个 url 加载不同的图像,但问题是一样的:source.unsplash.com/random/800x800
-
好的,现在你可以在这里做两件事。要么清除 Picasso 的整个缓存数据,要么使该 URL 无效。试试这个 Picasso.with(this).load(IMAGE_URL).skipMemoryCache().into(YOUR_IMAGE_VIEW);
-
太好了,我做到了,它奏效了。谢谢@Vijay
-
很高兴听到这个消息。你会接受这个作为答案吗?我可以写这个作为答案吗?
标签: android kotlin picasso android-lifecycle