int 变量不随屏幕旋转而保存 [英] int variable does not save with screen rotation

查看:25
本文介绍了int 变量不随屏幕旋转而保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我放置了一个 imageview 来加载一个 url 的图像,当我点击一个按钮时,它会传递到下一个 url,从而更改图像.问题是,当我旋转屏幕时,它返回到显示的第一个图像.我试图用递增的计数器做一个 if 但它删除了变量并再次返回到第一个.有人知道如何保存next"变量的值,因此当屏幕旋转时它会保存该值,或者知道另一种方法来保存最后一张图像.api 毕加索

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

代码完成

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


}}

推荐答案

当您的 Activity 在之前被销毁后重新创建时,您可以从系统传递给您的 Activity 的 Bundle 中恢复您保存的状态.onCreate() 和 onRestoreInstanceState() 回调方法都接收包含实例状态信息的相同 Bundle.

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.

因为无论系统是在创建您的 Activity 的新实例还是重新创建前一个实例,都会调用 onCreate() 方法,所以您必须在尝试读取它之前检查状态 Bundle 是否为空.如果它为空,则系统正在创建 Activity 的新实例,而不是恢复先前已销毁的实例.

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.

像下面的例子一样保存你的 int 变量:

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

这篇关于int 变量不随屏幕旋转而保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆