2

I have a test application that changes the image on the button. My test app should have 3 functions. 1. When you click on the image button, change the background image of the button on from image 0 to image 1. (I can do it) When you click on the save button, it should be recorded which image stands on the image button. (I can't do this) When you restart the application, when you click on the load button, the image should appear on the image button, which was saved using the save button

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btnSave:
            saveText();
            break;
        case R.id.btnLoad:
            loadText();
            break;
        case R.id.cardButton:
            cardButton.setImageResource(R.drawable.n01);
            cardButton.setTag(R.drawable.n01);
            Integer resource = (Integer)cardButton.getTag();
        default:
            break;
    }
}
void saveText() {
    sPref = getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor ed = sPref.edit();
    "some code"
    ed.apply();
    Toast.makeText(this, "Text saved", Toast.LENGTH_SHORT).show();
}
void loadText() {
    sPref = getPreferences(MODE_PRIVATE);
    "some code"
    Toast.makeText(this, "Text loaded", Toast.LENGTH_SHORT).show();
}
Guahoo
  • 19
  • 7

1 Answers1

0

There are multiple context to Look at this scenario.

  1. if we are using single preference in an activity then we use getPreferences() method to get the shared Preferences else we will go for the getSharedPreferences() method. to look more please refer: https://developer.android.com/training/data-storage/shared-preferences
  2. to save into shared Preferences what should we use either editor.apply() method or editor.commit() method to look more please refer: What's the difference between commit() and apply() in Shared Preference. Here a would like to suggest you to use editor.commit() method so that you could get the response of saving the value into shared preferences.

and one more thing is that if you want to save the imageId in shared Preferences for that you firstly you have to get the image resource id that save it in Preference. like:

 ImageView imageView = (ImageView)findViewById(R.id..imageName);
int imageid = getResourceId(this,"profiles","drawable", getPackageName());
//save this imageid into your shareedPreferences and getback when you return.
// set this image id into the imageVire
imageView.setImageResouce(imageid);

public static int gerResourceId(context context, String variableName, String resourceName, String pakageName) throws RuntimeException{
     return context.getResources().getIdentifier( variableName, resourceName, pakageName);
}

if any other help required so most welcome and i hope to doing correct flow of shared preferences this problem will resolve if and only if rest of the code is working fine. thanks.

singh.indolia
  • 1,292
  • 13
  • 26
  • My problem is that I do not know what form to write the image. "putString", "putInt" or something else – Guahoo Oct 11 '18 at 05:32
  • If u want to save image path then u can use putString(), it will save your updated image path and while you will return back then through that image path u can get image. second way to save the resource id, you have to use putInt() and through it you can get back your image. – singh.indolia Oct 15 '18 at 04:33