javaandroidandroid-studioimageviewonsaveinstancestate

Saving ImageView background state


I have an ImageView ImageView1, whose background can be set by the user. How can I save the state of that background, so that when the user closes the app and reopens it, the background of ImageView1 is still the same?

Is it possible with onSaveInstanceState or do I have to write something different, and if so: How can I do this?

Right now I have:

public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("ImageView1", imageView1);

but imageView1 gets red underline, even though declared before to id of ImageView


Solution

  • You can use shared preferences:

    Check documentation for more: https://developer.android.com/training/data-storage/shared-preferences