简体   繁体   English

按返回后重新加载图像

[英]Reload image after pressing back

I have two activities, A and B. When a user presses a button in A, B starts and loads an image using Picasso.我有两个活动,A 和 B。当用户按下 A 中的按钮时,B 开始使用 Picasso 加载图像。

Example A to B示例 A 到 B

When the user presses back and then presses the button in A again, B shows the same image.当用户按下回然后再次按下 A 中的按钮时,B 显示相同的图像。

I want the image in B to reload in order to show a different image.我希望 B 中的图像重新加载以显示不同的图像。

A to B image reloaded after pressing back按返回后重新加载 A 到 B 图像

How do I reload it?我如何重新加载它? I was thinking of moving the image loading code in B from onCreate() to onRestart() or onResume() .我正在考虑将 B 中的图像加载代码从onCreate()移动到onRestart()onResume() Is this the right idea?这是正确的想法吗?

Button:按钮:

val goButton = findViewById<Button>(R.id.btn_go)
        goButton.setOnClickListener {
            val intent = Intent(this, WritingActivity::class.java)
            startActivity(intent) 
        }

Load image:加载图像:

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
                            }

                        })
    }

I think you don't need to move code in onCreate function.我认为您不需要在 onCreate function 中移动代码。 when you press back button, the B activity lifecycle is already in stop status.当您按下返回按钮时,B 活动生命周期已经处于停止状态。 that's why when you press button in A again, it will call onCreate function again.这就是为什么当你再次按下 A 中的按钮时,它会再次调用 onCreate function。 Need to generate random image url in each lifecycle I think.我认为需要在每个生命周期中生成随机图像 url。 Thanks谢谢

Move only the Picasso code into onResume()仅将毕加索代码移动到 onResume()

When you visit this URL through the browser it does some processing and shows you a random image every time.当您通过浏览器访问此URL时,它会进行一些处理并每次都会向您显示随机图像。

But in the case of Picasso, Picasso first checks its cache memory against that same URL before fetching it from the server.但是在 Picasso 的情况下,Picasso 首先检查它的缓存 memory 和相同的 URL,然后再从服务器获取它。 Since the URL is constant it going to show you the same image which was fetched first.由于 URL 是恒定的,它将向您显示首先获取的相同图像。

So is this case either you can clear the cache for Picasso or request Picasso to load a fresh image for this URL.在这种情况下,您可以清除毕加索的缓存或请求毕加索为此 URL 加载新图像。

Picasso.with(this)
   .load(URL).skipMemoryCache().into(YOUR_IMAGE_VIEW);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM