2013-10-07 69 views
0

我在我的應用程序中有兩個活動。第一次活動SplashScreen和其他SplashActivity。我在SplashScreen中有sharedPreferences,但我想在SplashActivity中設置此值的真值。我認爲如果可以在SplashActivity中創建一個只運行一次的方法,即該方法比較boolean的值SplashScreen(如開始時這是錯誤的)。在第一次將其設置爲true之後,此方法將被跳過。我已經嘗試了很多,但沒有成功。如何在其他活動中設置sharedPreferences的值?

SplashScreen-

public class SplashScreen extends Activity 
{ 
    /** Called when the activity is first created. */ 
    TimerTask task; 
    SharedPreferences pref; 
    SharedPreferences.Editor ed; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.splash_screen); 
     pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE); 
     Log.v("","onCreate is calling"); 
     if(pref.getBoolean("activity_executed", false)) 
     { 
      Log.v("","Before if called"); 
      setContentView(R.layout.splash_screen); 
      Log.v("","after if called"); 
      new Handler().postDelayed(csRunnable1, 5000); 
     } 
     else 
     { 
      new Handler().postDelayed(csRunnable2, 5000); 
     } 
    } 

    Runnable csRunnable1=new Runnable() 
    {  
     @Override 
     public void run() 
     { 
      Intent intent = new Intent(SplashScreen.this, SplashActivity.class); 
       startActivity(intent); 
       finish(); 

     } 
    }; 

    Runnable csRunnable2=new Runnable() 
    {  
     @Override 
     public void run() 
     { 
      Intent intent = new Intent(SplashScreen.this, LoginActivity.class); 
       startActivity(intent); 
       finish(); 

     } 
    }; 
} 

SplashActivity-

public class SplashActivity extends Activity implements OnClickListener 
{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.welcome); 
     ////////////////////////////////////////////////////////////////////// 
     //////// GAME MENU ///////////////////////////////////////////////// 
     Button playBtn = (Button) findViewById(R.id.playBtn); 
     playBtn.setOnClickListener(this); 
     Button settingsBtn = (Button) findViewById(R.id.settingsBtn); 
     settingsBtn.setOnClickListener(this); 
     Button rulesBtn = (Button) findViewById(R.id.rulesBtn); 
     rulesBtn.setOnClickListener(this); 
     Button exitBtn = (Button) findViewById(R.id.exitBtn); 
     exitBtn.setOnClickListener(this); 
    } 


    /** 
    * Listener for game menu 
    */ 
    @Override 
    public void onClick(View v) { 
     Intent i; 

     switch (v.getId()){ 
     case R.id.playBtn : 
      //once logged in, load the main page 
      //Log.d("LOGIN", "User has started the game"); 
      //Get Question set // 
      List<Question> questions = getQuestionSetFromDb(); 
      //Initialise Game with retrieved question set /// 
      GamePlay c = new GamePlay(); 
      c.setQuestions(questions); 
      c.setNumRounds(getNumQuestions()); 
      ((CYKApplication)getApplication()).setCurrentGame(c); 
      //Start Game Now.. // 
      i = new Intent(this, QuestionActivity.class); 
      startActivityForResult(i, Constants.PLAYBUTTON); 
      finish(); 
      break; 

     case R.id.rulesBtn : 
      i = new Intent(this, RulesActivity.class); 
      startActivityForResult(i, Constants.RULESBUTTON); 
      finish(); 
      break; 

     case R.id.settingsBtn : 
      i = new Intent(this, SettingsActivity.class); 
      startActivityForResult(i, Constants.SETTINGSBUTTON); 
      finish(); 
      break; 

     case R.id.exitBtn : 
      finish(); 
      break; 
     } 

    } 

我知道這是我需要SplashActivity編輯線,但每當我添加此我的應用程序崩潰。

ed = pref.edit(); 
ed.putBoolean("activity_executed", true); 
ed.commit(); 
+0

如果這點是不是需要重新登錄,那麼就不要你真正想要的「LoginActivity」還是我誤解了一些東西? – codeMagic

+0

是的你是對的我想跳過LoginActivity當我第二次運行應用程序。 –

+0

,如果我在SplashActivity中設置值爲true,則表示用戶登錄一次,那就是我想要的。 –

回答

1

也稱,這可能是你正在做的事情是錯誤的。你可以簡單地這樣做閃屏

if(pref.getBoolean("activity_executed", false)) 
    { 
     Log.v("","Before if called"); 
     setContentView(R.layout.splash_screen); 
     Log.v("","after if called"); 
     new Handler().postDelayed(csRunnable1, 5000); 
     pref.edit().putBoolean("activity_executed", true).commit(); 
    } 

或在您的splashactivity OnCreate中調用這個

getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE).edit().putBoolean("activity_executed", true).commit(); 
+0

能否請你解釋一下這段代碼的含義。 pref.edit()。putBoolean(「activity_executed」,true).commit(); –

+0

這是你寫的相同的代碼..只是在一行 – stinepike

+0

好吧,我必須寫在splashActivity的代碼? –

0

在SplashActivity

onCreate(...){ 
.... 
SharedPreferences pref; 
SharedPreferences.Editor ed; 
pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE); 
ed = pref.edit(); 
ed.putBoolean("activity_executed", true); 
ed.commit(); 
.... 
} 
+0

好的,在splashactivity我要添加此代碼? –

+0

看到我的更新放在哪裏 –

相關問題