0

I want my splash screen to be shown only on first start. I know that I have to go through shared preferences but how? This is my splash:

public class Splash extends Activity {

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.splash);

        //animation
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        Animation alpha = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.fade_in);
        imageView.startAnimation(alpha);
        alpha.setAnimationListener((new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                // TODO Start your activity here.
                Intent mainIntent = new Intent(Splash.this, MainActivity.class);
                Splash.this.startActivity(mainIntent);
                Splash.this.finish();
            }
        }));
    }
}
Floern
  • 33,559
  • 24
  • 104
  • 119
Daxiv1
  • 9
  • 6

2 Answers2

1

Try This:

SharedPreferences mPrefs;
final boolean mLoginSession;
 public static final String PREFER_NAME = "MyPrefsFile";

//Inside onCreate
   mPrefs = getSharedPreferences(PREFER_NAME, MODE_PRIVATE);
  mLoginSession = mPrefs.getBoolean(PREFER_NAME, false);



if (mLoginSession) {
    Intent gotoMain = new Intent(Splash.this, MainActivity.class);
     startActivity(gotoMain);
     finish();
     } else {
      //Show Splash Data here and Change the shared value of "PREFER_NAME"

      SharedPreferences.Editor editor = mPrefs.edit();
            editor.putBoolean(PREFER_NAME, true);
            editor.commit();
      }
Rakshit Nawani
  • 2,604
  • 14
  • 27
0

Try like this.

public class Splash extends Activity {

    SharedPreferences sharedpreferences;
    boolean splash;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        sharedpreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
        splash = sharedpreferences.getBoolean("Splash", false);
        if (splash == true) {
            Intent mainIntent = new Intent(Splash.this, MainActivity.class);
            startActivity(mainIntent);
        }
        else
        {
            setContentView(R.layout.splash);

            //animation
            ImageView imageView = (ImageView) findViewById(R.id.imageView);
            Animation alpha = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.fade_in);

            imageView.startAnimation(alpha);

            alpha.setAnimationListener((new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    // TODO Start your activity here.
                    SharedPreferences.Editor editor = sharedpreferences.edit();
                    editor.putBoolean("Splash", true);
                    Intent mainIntent = new Intent(Splash.this, MainActivity.class);
                    Splash.this.startActivity(mainIntent);
                    Splash.this.finish();
                }
            }));
        }
    }
}
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
  • sharedpreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE); – Daxiv1 Mar 15 '16 at 09:41
  • although i have set animation duration to 1 second, it does not finish & it stays at splash and main activity is not started. why?? – Daxiv1 Mar 15 '16 at 09:42
  • right answer but its not recommended ! in this case you have to load 2 screens the loading time and the screen blinking when starting Activity A and closing it then start the second one theres another way to handle this case by using Application class – Hamza Awwad Mar 15 '16 at 09:55
  • @HamzaAwwad What do you mean by 2 screens? It is up to Daxiv1, where he want to redirect. I have just shown a way to redirect in case of returning user. – Jay Rathod Mar 15 '16 at 10:00
  • I know but just to share my knowledge anyway I'm gonna post an example for any one wants. – Hamza Awwad Mar 15 '16 at 10:05
  • @HamzaAwwad can i see your example, too? – Daxiv1 Mar 15 '16 at 10:16