Skip to content Skip to sidebar Skip to footer

How To Save State Of Onclicklistener?

I have a program that implements several onClickListeners. So as the user progresses through the button clicks. Is there anyway to save which onClick listener the user was on befor

Solution 1:

Use sharedpreference to achieve this. store the button name and its value whenever you click on any button.

example

SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);   

        passwordInString = password.getText().toString();
        userNameInString = username.getText().toString();
        getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
        .edit()
        .putString(PREFS_USERNAME, passwordInString)
        .putString(PREFS_PASSWORD, userNameInString)
        .commit();

and in oncreate() always get the state of button using the following code Sample example

StringusernameName= pref.getString(PREFS_USERNAME, "");
    StringupassWord= pref.getString(PREFS_PASSWORD, "");

depending on the value you can set the state of button

Solution 2:

You could use SharedPreferences for that purpose.

Post a Comment for "How To Save State Of Onclicklistener?"