Shared Preferences KEY_FIRST_RUN is not working in android 1.6?

前端 未结 1 1781
再見小時候
再見小時候 2021-01-23 15:54

I am using the following code to check the application is first time running or not

SharedPreferences pref = getPreferences(MODE_PRIVATE);
if (!pref.contains(KEY         


        
1条回答
  •  时光说笑
    2021-01-23 16:32

    The following works for me:

    private static String KEY_FIRST_RUN = "";
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        sharedPreferences = getPreferences(MODE_PRIVATE);
    
        if (!sharedPreferences.contains("KEY_FIRST_RUN")) {
            KEY_FIRST_RUN = "something";
            Log.d("First", "First run!");
        } else {
            Log.d("Second...", "Second run...!");
        }
    
        editor = sharedPreferences.edit();
        editor.putString("KEY_FIRST_RUN", KEY_FIRST_RUN);
        editor.commit();
    }
    

    0 讨论(0)
提交回复
热议问题