【问题标题】:How to clear android cache onCreate如何清除android缓存onCreate
【发布时间】:2013-02-17 10:13:49
【问题描述】:

我是 Android 开发新手,我已经开始创建这个应用程序,我在其中读取 XML 提要并列出内容。但即使提要发生变化,我仍然会显示相同的数据。尽管一夜之间缓存似乎自行清除。我想在每次启动我的应用程序时强制阅读提要。我尝试了How to clear cache Android 此处描述的方法,但没有帮助。

我的猜测是我必须在我的主要活动中执行 onCreate(至少这是我迄今为止尝试过的)。

谁能指出我正确的方向?

提前致谢:-)

这里有一些代码sn-ps。

来自我的活动:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_game);
    deleteCache(this);
    new ReadLeagueXml().execute();
}

deleteCache 是我链接到的另一个线程的方法。

然后在我的 doInBackground 我有

InputStream inputStream = new URL(xmlURL).openStream();

if (inputStream != null) {
    list = parse(inputStream);
}

最后在我的 onPostExecute 中:

ListView matchListView = (ListView)findViewById(R.id.match_list);
List<String> matchArray = new ArrayList<String>();
for (int i = 0; i < matches.size(); i++) {
    matchArray.add(matches.get(i).HomeTeam + " - " + matches.get(i).AwayTeam);
}
matchArray.toArray();
matchesArrayAdapter = new MatchListAdapter(ListGameActivity.this, matches);
matchListView.setAdapter(matchesArrayAdapter);

【问题讨论】:

  • 您能发布更多数据吗?可能是一些代码和更多关于你的应用到底做什么的细节,比如数据分配给 ListView 的位置?
  • 添加了代码 sn-p。希望它有所帮助:-/

标签: android caching feed


【解决方案1】:

您的代码看起来非常好。此外,您不需要“清除缓存”。

现在回到问题,如果我正确理解您的问题,Feed 不会刷新。这是因为您在onCreate() 上下载了新数据。因此,当您第一次启动应用程序时,数据会被下载。然后用户转到另一个应用程序(请记住,Android 不会关闭您的活动,它只会暂停它)。所以当用户回来时,如果应用程序没有关闭,Android 会恢复它!所以你的onCreate() 不会被再次调用。

简而言之, 更新onResume() 中的数据,每次向用户显示您的活动时都会调用该数据。将new ReadLeagueXml().execute(); 移动到onResume(),每次用户打开应用程序时都会下载新数据。

您可能想查看 Android Activity 生命周期 here

【讨论】:

  • 这很有意义!但有一件事:onResume 是否与 OnCreate 同时执行?换句话说,我必须调用 ReadLeagueXml().execute();在 onCreate 和 onResume 中,还是在 onResume 中就足够了?
  • 没有。如果您看到 Android 生命周期,您的应用程序第一次运行时首先调用 onCreate(),然后调用 onResume()。所以你只需要在onResume()
猜你喜欢
  • 2011-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-25
  • 1970-01-01
相关资源
最近更新 更多