Reputation: 509
I'm developing an Android app. The app should show a Splash Screen at startup while checking if a file is updated. If the file is not updated, it launches an Async Task to update the file. The problem is, the image of the Splash Screen only shows when the file actually needs updating. Else, a black screen shows while performing the check.
My SplashScreen activity:
public class SplashActivity extends Activity
{
private final static String placesFile = "places";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash);
}
@Override
protected void onResume()
{
super.onResume();
if(!isFileUpdated()){
new PlacesService(this).execute();
}else{
intentAndFinish();
}
}
private void intentAndFinish() {
finish();
Intent mainIntent = new Intent(this, MainActivity.class);
startActivity(mainIntent);
}
/**
*
* @return false if Places Data is too old
*/
private boolean isFileUpdated() {
int daysOld = 0;
File f = new File(this.getFilesDir().getAbsolutePath() +"/"+placesFile);
if(f.exists()){
System.out.println("existe");
}
Date d = new Date();
Date currentDate = new Date(System.currentTimeMillis());
d.setTime(f.lastModified());
if(currentDate.compareTo(d)>0)
daysOld = determineDifferenceInDays(d, currentDate);
return daysOld < Consts.PLACES_DAYS_OLD_QTY_PERMITTED?true:false;
}
private static int determineDifferenceInDays(Date date1, Date date2) {
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(date1);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(date2);
long diffInMillis = calendar2.getTimeInMillis() - calendar1.getTimeInMillis();
return (int) (diffInMillis / (24* 1000 * 60 * 60));
}
public void onResultFromAsyncTask(boolean finished) {
if(finished){
intentAndFinish();
}
}
}
activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:src="@drawable/splash_es"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Upvotes: 0
Views: 411
Reputation: 1001
You call to isFileUpdated() takes time put it in background You asyntask should be something like this
public class PlacesService extends Asynctask<Void,Void,Void>{
boolean flag=false
public Void doInBackground(){
flag=isFileUpdated()
if (!flag){
return
}else {
// Do your file update
}
return
}
public onPostExcecute(){
intentAndFinish();
}
}
Upvotes: 0
Reputation: 3064
If your file is updated You are closing your activity in onResume and switching to main activity and the process will take fraction of milliseconds to do that..so it is very much possible you will never see your spash screen..If you want you can use
private void intentAndFinish() {
Thread.sleep(10000)
finish();
Intent mainIntent = new Intent(this, MainActivity.class);
startActivity(mainIntent);
}
Just to catch up with your splashScreen.
See @bruno 's post...We should use a handler
Upvotes: 0
Reputation: 1737
You are killing your Splash before it be on the Screen. Because you are killing it inside the onResume method. Look this piece of documentation:
The foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause(). During this time the activity is in front of all other activities and interacting with the user. An activity can frequently go between the resumed and paused states -- for example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered -- so the code in these methods should be fairly lightweight.
You can use a handler, it is more elegant than Thread to solve this problem, but the main ideia is the same of @sheetal.
On onCreate method:
handler = new Handler();
On onResume method:
handler.postDelayed(new Runnable() {
public void run() {
if(!isFileUpdated()){
new PlacesService(this).execute();
}else{
intentAndFinish();
}
}
}, 1000);
Upvotes: 2