My Android app starts on an activity named LoadingActivity. This activity loads a few files of my webserver. First it checks a timestamp in a text file on my server and checks if this timestamp is newer than one stored on the device. If it is, it loads the content of another file on my webserver and saves this to a file on the device.
When all this is done, I would like to go to another activity, MainActivity.
I do all this in the onWindowFocusChanged method like this:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
URL urlUpdated = null;
BufferedReader in = null;
try {
urlUpdated = new URL("http://someurl.com/timestamp.txt");
in = new BufferedReader(new InputStreamReader(urlUpdated.openStream()));
String updated = "";
String inputLine;
while ((inputLine = in.readLine()) != null)
updated = updated + inputLine;
in.close();
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
String lastUpdated = preferences.getString("lastUpdated", "");
if (!lastUpdated.equals(updated)) {
// Download JSON file (schedule)
String schedule = "";
URL urlSchedule;
urlSchedule = new URL("http://someurl.com/downloadMe.txt");
in = new BufferedReader(new InputStreamReader(urlSchedule.openStream()));
while ((inputLine = in.readLine()) != null)
schedule = schedule + inputLine;
Log.d(LOG_TAG, schedule);
// Write schedule to file
String PATH = "/data/data/com.codeinacup.NibeFestival/";
File file = new File(PATH + "Schedule.json");
FileOutputStream fileOutputStream = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream);
writer.write(schedule);
writer.close();
fileOutputStream.close();
// Update last updated preference
editor.putString("lastUpdated", updated);
editor.commit();
}
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
// Go to next MainActivity
Intent scheduleIntent = new Intent(this, MainActivity.class);
startActivity(scheduleIntent);
}
My problem is that when doing this, my LoadingActivity will be displayed but disappear again very fast and the screen will turn black for a couple of seconds and then MainActivity appears. Then, when the user press the back button, it will go back to MainActivity again. I found it obvious, that the back button should go back to LoadingActivity.
So my question is, how would I implement an activity for loading correctly? My idea of a loading activity is:
- Displayed when the application launches
- Downloads the files
- Goes to the next activity,
MainActivity - When at
MainActivityand pressing the back button, the application should quit
onWindowFocusChanged gets called when the Activity gains or loses focus. So you start your Activity and it calls onWindowFocusChanged, because it gains focus. When it’s finished loading your stuff it starts the MainActivity and loses focus again but executes the method again.
What I would do is create an AsyncTask which loads your stuff and executes the task in the MainActivity. And it would be nice if you show the user something, so that he knows the app is loading something. ProgressDialog
No need of 2 Activitys.
Hope it helps.
Regards
Dominic