3

I'm getting some values from an Activity and getting those in onActivityResult(), onActivityResult() is invoked but data recieved is null CODE of MainActivity

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    // TODO Auto-generated method stub

    if(item.getItemId() == R.id.action_settings){
        Intent intent = new Intent(MainActivity.this, AddDetailActivity.class);
        startActivityForResult(intent, request_code);
    }
    return true;
}


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

            String name = data.getExtras().getString(AddDetailActivity.NAME_KEY);
            String address = data.getExtras().getString(AddDetailActivity.ADDRESS_KEY);

            Toast.makeText(MainActivity.this, "Name is : " + name + " Address : " + address, Toast.LENGTH_LONG).show();

                        }
    }
}

and this is the second Activity from which Im getting data like this :

public class AddDetailActivity extends Activity {

public static final String NAME_KEY = "U-name";
public static final String ADDRESS_KEY = "add";

protected EditText name_et, address_et;
protected String put_name, put_address;
protected Button button = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_detail);

    name_et = (EditText) this.findViewById(R.id.name_editText);
    address_et = (EditText) this.findViewById(R.id.address_editText);

    put_name = name_et.getText().toString();
    put_address = address_et.getText().toString();
    //put_type = null;

    button = (Button) findViewById(R.id.add_button);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent();

            intent.putExtra(NAME_KEY, put_name);
            intent.putExtra(ADDRESS_KEY, put_address);
            setResult(RESULT_OK, intent);

            finish();
        }
    });
}}

how to solve this bug, thanks

Arshad Ali
  • 3,082
  • 12
  • 56
  • 99
  • instead of this `String name = data.getExtras().getString(AddDetailActivity.NAME_KEY)` try this `String name = data.getExtras().getString(U-name)`. – Raghunandan Jul 02 '13 at 04:33
  • @Raghunandan I have tried that as well, but the result is still `null` – Arshad Ali Jul 02 '13 at 04:37
  • I'd suggest putting a breakpoint or adding some logging around AddDetailActivity.onClick(), and confirming that you're putting sensible data in. – Martin Jul 02 '13 at 04:48
  • 1
    You are getting your EditText value outside of add_button click. Get this Value inside add_button click . – SAURABH_12 Jul 02 '13 at 04:50
  • Ooops! that was my mistake, now I'm done, thanks for assistance! – Arshad Ali Jul 02 '13 at 04:57

3 Answers3

7

Just make changes in AddDetailActivity.java

This should be inside onClick method

put_name = name_et.getText().toString();
put_address = address_et.getText().toString();

your on click method should be like this

button.setOnClickListener(new View.OnClickListener() {

        @Override
           public void onClick(View arg0) {
            // TODO Auto-generated method stub

            put_name = name_et.getText().toString();
            put_address = address_et.getText().toString();
            // put_type = null;

            Intent intent = new Intent();

            intent.putExtra(NAME_KEY, put_name);
            intent.putExtra(ADDRESS_KEY, put_address);
            setResult(RESULT_OK, intent);

            finish();
        }
    });
Ronak Mehta
  • 5,971
  • 5
  • 42
  • 69
4

Move your get code inside button click

 button = (Button) findViewById(R.id.add_button);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            put_name = name_et.getText().toString();
            put_address = address_et.getText().toString();
            Intent intent = new Intent();

            intent.putExtra(NAME_KEY, put_name);
            intent.putExtra(ADDRESS_KEY, put_address);
            setResult(RESULT_OK, intent);

            finish();
        }
    });

Also in your onActivityResult make the below changes

    String name = data.getExtras().getString("U-name");
    String address = data.getExtras().getString("add");
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

Public static variables and Android activity life cycle management

please refer to this, when the activity B is finished you are using the static variables from that activity.

so the AddDetailActivity.NAME_KEY won't be available do this instead

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

            String name = data.getExtras().getString("U-name");
            String address = data.getExtras().getString("add");

            Toast.makeText(MainActivity.this, "Name is : " + name + " Address : " + address, Toast.LENGTH_LONG).show();

                        }
    }
Community
  • 1
  • 1
Aashish Bhatnagar
  • 2,595
  • 2
  • 22
  • 37
  • Dear! sorry to say that, but the static veriable still works for me in my case, bye the way thanks for suggestion. – Arshad Ali Jul 02 '13 at 04:55
  • @ArshadAliSoomro it is not good to use static variable in this case coz you call finish and you should let the gc to free memory so static variables in this case hols reference to the activity which might garbage collected. – Raghunandan Jul 02 '13 at 05:01
  • @Raghunandan I agree, but I hve used those for `KEY` so that I can get correct value with that `KEY`, As you said and I know as well that this is garbage collected. – Arshad Ali Jul 02 '13 at 05:07