Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

android - Passing data from New activity to old activity?

I am trying to receive data from new Activity to old Activity.

In other words, newer Activity must send intend data to old activity when it finishes.

I've use setResult(resultcode, NewActivity.this.getIntend); on newer activity and

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(data != null) {
        // doing something
    }     
}

on old activity. Receives intend data from newer activity.

But this code doesn't work. Always getting data is null.

How to send intent data from finishing Activity to parent activity ?

Also when create new Activity, I am using following code:

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setClass(OldActivty.this, NewActivity.class);
startActivityForResult(intent, 0);

I don't understand why always getting null intent data from newer one.

how to fix it ?

Please advice.

Thanks,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As CommonsWare wrote in his comment you are passing the same Intent back to your Activity that you used to start the Activity.

Try something like the following code:

Intent newIntent = new Intent();
newIntent.putExtra(key, yourData)
newIntent.putExtra(key2, moreData)

setResult(resultcode, newIntent);

In your case the Intent is only needed to store the data that is passed back. Therefor you can use the standard constructor.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...