Last active
August 29, 2015 14:04
-
-
Save takahirom/164c56c9f10eb59c58d5 to your computer and use it in GitHub Desktop.
初回のみ○○したいを実現するクラス
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.preference.PreferenceManager; | |
/** | |
* 初めて使うときは〜というのを実現するためのクラス | |
* Created by tmen on 2014/07/17. | |
*/ | |
public class FirstTimePreference { | |
private final String mKey; | |
public interface FirstTimePreferenceListener{ | |
/** | |
* 初めてこの鍵が使われた時に使われる | |
* @return 初めてが終わったかどうか(falseを返すと次回もこのメソッドが呼ばれる) | |
*/ | |
boolean onFirstTime(); | |
void onNotFirstTime(); | |
} | |
public FirstTimePreference(String key){ | |
mKey = key; | |
} | |
public void action(Context context,FirstTimePreferenceListener listener){ | |
SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); | |
if(defaultSharedPreferences.getBoolean(mKey,true)){ | |
if(listener.onFirstTime()){ | |
defaultSharedPreferences.edit().putBoolean(mKey,false).apply(); | |
} | |
}else{ | |
listener.onNotFirstTime(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment