2015-10-27 2 views
0

У меня есть 3 Активность:Как настроить SharedPreferences для первого запуска активности в Android?

1- SplashScreensActivity

2- IntroActivity

3- MainActivity

мне нужно сделать, при первом запуске appliation начать IntroActivity еще MainActivity после SplashScreenActivity.

Отредактировано:
Я написал этот код, но не работает, и после показа экрана заставки, MainActivity работы и IntroActivity никогда не начинать.

Plese дал мне знать, где проблема.

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); 
} 

}

+0

вы можете добавить вывод журнала? –

+0

Да, пожалуйста, подождите минутку –

+0

кстати. это код в SplashScreensActivity? –

ответ

0

Проблема была решена с изменением SplashScreenActivity.
Так что я ве редактировал Handler кодов в 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); 
} 

и удалить FirstRun метода из Intro.Java
Я надеюсь быть полезным

Смежные вопросы