I'm new to Android development and I've started creating this app where I read an XML feed and list the contents. But even though the feed changes I still get the same data displayed. Although over night the cache seemed to clear itself. I would like to force reading of the feed each time I start my app. I tried the methods described here How to clear cache Android but it didn't help.
My guess is that I would have to do it onCreate in my main activity (at least that's what I have tried so far).
Can anyone point me in the right direction?
Thanks in advance :-)
Here are some code snippets.
From my activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_game);
deleteCache(this);
new ReadLeagueXml().execute();
}
deleteCache is the method from the other thread I linked to.
Then in my doInBackground I have
InputStream inputStream = new URL(xmlURL).openStream();
if (inputStream != null) {
list = parse(inputStream);
}
And finally in my 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);
Your code seems perfectly Fine. Also you do not need to "clear cache".
Now back to the question, If i understand your question correctly, The feed is not getting refreshed. This is because you download the new data on onCreate()
. So when you launch the app for the first time the data is downloaded. Then the user goes to another application (remember Android will not close your activity, it will simply pause it). So when the user comes back, If the application is not closed Android will resume it! So your onCreate()
will not be called again.
In short,
Update the data in onResume()
which is called everytime your activity is shown to the user. Move the new ReadLeagueXml().execute();
to onResume()
and new data will be downloaded every time the user opens the app.
You might want to look at the Android Activity Life Cycle here