0

I put an imageview to load an image of a url, it is when I click on a button it passes to the next url, thus changing the image. the problem is that when I rotate the screen it returns to the first image displayed. I tried to do an if with incremented counter but it deletes the variable and returns to the first one again. someone knows how to save the value of the "next" variable so when the screen rotates it keeps the value saved, or knows another way to keep the last image saved. api picasso

code complete

private SmartImageView smartImage;

private Button btn;
private int proxima = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_redacao_nota_1000);

     if (proxima == 0) {
         smartImage = (SmartImageView) findViewById(R.id.meuSmartImage);
         smartImage.setImageUrl("http://gabrielmartins70.000webhostapp.com/bao.png");
         proxima++;

     }

    btn = (Button) findViewById(R.id.button18);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (proxima == 1) {
                smartImage.setImageUrl("http://gabrielmartins70.000webhostapp.com/2.png");
            }
        }
    });


}}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
gabriel
  • 37
  • 2
  • 10
  • 1
    Possible duplicate of [Saving Android Activity state using Save Instance State](https://stackoverflow.com/questions/151777/saving-android-activity-state-using-save-instance-state) – Ben P. Dec 14 '17 at 21:55
  • As mentioned in the linked question/answer, you're going to want to override `onSaveInstanceState()` to retain the value of your `proxima` counter, and then use the `savedInstanceState` bundle in `onCreate()` to restore its value. – Ben P. Dec 14 '17 at 21:56
  • I tried anyway, even though I could not save the variable – gabriel Dec 17 '17 at 01:56

1 Answers1

1

When your activity is recreated after it was previously destroyed, you can recover your saved state from the Bundle that the system passes your activity. Both the onCreate() and onRestoreInstanceState() callback methods receive the same Bundle that contains the instance state information.

Because the onCreate() method is called whether the system is creating a new instance of your activity or recreating a previous one, you must check whether the state Bundle is null before you attempt to read it. If it is null, then the system is creating a new instance of the activity, instead of restoring a previous one that was destroyed.

Save your int variable like following example does:

static final String STATE_USER = "user";
private String mUser;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mUser = savedInstanceState.getString(STATE_USER);
    } else {
        // Probably initialize members with default values for a new instance
        mUser = "NewUser";
    }
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putString(STATE_USER, mUser);
    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}
Robillo
  • 192
  • 11