切换意图时我会丢失数据

时间:2013-03-15 07:04:33

标签: android android-intent

当切换意图时,我丢失了之前onActivityResult的数据我需要它来保留它从用户获得的两个数字,目前它将保留一个数字,然后当输入下一个时它丢失了前一个,这里的代码:

package com.eric.theworks;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

Button width, height, calc;
TextView area;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    width = (Button) findViewById(R.id.button1);
    height = (Button) findViewById(R.id.button2);
    calc = (Button) findViewById(R.id.button3);
    area = (TextView) findViewById(R.id.textView1);

    width.setOnClickListener(this);
    height.setOnClickListener(this);
    calc.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent i = new Intent(this, Numbers.class);
    switch (v.getId()) {

    case R.id.button1:
        // width
        i.putExtra("numbers", "width");
        startActivityForResult(i, 1);

        break;

    case R.id.button2:
        // height
        i.putExtra("numbers", "height");
        startActivityForResult(i, 1);

        break;

    case R.id.button3:
        // calc

        break;

    }


    }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (data.getExtras().containsKey("widthInfo")){
        width.setText(data.getStringExtra("widthInfo"));

}
    if (data.getExtras().containsKey("heightInfo")){
        height.setText(data.getStringExtra("heightInfo"));

}

}

}





package com.eric.theworks;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Numbers extends Activity implements OnClickListener {
EditText number;
Button sendInfo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.numbers);
    number = (EditText) findViewById(R.id.editText1);
    sendInfo = (Button) findViewById(R.id.button1);
    sendInfo.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    String s = number.getText().toString();
    Intent i = getIntent();
    String msg = i.getStringExtra("numbers");
    if (msg.contentEquals("width")) {
        i.putExtra("widthInfo", s);
        setResult(RESULT_OK, i);
        finish();

    }
    if (msg.contentEquals("height")) {
        i.putExtra("heightInfo", s);
        setResult(RESULT_OK, i);
        finish();

    }

    }

}

3 个答案:

答案 0 :(得分:1)

您可以使用静态变量来存储以前的数据。

全局声明静态字符串。

static String widthInfo="";
static String heightInfo="";

还提供不同的请求代码。

case R.id.button1:
        // width
        i.putExtra("numbers", "width");
        startActivityForResult(i, 1);

        break;

    case R.id.button2:
        // height
        i.putExtra("numbers", "height");
        startActivityForResult(i, 2);

然后在你的onActivityResult中使用它。

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

        if (data.getExtras().containsKey("widthInfo")){
            widthInfo=data.getStringExtra("widthInfo")
            width.setText(data.getStringExtra("widthInfo"));

    } else {
    height.setText(heightInfo);
    width.setText(widthInfo);
    }
        }
   break;
       case (2): 
        {
        if (data.getExtras().containsKey("heightInfo")){
            heightInfo=data.getStringExtra("heightInfo")
            height.setText(data.getStringExtra("heightInfo"));

    }else {
    height.setText(heightInfo);
    width.setText(widthInfo);
    }

    }
   break;
    }
}

答案 1 :(得分:0)

您可以使用Bundle将数据从一个活动传递到另一个活动而不是直接传递意图。例如:

 Bundle b = new Bundle();
        b.putString("SingleClick",a );
        b.putString("LongClick", "no");
        i.putExtras(b);

并使用

获取另一个活动中的数据
Bundle bundle = getIntent().getExtras();
        String admin = bundle.getString("LongClick");

答案 2 :(得分:0)

String s = number.getText().toString();
Intent i = getIntent();
String msg = i.getStringExtra("numbers");
if (msg.contentEquals("width")) {
    i.putExtra("widthInfo", s);
    setResult(RESULT_OK, i);
    finish();

}

确保字符串 s 具有任何值....