0

When i install an android app ,on installation complete there are two options ,'Done' and 'Open' if i choose open to run installed app, and then press home button and click on app icon (First time click on app icon) then a splash activity is opened while the app is already running.The problem is , I don't want to call splash activity if the app is running already in background.

Activities flow: 1. splash screen extends Activity 2. Main Activity extends SherlockFragmentActivity

public class SplashScreen extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
     /****** Create Thread that will sleep for 5 seconds *************/        
    Thread background = new Thread() {
        public void run() {

            try {
                // Thread will sleep for 5 seconds
                sleep(5*1000);

                // After 5 seconds redirect to another intent
                Intent i=new Intent(getBaseContext(),MainActivity.class);
                startActivity(i);

                //Remove activity
                finish();

            } catch (Exception e) {

            }
        }
    };

    // start thread
    background.start();

}
@Override
protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);
}

}

1 Answers1

0

What you could do is the following:

private boolean mSplashShown;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    /****** Create Thread that will sleep for 5 seconds *************/
    Thread background = new Thread() {
        public void run() {
            try {
                // Thread will sleep for 5 seconds
                sleep(5 * 1000);

                // After 5 seconds redirect to another intent
                proceed();

                //Remove activity
                mSplashShown = true;
                finish();
            } catch (Exception e) {}
        }
    };

    // start thread
    if (mSplashShown) {
        proceed();
    } else {
        background.start();
    }
}

private void proceed() {
    final Intent i = new Intent(getBaseContext(), MainActivity.class);
    startActivity(i);
}

As long as the SplashActivity will be kept in memory, the mSplashShown will hold the appropriate value and act accordingly.

An alternative solution:

private static final String KEY_SHOW_SPLASH = "MyActivity.KEY_SHOW_SPLASH";

@Override
protected void onResume() {
    super.onResume();

    if (showSplash()) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setShowSplash(false);
                proceed();
                finish();
            }
        }, 2000);
    } else {
        proceed();
    }
}

private void proceed() {
    final Intent i = new Intent(this, MainActivity.class);
    startActivity(i);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    setShowSplash(true);
}

private void setShowSplash(final boolean show) {
    final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    preferences.edit().putBoolean(KEY_SHOW_SPLASH, show).commit();
}

private boolean showSplash() {
    return PreferenceManager.getDefaultSharedPreferences(this).getBoolean(KEY_SHOW_SPLASH, true);
}
Tadej
  • 2,901
  • 1
  • 17
  • 23