0
votes

I want to launch my Splash Screen only when the application is launched for the First time. My Splash Screen is linked with another screen which also comes only for the first time. My code for Splash Screen is:

public class SplashPage extends Activity{
private static int startPage = 7000;
SharedPreferences prefs = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splashpage);

    prefs = getSharedPreferences("com.tech.spam", MODE_PRIVATE);

        }
@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
     if (prefs.getBoolean("firstrun", true)) {
            calll();
        }
     if (prefs.getBoolean("firstrun", false)) {
         finish();
         startActivity(new Intent(SplashPage.this,MainActivity.class));
        }
            //prefs.edit().putBoolean("firstrun", false).commit();
            //finish();

}
private void calll() {
    // TODO Auto-generated method stub
    Animation anim = AnimationUtils.loadAnimation(this, R.anim.move);
    ImageView iv = (ImageView) findViewById(R.id.imageView1);
    iv.startAnimation(anim);



    Animation anim1 = AnimationUtils.loadAnimation(this, R.anim.move);
    TextView ivvv = (TextView) findViewById(R.id.textView1);
    ivvv.startAnimation(anim1); 



       new Handler().postDelayed(new Runnable() {          
            public void run() {    
                Intent intent = new Intent(SplashPage.this, SplashPageTutorial.class);

                startActivity(intent);

                overridePendingTransition(R.anim.fade, R.anim.hold);
                finish();
                }
            },     
        startPage);

}

Now what i wanted to do that here is that Splash Runs for 7 seconds for the first time app launched and then next screen open which is linked with splash after 7 seconds and then after that second screen my main Activity starts. Now what i want to do that is that when I open the application for the first time then only Main Activity is launch (splash screen and the screen linked with splash are now gone)

I use this code so that when ever app open again the boolean returns false and it start Main Activity but it does not happening

if (prefs.getBoolean("firstrun", true)) {
            calll();
        }
     if (prefs.getBoolean("firstrun", false)) {
         finish();
         startActivity(new Intent(SplashPage.this,MainActivity.class));
        }
            //prefs.edit().putBoolean("firstrun", false).commit();
            //finish();

Please help what should i do

My Manifest is this:

<application android:icon="@drawable/appicon"
    android:label="@string/app_name"
    android:theme="@style/AppBaseTheme">

    <activity android:name="com.tech.spam.SplashPage"

        android:label="@string/app_name">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
    <activity
        android:name="com.tech.spam.MainActivity"

         >

    </activity>    
    <activity
        android:name="com.tech.spam.SplashPageTutorial"
        android:theme="@style/FullscreenTheme"
        >
    </activity>
1

1 Answers

4
votes

One easy way is to have a shared preference variable. On launching the splash screen set this variable to true. And before launching the activity check if this variable is false and only launch if it is false.

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    SharedPreferences settings=getSharedPreferences("prefs",0);
    boolean firstRun=settings.getBoolean("firstRun",false);
    if(firstRun==false)//if running for first time
    //Splash will load for first time
    {
        SharedPreferences.Editor editor=settings.edit();
        editor.putBoolean("firstRun",true);
        editor.commit();
        Intent i=new Intent(check.this,Splash.class);
        startActivity(i);
        finish();
    }
    else
    {

        Intent a=new Intent(check.this,Main.class);
        startActivity(a);  // Launch next activity
        finish();
    }
}

}