整理活动时,避免再次加载数据加载、数据

2023-09-12 06:01:25 作者:夜巷

在我的主要应用程序的活动,我下载了一些用户数据在 ONSTART()。当我打开设置和我打了返回按钮,我完成了设置活动,我返回到主活动。问题是,正在重新下载的数据。

In my main Activity of the app, I download some user data at the onStart(). When I open the Settings and I hit the Back button, I finish the Settings Activity and I return to the main Activity. The problem is that the data is being re-downloaded.

那么,怎样才能避免重新下载数据的?

So, how can avoid the re-downloading of data?

我有,虽然约 startActivityForResult ,但我不知道如何使用它,我不明白它是如何工作的。

I've though about startActivityForResult but I don't know how to use it and I do not understand how it works.

感谢你在前进!

推荐答案

在这样的主活动类中定义一个静态变量:

Define a static variable in your main activity class like this:

public static bool download = true;

当您从设置返回的值更改为false,如:

When you are returning from settings change its value to false like:

MainActivity.download = false;

和把你的下载方法的if语句像这里面的:

And put your download method inside an if statement like this:

if (download) {
yourdownloadcode();
}
 
精彩推荐
图片推荐