Skip to content Skip to sidebar Skip to footer

How To Save A Result In Android?

I'm a rookie in android programming. I have a small problem. When I click a ImageView, I make that ImageView invisible and set a Button to visible. My problem is that how do you sa

Solution 1:

Use SharedPreferences. here is a good tutorial on how to use them. example

But basically you are good to go by adding this code to your Activity

privateboolean isVisible;
@OverridepublicvoidonCreate(Bundle myBundle){
          super.onCreate(myBundle);
 isVisible = getPreferences(MODE_PRIVATE).getBoolean("visible", true);
  .... your code
  if (isVisible){
    // show ImageView
 } else {
        //don't
 }
}
}
publicvoidonPause(){
       if(isFinishing()){
         getPreferences(MODE_PRIVATE)
        .edit().
        putBoolean("visible", isVisible).commit();
 }
}

Solution 2:

Use a shared preference to save the state, i.e. say in your case a boolean value to indicate whether imageview was visible or not when you exit the app. When you launch the app, use this value and accordingly perform the action.

For usage of shared preference, How to use SharedPreferences in Android to store, fetch and edit values

Solution 3:

you can store the state in shared preference when you leave your app onPause() or on the click event and can get result back on onCreate() method from that preferences

To store data in shared preference(in OnPause() or on the click event):

SharedPreferencesprefs= getSharedPreferences("yourPrefName", MODE_PRIVATE);            
SharedPreferences.Editoreditor= prefs.edit();
// save values
editor.putBoolean("isButtonVisible", true);
editor.commit();

To get data from sharedPrefs(in onCreate()):

SharedPreferencesprefs= getSharedPreferences("yourPrefName", MODE_PRIVATE);
 booleanbtnstatus= prefs.getBoolean(Constants.IS_LOGIN, false);
 if (btnstatus) {
       //put the code to show button and hide imageview
 }

Post a Comment for "How To Save A Result In Android?"