2015-07-03 126 views
1

我有一個屏幕,當我的應用程序加載時,我想第一次顯示。然後,如果退出或打開主屏幕,請再次打開該應用程序,我不希望啓動屏幕顯示。使用共享首選項顯示啓動屏幕一次

我試圖通過使用共享首選項來完成此操作。

當我運行該應用程序時,它顯示第一個屏幕。但是如果我退出並打開它,它仍然顯示相同的屏幕。

我試着通過默認設置Boolean爲真,因此它顯示屏幕。然後,它設置爲false被追着所以下一次它貫穿它會顯示main_activity.xml而不是splashscreen.xml

public class MainActivity extends AppCompatActivity { 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getSupportActionBar().hide(); 


     boolean firstTimeRun = getFirstTimeRun(); 

     if (firstTimeRun == true) { 
      firstTimeRun(); 
     } else { 
      run(); 
     } 

     storeFirstTimeRun(); 

    } 

    private boolean getFirstTimeRun() { 
     SharedPreferences prefs = getSharedPreferences("First Time Run Value", MODE_PRIVATE); 
     boolean firstTimeRun = prefs.getBoolean("firstRun", true); 
     return firstTimeRun; 
    } 

    private void storeFirstTimeRun() { 
     SharedPreferences prefs = getSharedPreferences("First Time Run Value", MODE_PRIVATE); 
     SharedPreferences.Editor editor = prefs.edit(); 
     editor.putBoolean("first Run", false); 
     editor.commit(); 
    } 


    private void firstTimeRun() { 
     setContentView(R.layout.splashscreen); 
    } 

    private void run() { 
     setContentView(R.layout.activity_main); 
     AdView mAdView = (AdView) findViewById(R.id.adView); 
     AdRequest adRequest = new AdRequest.Builder().build(); 
     mAdView.loadAd(adRequest); 
    } 


    public void CuFStudy(View view) { 
     Intent i = new Intent(this, Careunderfirestudy.class); 
     startActivity(i); 
    } 

    public void TFCStudy(View view) { 
     Intent i = new Intent(this, Tacticalfieldcarestudy.class); 
     startActivity(i); 
    } 

    public void TECStudy(View view) { 
     Intent i = new Intent(this, Tacticalevacuationcarestudy.class); 
     startActivity(i); 
    } 

    public void CuFQuiz(View view) { 
     Intent i = new Intent(this, Careunderfirequiz.class); 
     startActivity(i); 
    } 

    public void TFCQuiz(View view) { 
     Intent i = new Intent(this, Tacticalfieldcarequiz.class); 
     startActivity(i); 
    } 

    public void TECQuiz(View view) { 
     Intent i = new Intent(this, Tacticalevacuationcarequiz.class); 
     startActivity(i); 
    } 

} 

回答

2

兩個鍵是不同的,其用於檢索和存儲SharedPreferences價值。

使用firstRun關鍵在storeFirstTimeRun方法:

... 
editor.putBoolean("firstRun", false); 
editor.commit(); 
+0

nubbish傾向......謝謝你,它的工作。 – th3ramr0d

+0

@ th3ramr0d所以你應該接受的答案 –

+1

十分鐘計時器有點阻止。我定期檢查。停止向我施壓!!! 1 – th3ramr0d