简体   繁体   中英

How can I receive send and recieve multiple data with an implicit intent?

I am sending an implicit intent from one activity to another and I want to send some String variables. I'm doing this as an implicit intent as I wish to use activity B in multiple projects, it's part of a library project.

I have got the intent working just fine but the extra data is not being read in at activity B.

Here's my code in Activity A to create the intent:

    Intent intent = new Intent(this, ShareResultsActivity.class);
    intent.putExtra(EMAIL_SUBJECT, "Results");//String
    intent.putExtra(RESULTS_SAVENAME, saveName);//String
    intent.putExtra(RESULTS_BODY, body);//String
    intent.putExtra(WITH_SAVE_BUTTON, withSaveButton);//Boolean
    startActivity(intent);

And in Activity B:

    Bundle extras = getIntent().getExtras();

    Boolean withSaveButton = true;

    if(extras != null){
        emailSubject = extras.getString("EMAIL_SUBJECT");
        resultsSaveName = extras.getString("RESULTS_SAVENAME");
        resultsBody = extras.getString("RESULTS_BODY");
        withSaveButton = extras.getBoolean("WITH_SAVE_BUTTON", true);
    }

This doesn't work and the String variables are null.

I have also tried to do this in the way an explicit intent recieves the data:

    Intent intent = getIntent();

    emailSubject = intent.getStringExtra("EMAIL_SUBJECT");
    resultsSaveName = intent.getStringExtra("RESULTS_SAVENAME");
    resultsBody = intent.getStringExtra("RESULTS_BODY");
    Boolean withSaveButton = intent.getBooleanExtra("WITH_SAVE_BUTTON", true);

But this doesn't work either.

The only way I can get it to work is if I reference the sending Activity in getStringExtra but I can't do this as it's supposed to be a library class.

Can anyone shed any light on this? Or am I going about this in the wrong way?

I assume your code like this:

final static String EMAIL_SUBJECT = "email_subject";
//  RESULTS_SAVENAME and others are the same style.
Intent intent = new Intent(this, ShareResultsActivity.class);
intent.putExtra(EMAIL_SUBJECT, "Results");//String
startActivity(intent);

then in your another activity:

Intent intent = getIntent();
emailSubject = intent.getStringExtra("email_subject");
// or this
// emailSubject = intent.getStringExtra(EMAIL_SUBJECT);

Please make use of the key , it should be same in both the activities. I clearly see a difference in naming convention of the key name in both the activities.If key name is same, no way it data should not pass.

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