如何清除onCreate上的android缓存

时间:2013-02-17 10:13:49

标签: android caching feed

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

我的猜测是我必须在我的主要活动中创建它(至少那是我迄今为止尝试过的)。

有人能指出我正确的方向吗?

提前致谢: - )

以下是一些代码段。

从我的活动:

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);

1 个答案:

答案 0 :(得分:2)

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

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

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

您可能需要查看Android活动生命周期here