2016-04-15 81 views
-4

我試圖添加一些介紹屏幕,只會運行應用程序第一次啓動後,它將直接加載登錄頁面 我使用以下指南來實現這第一次應用程序啓動介紹錯誤(viewpager)

SharedPreferences sp = getSharedPreferences(MyPrefs, 0); 
    if (sp.getBoolean("first", true)) { 
     SharedPreferences.Editor editor = sp.edit(); 
     /*editor.putBoolean("first", false);*/ 
     sp.edit().putBoolean("first", false).commit(); 
     editor.commit(); 
     Intent intent = new Intent(this, login.class); //call your ViewPager class 
     startActivity(intent); 
    } 

但應用程序將跳過開頭部分,並加載使用首次應用程序時的登錄頁面,並再次 啓動時加載的介紹頁我怎麼能扭轉這種 感謝

+0

你可以改變if(sp.getBoolean(「first」,true))if(sp.getBoolean(「first」,false))並且讓我知道發生了什麼:)我會解釋你爲什麼後來:)雖然我可以建議一個更好的方式來實現它:)它只是一個補丁到您的代碼:) –

+0

更改條件 –

+0

@SandeepBhandari感謝您的回覆,將其更改爲false確實有助於引入介紹屏幕,但在下次啓動應用程序時它們仍顯示出來。 – 7rocker

回答

0
SharedPreferences sp = getSharedPreferences(MyPrefs, 0); 
    if(sp.contains("first"){ 
     Intent intent = new Intent(this, login.class); //call your ViewPager class 
     startActivity(intent); 
    } 
    else{ 
     SharedPreferences.Editor editor = sp.edit(); 
     sp.edit().putBoolean("first", true).commit(); 
     editor.commit(); 
    } 

說明: 當您啓動您的應用程序首次變量「第一」將不會出現在共享偏好:)因爲你getBoolean指定的默認值的情況下,回到「第一」不發現是真的:)所有你的代碼是折騰:)

正確的方式來做到這一點:) 檢查首先,如果「第一」關鍵詞是否存在?如果不是這意味着你第一次啓動它顯示介紹屏幕:)但不要忘記首先輸入值真正的:)

因此,下一次啓動第一個關鍵將出現在共享首選項所以現在你知道你是第二次啓動:)你可以跳過介紹並啓動登錄活動:)

不要打擾你的自我與「第一」和所有:)的價值所有你需要知道的是如果這個鍵存在或不是所有:)

+0

非常感謝@Sandeep Bhandari現在的工作! – 7rocker

1

你在你的情況下有問題。試試這個代碼:

SharedPreferences sp = getSharedPreferences(MyPrefs, 0); 
if (sp.getBoolean("first", true)) { 
    sp.edit().putBoolean("first", false).apply(); 

    // Show Intro Activity 
} else { 
    Intent intent = new Intent(this, login.class); 
    startActivity(intent); 
} 

首先你檢查一下是否是第一次。如果是,那麼您將顯示介紹Activity。如果這不是第一次,那麼你顯示登錄Activity

注意我刪除了一些SharedPreferences代碼,其中一部分是多餘的。我也將commit()更改爲apply()

相關問題