Skip to content

Instantly share code, notes, and snippets.

@takahirom
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takahirom/164c56c9f10eb59c58d5 to your computer and use it in GitHub Desktop.
Save takahirom/164c56c9f10eb59c58d5 to your computer and use it in GitHub Desktop.
初回のみ○○したいを実現するクラス
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