SplashActivity.java:
public class SplashActivity extends FragmentActivity {
// create object of progressbar
ProgressBar prgLoading;
// set variable for progress bar to 0
int progress = 0;
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_splash);
Animation slide = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_in_left);
final ImageView splashImageView = (ImageView) findViewById(R.id.splashImageView);
splashImageView.setBackgroundResource(R.anim.splash);
final AnimationDrawable frameAnimation = (AnimationDrawable) splashImageView
.getBackground();
view.startAnimation(slide);
Thread timer = new Thread() {
public void run() {
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent splash = new Intent(SplashActivity.this,
HomeActivity.class);
startActivity(splash);
}
}
};
timer.start();
splashImageView.post(new Runnable() {
@Override
public void run() {
frameAnimation.start();
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
layout_splash.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@android:id/tabs" >
<ImageView
android:id="@+id/splashImageView"
android:background="@drawable/spl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</FrameLayout>
</RelativeLayout>
</LinearLayout>
In res/anim/slide_in_left.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate android:duration="1000" android:fromXDelta="0%" android:toXDelta="-100%"/>
<alpha android:duration="1000" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>
In res/anim/splash.xml:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/flaganim"
android:oneshot="false"
>
<item android:drawable="@drawable/spl1" android:duration="2000" />
<item android:drawable="@drawable/spl2" android:duration="2000" />
<item android:drawable="@drawable/spl3" android:duration="2000" />
</animation-list>
I have removed all unnecessary portions of your code. Additionally I improved it a bit, for example I use a Timer
instead of Thread.sleep()
and that weird finish()
in onPause()
can be replaced with Intent
flags. I also removed the call to your animation drawable. With the slide animation I assume you aren't going to need that anymore.
public class SplashActivity extends FragmentActivity {
private final TimerTask spashScreenFinished = new TimerTask() {
@Override
public void run() {
Intent splash = new Intent(SplashActivity.this, HomeActivity.class);
// We set these flags so the user cannot return to the SplashScreen
splash.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(splash);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_splash);
// We get the ImageView and set the background. (If possible do this in XML instead of code)
final ImageView splashImageView = (ImageView) findViewById(R.id.splashImageView);
splashImageView.setBackgroundResource(R.anim.splash);
// We load the slide animation and apply it to the ImageView
Animation slide = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_in_left);
splashImageView.startAnimation(slide);
// We use a Timer to schedule a TimerTask for 5 seconds in the future!
Timer timer = new Timer();
timer.schedule(this.spashScreenFinished, 5000);
}
}
Also: Don't use splash screens! They are always a sign of a badly designed app. If your app needs to load that much on startup then you are doing something wrong. And if it doesn't need to load anything than there is no reason to keep the user waiting for 5 seconds. A good app should start instantly and be usable as quick as possible.