简体   繁体   中英

Sending data from one activity to another startactivityforresult

I searched on the forums but couldn't find the right answer for me. I've included the relevant parts below

ACTIVITY ONE

implicitActivationButton.setOnClickListener(new OnClickListener() {

            // Call startImplicitActivation() when pressed
            @Override
            public void onClick(View v) {

Intent myIntent = new Intent(ActivityLoaderActivity.this,
                ExplicitlyLoadedActivity.class);
        startActivityForResult(myIntent, GET_TEXT_REQUEST_CODE);
            }
        });

and a little lower

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {

            Log.i(TAG, "Entered onActivityResult()");


            String input=data.getStringExtra(TAG);
            mUserTextView.setText(input);
        }

This is activity 2 after user enters some data

String input=mEditText.getText().toString();


    Intent i = new Intent(ExplicitlyLoadedActivity.this, ActivityLoaderActivity.class);
    i.putExtra("TAG",input);
    startActivity(i);       
    this.setResult(RESULT_OK);      
    finish();

No error messages at all but the text on screen doesnt update. it is supposed to

don't need start activity in second class:

you need change your code with:

Intent i = new Intent();  // or // Intent i = getIntent()
i.putExtra("TAG",input);
setResult(RESULT_OK , i);         
finish();

and for cancel that,

setResult(RESULT_CANCELED, i);        
finish();

try like this

String input=data.getStringExtra("TAG"); 

in place of

 String input=data.getStringExtra(TAG);

On your Activity2 you are launching a new instance of ExplicityLoadedActivity instead of returning into the previous instance.

You should only set the result, and finish your second activity.

Here's the code you can try on your 2nd activity:

Intent returnIntent = new Intent();
returnIntent.putExtra("TAG",input);
setResult(RESULT_OK, returnIntent);        
finish();

Try like this to set result code in ExplicitlyLoadedActivity

String input=mEditText.getText().toString();
Intent i = new Intent();
i.putExtra("TAG",input);
this.setResult(RESULT_OK,i);      
finish();

and in ActivityLoaderActivity access that result string ie

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

            Log.i(TAG, "Entered onActivityResult()");


            String input=data.getStringExtra("TAG");
            mUserTextView.setText(input);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM