在活动之间使用 SharedPreferences

发布于 2025-01-05 02:49:33 字数 1903 浏览 0 评论 0原文

我的应用程序基于 DashboardActivity。另一项活动称为新闻。

在此新闻活动中,我使用 AsyncTask 从 Internet 加载信息。用户可以离开此活动并启动其他活动,并且我只想在应用程序生命周期的持续时间内下载信息一次

也就是说,我想要的是:

  • 用户启动应用程序,转到新闻,下载信息。
  • 应用程序会记住该信息已下载,因此如果再次启动新闻,将不会下载该信息。
  • 当应用程序停止时(假设用户从主要活动中按后退按钮),应用程序必须“忘记”信息已下载。因此,下次启动应用程序时,必须在新闻启动时再次下载信息。

我考虑过使用 SharedPreferences 来做到这一点。

这是新闻中的样子:

int news_loaded = 0;
public static final String PREFS_NAME = "MyPrefsFile";
static SharedPreferences preferences;
SharedPreferences.Editor editor;

public void onCreate(Bundle savedInstanceState)
{
    ....
    preferences = getSharedPreferences(PREFS_NAME, 0);
    editor = preferences.edit();
    news_loaded = preferences.getInt("news_loaded", 2);

    <<start asynctask here>>
}

在 AsyncTask 的 doInBackground 中:

if(news_loaded == 0)
    <<download info>>

在 AsyncTask 的 onPostExecute 中(如果仅下载信息):

editor.putInt("news_loaded", 1);
editor.commit();

这是 onStop< DashboardActivity 的 /code> 方法:

public static final String PREFS_NAME = "MyPrefsFile";
static SharedPreferences preferences;
SharedPreferences.Editor editor;

protected void onStop()
{
    preferences = getSharedPreferences(PREFS_NAME, 0);
    editor = preferences.edit();
    editor.putInt("news_loaded", 0);
    editor.commit();
    super.onStop();
}

问题:当我启动新闻活动时,news_loaded 似乎始终为 0。


更新

这是 DashboardActivity 的清单部分:

    <activity
        android:name=".DashboardActivity"
        android:launchMode="singleInstance"
        android:configChanges="keyboardHidden|orientation"/>

似乎每次我按下后退按钮时都会调用 onStop() (和 onDestroy()) - 无论“多远”在”我在不同的活动中。

My app is based around a DashboardActivity. Another activity is called News.

In this News activity, I load information from the internet using an AsyncTask. The user can navigate away from this activity and start others, and I would like to download the information only once in the duration of the app's life cycle.

That is, what I want is:

  • User starts app, goes to News, information is downloaded.
  • App remembers that the info has been downloaded, so if News is started again, the information will not be downloaded.
  • When the app is stopped (let's say user presses back button from main activity), the app must "forget" that the info had been downloaded. So next time the app is started, the info must be downloaded again when News is started.

I thought about doing this with SharedPreferences.

Here's what it looks like in News:

int news_loaded = 0;
public static final String PREFS_NAME = "MyPrefsFile";
static SharedPreferences preferences;
SharedPreferences.Editor editor;

public void onCreate(Bundle savedInstanceState)
{
    ....
    preferences = getSharedPreferences(PREFS_NAME, 0);
    editor = preferences.edit();
    news_loaded = preferences.getInt("news_loaded", 2);

    <<start asynctask here>>
}

In the doInBackground of the AsyncTask:

if(news_loaded == 0)
    <<download info>>

In the onPostExecute of the AsyncTask (if info was downloaded only):

editor.putInt("news_loaded", 1);
editor.commit();

Here's the onStop method of the DashboardActivity:

public static final String PREFS_NAME = "MyPrefsFile";
static SharedPreferences preferences;
SharedPreferences.Editor editor;

protected void onStop()
{
    preferences = getSharedPreferences(PREFS_NAME, 0);
    editor = preferences.edit();
    editor.putInt("news_loaded", 0);
    editor.commit();
    super.onStop();
}

Problem: the news_loaded always seems to be 0 when I start the News activity.


Update

This is the manifest part of DashboardActivity:

    <activity
        android:name=".DashboardActivity"
        android:launchMode="singleInstance"
        android:configChanges="keyboardHidden|orientation"/>

It seems that onStop() (and onDestroy()) are called each time I press the back button - no matter "how far in" I am in different activities.

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

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

发布评论

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

评论(1

帅气称霸 2025-01-12 02:49:34

也许您应该使用静态布尔值,默认情况下为 false。一旦启动应用程序,您就将该值设置为 true。这样您就可以确保在使用应用程序时新闻仅下载一次。静态值在整个运行时“实时”存在。在 onRestart() 或 onResume() 上再次将该值设置为 false ;)

Maybe you should use a static boolean, which is false by default. As soon as your start the App you set the value to true. That's how you can make sure that while you are in your app the news are only downloaded once. Static values "live" through your entire runtime. Set the value to false again on onRestart() or onResume() ;)

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