int variable does not save with screen rotation

戏子无情 提交于 2019-12-13 03:44:49

问题


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");
            }
        }
    });


}}

回答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);
}


来源:https://stackoverflow.com/questions/47822601/int-variable-does-not-save-with-screen-rotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!