Skip to content

Instantly share code, notes, and snippets.

@guptasanchit90
Created June 7, 2016 09:32
Show Gist options
  • Select an option

  • Save guptasanchit90/40494c3cba89f23943a1990504dccb8a to your computer and use it in GitHub Desktop.

Select an option

Save guptasanchit90/40494c3cba89f23943a1990504dccb8a 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