How to save state of onClickListener?

痴心易碎 提交于 2019-12-02 10:33:26

问题


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 before they left the application or is was destroyed?


回答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

String usernameName = pref.getString(PREFS_USERNAME, "");
    String upassWord = pref.getString(PREFS_PASSWORD, "");

depending on the value you can set the state of button




回答2:


You could use SharedPreferences for that purpose.



来源:https://stackoverflow.com/questions/6514994/how-to-save-state-of-onclicklistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!