Remember click in Android

365 views Asked by At

I have created an activity that should be displayed only once. In this Activity, I added a button, and click its close and will not reopen anymore when the application is started again. I thought of using SharedPreferences, but I do not get the desired result.

I memorize a data in Shared:

@Override
public void onClick(View v) {
    switch (v.getId()) {

    case R.id.button1:
        SharedPreferences userDetails = getSharedPreferences("entrato", MODE_PRIVATE);
        Editor edit = userDetails.edit();
        edit.clear();
        edit.putString( "entrato", "entra" );
        edit.commit();
        this.finish();
        break;  

}
}

then, in OnCreate() of the previous activity call the Shared and if the data is present do not open the activity:

    //remember click
    SharedPreferences userDetails = getSharedPreferences("entrato", MODE_PRIVATE);
    String  valore = userDetails.getString("entrato", "");
    if (valore.equals("entra")){
        return;
    }else{
    Intent intent = null;
    intent = new Intent(this, Benvenuto.class); 
    startActivity(intent);
3

There are 3 answers

0
Mahdi On BEST ANSWER

create share preference class like this:

public class MyPreference {

    private static final String APP_SHARED_PREFS = "myPref"; 
    private SharedPreferences   appSharedPrefs;
    private Editor              prefsEditor;


    public MyPreference(Context context) {
        this.appSharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE);
        this.prefsEditor = appSharedPrefs.edit();
    }



    public boolean loadServices(String servicName) {
        return appSharedPrefs.getBoolean(servicName, false);
    }



    public void saveServices(String servicName, boolean isActivated) {
        prefsEditor.putBoolean(servicName, isActivated);
        prefsEditor.commit();
    }

}

then On Oncreate activity check if service exist so wont show and exit.

 public static MyPreference  myPref = new MyPreference(this);

if(myPref.loadService("this is not first time")){

thisActivity.finish();

}else {

// so it's  first time and you can do what ever you want to do

}

and go on you click events and save service like this.

 public static MyPreference  myPref = new MyPreference(this);
 myPref.saveService("this is not first time");
0
Vinayak Parab On

I have Four activities

  1. Splash
  2. Have 2 Buttons And 1 CheckBox ( Remember Me) 3 & 4. Another Activity

If I check the remember me option using checkbox, then I have to remember the button I have clicked and then start the 3rd or 4th activity directly whenever next start.

0
Jitender Dev On

Try this code instead

// save string 
static public boolean setPreference(Context c, String value, String key) {
    SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 0);
    settings = c.getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString(key, value);
    return editor.commit();
}

// retrieve String
static public String getPreference(Context c, String key) {
    SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 0);
    settings = c.getSharedPreferences(PREFS_NAME, 0);
    String value = settings.getString(key, "");
    return value;
}