0

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 save this? For eg, I click the ImageView, Button shows up and ImageView disappears. And I exit the app and enter back into that same activity and I want that Button to remain there. How do I go about doing that?

Thanks!

WhiplashOne
  • 321
  • 1
  • 4
  • 11

3 Answers3

2

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

private boolean isVisible;
@Override
public void onCreate(Bundle myBundle){
          super.onCreate(myBundle);
 isVisible = getPreferences(MODE_PRIVATE).getBoolean("visible", true);
  .... your code
  if (isVisible){
    // show ImageView
 } else {
        //don't
 }
}
}
public void onPause(){
       if(isFinishing()){
         getPreferences(MODE_PRIVATE)
        .edit().
        putBoolean("visible", isVisible).commit();
 }
}
Mr.Me
  • 9,192
  • 5
  • 39
  • 51
0

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

Community
  • 1
  • 1
sat
  • 40,138
  • 28
  • 93
  • 102
0

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):

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

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

 SharedPreferences prefs = getSharedPreferences("yourPrefName", MODE_PRIVATE);
 boolean btnstatus = prefs.getBoolean(Constants.IS_LOGIN, false);
 if (btnstatus) {
       //put the code to show button and hide imageview
 }
Akbari Dipali
  • 2,173
  • 1
  • 20
  • 26
  • Thanks for that quick reply! If you don't mind, can give me like a sample code please? – WhiplashOne Mar 20 '13 at 17:08
  • You should not depend on onDestroy() ever being called, because it may not be. Mr. Me has done it correctly, using onPause(). – j__m Mar 20 '13 at 17:11
  • @user1853479 In question it is mentioned that `when I exits the app` so I have suggested to use onDestroy. If it is that activity goes in background, then we can use onPause() – Akbari Dipali Mar 20 '13 at 17:17
  • @AkbariDipali your solution `does not work` Android does not guarantee that you will ever be notified before your process is terminated. Therefore you must use onPause(). – j__m Mar 20 '13 at 17:44
  • @user1853479 I have only written answer as per the question, otherwise we can also use `onPause`, `onSaveInstanceState` as per our requirement, we can also store data on button or imageview click if we want. But for Your satisfaction, I am editing the ans – Akbari Dipali Mar 20 '13 at 17:57