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

    }

    }

}

3 Answers 3

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;
    }
}
20
  • 1
    This method does work, but for some reason it gets rid of the text on button 2 and makes it blank (tho can still be used), as of right now they but just say "button", but that would change eventually. Mar 15, 2013 at 9:45
  • you can do one thing. put the resultCode different 1 for width and 2 for height let say then in activityResult separate above code according to resultCode. switch(ResultCode){case 1: {// code of width break;} case 2: { // code of height. break;}} Mar 15, 2013 at 9:51
  • Same issue.... it is strange as Tutorial I watched guy coded it the original way and showed himself running it and everything and it worked fine. Heres link: youtube.com/watch?v=03uml_haPH0&list=SP667C75EE599C22C6 Mar 16, 2013 at 5:12
  • Ok very strange, the program works on a avd but not my actual device lol? Mar 16, 2013 at 5:26
  • whats different in your edited version anyway maybe im missing somthing? I tryed the =""; Mar 16, 2013 at 5:33
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");
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....

1
  • 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. Mar 15, 2013 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.