简体   繁体   中英

How to config SharedPreferences for the first run activity in the Android?

I have 3 Activity:

1- SplashScreensActivity

2- IntroActivity

3- MainActivity

I need to do, At First launch appliation start IntroActivity else MainActivity after SplashScreenActivity .

Edited:
I wrote this code but did not work and after showing Splash Screen, MainActivity Started and IntroActivity never start.

Plese let me to know where is the problem.

Intro.Java:

    SharedPreferences settings=getSharedPreferences("prefs", 0);
    final boolean firstRun=settings.getBoolean("firstRun",false);
    if(firstRun==false)
    {
        SharedPreferences.Editor editor=settings.edit();
        editor.putBoolean("firstRun",true);
        editor.commit();
        Intent i=new Intent(Intro.this,Intro.class);
        startActivity(i);
        finish();
    }
    else
    {
        Intent a=new Intent(Intro.this,MainActivity.class);
        startActivity(a);
        finish();
    }


SplashScreeActivity.Java:

public class SplashScreensActivity extends Activity {

private ImageView mLogo;
private TextView welcomeText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.activity_splash_screen);

    Typeface roboto_s = Typeface.createFromAsset(getAssets(), "Roboto-Thin.ttf");
    mLogo = (ImageView) findViewById(R.id.logo_sp);
    welcomeText = (TextView) findViewById(R.id.welcome_text);
    welcomeText.setTypeface(roboto_s);

    Animation animation2;{
        mLogo.setAlpha(1.0F);
        Animation anim = AnimationUtils.loadAnimation(this, R.anim.translate_top_to_center);
        mLogo.startAnimation(anim);
    }
    Animation animation3; {
        ObjectAnimator alphaAnimation = ObjectAnimator.ofFloat(welcomeText, "alpha", 0.0F, 1.0F);
        alphaAnimation.setStartDelay(700);
        alphaAnimation.setDuration(1200);
        alphaAnimation.start();
    }


    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        // TODO Auto-generated method stub
        finish();
        Intent next = new Intent(getBaseContext(), Intro.class);//
        startActivity(next);
        }
    }, 2000);
}

}

The problem was solved with a change in SplashScreenActivity.
So I 've edited the Handler codes in the SplashScreenActivity :

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
        public void run() {
            if (settings.getBoolean("isFirstRun", true)) {
                SharedPreferences.Editor editor = settings.edit();
                editor.putBoolean("isFirstRun", false);
                editor.commit();
                Intent next = new Intent(SplashScreensActivity.this, Intro.class);
                startActivity(next);
            } else {
                Intent next2 = new Intent(SplashScreensActivity.this, MainActivity.class);
                startActivity(next2);
                finish();
            }
        }
    }, 2000);
}

and remove firstRun method from Intro.Java
I hope to be useful

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM