如何在的onSaveInstanceState使用可变 [英] how to use variable in onSaveInstanceState

查看:88
本文介绍了如何在的onSaveInstanceState使用可变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习的机器人编程和我有使用可变的onSaveInstanceState处的一个问题。这是我的code:

I just started learning programming at the android and I have a problem with using variable at onSaveInstanceState. This is my code:

int resultCode;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    if (savedInstanceState != null) {

        super.onRestoreInstanceState(savedInstanceState);

        int resultCode = savedInstanceState.getInt("resultCode");

    } 


    Button btnOpenWithResult = (Button)findViewById(R.id.btnOpenWithResult);
    btnOpenWithResult.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent myIntent = new Intent(flashlight.this, ThirdActivity.class);
            startActivityForResult(myIntent, 1);

        }
    });
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    switch (resultCode) {
    case 1:   
         /** option 1  */            
        break;

    case 2:
         /** option 2 */
        break;

} 
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
     savedInstanceState.putInt("resultCode", resultCode);
    super.onSaveInstanceState(savedInstanceState);
}

我要保存使用的onSaveInstanceState变量结果code和活动的恢复后再次使用它...

I want to save the variable resultCode using onSaveInstanceState and after the resumption of activity once again to use it ...

(对不起我的英语水平)

(sorry for my level of English)

推荐答案

Cyren ...... 1)我看不出有什么理由说super.onRestoreInstanceState中的onCreate。它将使意义,使这一呼吁的方法

Cyren... 1) I see no reason to call super.onRestoreInstanceState in onCreate. It WOULD make sense to make that call in the method

public void onRestoreInstanceState(Bundle saved) {
    super.onRestoreInstanceState(saved);

2)声明:

2) The declaration:

   int resultCode = savedInstanceState.getInt("resultCode");

是隐藏的变量:

is "hiding" the variable:

int resultCode;

前面声明。因此有两个版本的变量结果code有不同的范围。也许你的意思是code:

declared earlier. So there are two version of the variable resultCode with different scopes. Perhaps you mean to code:

int resultCode;

stuff here

    resultCode = savedInstanceState.getInt("resultCode");

希望帮助, 日航

Hope that helps, JAL

这篇关于如何在的onSaveInstanceState使用可变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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