1

When switching intents I lose data from the previous onActivityResult I need it to keep both numbers it gets from the user, currently it will keep one number, then when the next is entered it loses the previous, here's code:

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

    }

    }

}
ShreeshaDas
  • 2,042
  • 2
  • 17
  • 36
Eric Miller
  • 49
  • 10

3 Answers3

1

You can use static variable to store the previous data.

Declare static string globally.

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

Also Give different Requestcode.

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

Then use it in your 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;
    }
}
KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
0

you can use Bundle to pass data from one activity to another rather than the intent directly. for example:

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

and to get the data in another activity use

Bundle bundle = getIntent().getExtras();
        String admin = bundle.getString("LongClick");
krishna
  • 142
  • 7
  • 12
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();

}

make sure the string s has any values....

kalandar
  • 793
  • 6
  • 13
  • What do you mean? It gets text from the edit field in .Numbers. It does work and return the string but the issue is when I change heightInfo the the widthInfo just goes back to saying "button", where I need to keep both strings. – Eric Miller Mar 15 '13 at 09:48