Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...