Skip to content

Instantly share code, notes, and snippets.

@pyadav
Forked from guptasanchit90/SplashScreen.java
Created August 8, 2016 03:36
Show Gist options
  • Save pyadav/9cc238e05248abd5b515174beb832ce3 to your computer and use it in GitHub Desktop.
Save pyadav/9cc238e05248abd5b515174beb832ce3 to your computer and use it in GitHub Desktop.
Simple Splash screen
public class SplashScreen extends Activity {
private Handler mHandler;
private Runnable myRunnable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Just create simple XML layout with i.e a single ImageView or a custom layout
setContentView(R.layout.splash_screen_layout);
mHandler = new Handler();
myRunnable = new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashScreen.this, MainActivity.class);
startActivity(intent);
finish();
}
};
}
@Override
public void onBackPressed() {
// Remove callback on back press
if (mHandler != null && myRunnable != null) {
mHandler.removeCallbacks(myRunnable);
}
super.onBackPressed();
}
@Override
protected void onPause() {
// Remove callback on pause
if (mHandler != null && myRunnable != null) {
mHandler.removeCallbacks(myRunnable);
}
super.onPause();
}
@Override
protected void onResume() {
// Attach and start callback with delay on resume
if (mHandler != null && myRunnable != null) {
mHandler.postDelayed(myRunnable, ConstantValues.SPLASH_TIME_OUT);
}
super.onResume();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment