如何防止 Activity 在后退操作时重新加载

发布于 2024-11-16 17:50:51 字数 138 浏览 2 评论 0原文

我有连接到互联网以获取数据的应用程序。我可以多层次访问数据。

假设我从第 3 级开始,在第 4 级我决定返回,每当我按回之前的活动时,就会从互联网重新加载数据。

有可能阻止这种情况吗?

我尝试以单顶模式运行该活动。

I have app that connects to internet to get data. I can access data multi-level.

So let say I start at level 3 and on level 4 I decide to go back, whenever I press back the previous activity reloads the data from the internet.

Is there any possibility to prevent that?

I have tried to run the activity in single-top mode.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

樱花坊 2024-11-23 17:50:51

将数据加载代码移至 single-exec 事件:onStart 或

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(savedInstanceState == null) {
         // here
    }
}

Move the data loading code to the single-exec event: onStart or

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(savedInstanceState == null) {
         // here
    }
}
一抹微笑 2024-11-23 17:50:51

您在这里有很多选择。
更简单的方法是避免在切换到另一个活动时停止您的 activity。当您通过 startActivity() 转到级别 4 时,您的 onPause() 会在级别 3 活动上调用,但它继续驻留在后台。然后,当您从 3 返回时,将调用 onResume()
因此,只要您不在 resume 上加载数据,也不在 finish() 您的 3 级活动 上加载数据,就应该没问题。如果系统碰巧资源非常短缺,那么当您返回时,3 级活动可能会被终止(尽管这种情况非常罕见)并重新启动,但如果发生这种情况,则意味着系统需要内存对于一些重要的事情,重新加载可能没问题。

现在,还有其他方法。按照我上面的描述进行操作通常会更好,但如果由于某种原因您想完成 3 级活动,以下是您的选择。
正如已经指出的,您可以选择将数据转储到某个地方。保存的实例状态是一个选项 - 但如果数据量很大(超过几千字节),则不建议这样做。 想法是,您将数据保存在 Bundle 的 onSaveInstanceState() 中,并在 onCreate() 中恢复。
如果数据量很大,最好将其转储到缓存文件中。

如果您有一个数据模型,并且希望在多个活动(可能是一个小部件,甚至可能是一个不同的应用程序)中使用相同的数据,您可能需要考虑构建一个 ContentProvider 来提供数据。它将独立于应用程序的其他部分运行并管理对数据的访问。其他部分将查询它以获取所需的数据。

这样做的巧妙之处在于,它将数据从程序的其余部分中抽象出来。它可以从任何地方访问,并且缓存策略,一切都在专用的地方处理。缺点是,它要复杂得多。

You have a lot of options here.
The simpler one is to avoid stopping your activity when you switch to another one. When you go to level 4 by means of startActivity(), your onPause() is called on the level 3 activity but it continues living in the background. Then when you come back from 3, onResume() is called.
So as long as you don't load data on resume nor finish() your level 3 activity, you should be fine. If the system happens to be very short on resources then level 3 activity might be killed (though this is very rare) and restarted when you get back to it, but if it happens it means the system needed memory for something important and it's probably fine to reload.

Now, there are other ways. It's usually much better to do it as I described above, but if for some reason you want to finish your level 3 activity, Here are your options.
As it has been noted, you may elect to dump your data somewhere. The saved instance state is an option - though if it's heavy data, more than a few kilobytes, it's not recommended. The idea is, you save your data in onSaveInstanceState() in the Bundle and restore it in onCreate().
If it's heavy data, you would be better off dumping it in a cache file.

If you have a data model, and want to use the same data across several activities, maybe a widget and possibly even a different app, you may want to consider building a ContentProvider to supply the data. It would live independently of the other parts of your application and manage access to the data. Other parts would query it for the data they would need.

The neat thing about that is, it abstracts the data away from the rest of the program. It can be accessed from anywhere and caching policies and everything is handled in a dedicated place. The drawback is, it's significantly more complicated.

情栀口红 2024-11-23 17:50:51

一种可能的解决方案是与各州合作。基本上,您有一个布尔值来指示您的活动是否执行了某个操作。

如果您执行了该操作,它将不会再次到达该代码。当然,该标志必须保存在应用程序上下文或 SharedPreferences 中的某个位置。

One possible solution would be to work with states. You basically have a boolean which indicates if your activity has performed a certain action.

If the action was performed you it won't reach that code again. Of course that flag must be saved somewhere in the Application context or in SharedPreferences.

遇到 2024-11-23 17:50:51

您可以将此参数添加到您的意图中以防止重新加载。

//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(new Intent(this,My.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));

You can add this parameter to your intent to prevent reloading.

//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(new Intent(this,My.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文