1

I intend to share 6 text information, however it always shows the last information only, i.e.

share.putExtra(Intent.EXTRA_TEXT, Contact); 

I also tried to use a string array to store all 6 information, i.e:

share.putExtra(Intent.EXTRA_TEXT, stringArray); 

However, it still doesn't work. Can anyone help ? Thank you.

My code:

public class SingleJobActivity extends Activity {

// JSON node keys
private static final String TAG_POSTNAME = "PostName";
private static final String TAG_LOCATION = "Location";
private static final String TAG_SALARY = "Salary";

private static final String TAG_RESPONSIBILITY = "Responsibility";
private static final String TAG_COMPANY = "Company";
private static final String TAG_CONTACT = "Contact";


String PostName;
String Location;
String Salary;
String Responsibility;
String Company;
String Contact;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_single_job_json_parsing);

    // getting intent data
    Intent in = getIntent();

    // Get JSON values from previous intent
    PostName = in.getStringExtra(TAG_POSTNAME);
    Location = in.getStringExtra(TAG_LOCATION);
    Salary = in.getStringExtra(TAG_SALARY);

    Responsibility = in.getStringExtra(TAG_RESPONSIBILITY);
    Company = in.getStringExtra(TAG_COMPANY);
    Contact = in.getStringExtra(TAG_CONTACT);

    // Displaying all values on the screen
    TextView lblPostName = (TextView) findViewById(R.id.PostName_label);
    TextView lblLocation = (TextView) findViewById(R.id.Location_label);
    TextView lblSalary = (TextView) findViewById(R.id.Salary_label);

    TextView lblResponsibility = (TextView) findViewById(R.id.Responsibility_label);
    TextView lblCompany = (TextView) findViewById(R.id.Company_label);
    TextView lblContact = (TextView) findViewById(R.id.Contact_label);

    lblPostName.setText(PostName);
    lblLocation.setText(Location);
    lblSalary.setText(Salary);

    lblResponsibility.setText(Responsibility);
    lblCompany.setText(Company);
    lblContact.setText(Contact);

    // listeners of our  button
    View.OnClickListener handler = new View.OnClickListener() {
        public void onClick(View v) {
            switch (v.getId()) {

                case R.id.share:
                    shareTextUrl();
                    break;

            }
        }
    };

    // our button
    findViewById(R.id.share).setOnClickListener(handler);
}

// Method to share either text or URL.
private void shareTextUrl() {
    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType("text/plain");
    share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);


    // Add data to the intent, the receiving app will decide
    // what to do with it.
    share.putExtra(Intent.EXTRA_SUBJECT, "Job Information:");
    share.putExtra(Intent.EXTRA_TEXT, PostName);
    share.putExtra(Intent.EXTRA_TEXT, Location);
    share.putExtra(Intent.EXTRA_TEXT, Salary);
    share.putExtra(Intent.EXTRA_TEXT, Responsibility);
    share.putExtra(Intent.EXTRA_TEXT, Company);
    share.putExtra(Intent.EXTRA_TEXT, Contact);


    startActivity(Intent.createChooser(share, "Share via"));
}

}
Tip
  • 21
  • 4
ncc785
  • 13
  • 1
  • 3
  • It's look like there's no way to do that except to be compatible with share rules. The only possible solution is concatenating the shared content in one String like other apps do. – IgniteCoders Jan 16 '18 at 10:38

3 Answers3

1

Could anyone help ?

Concatenate the six strings into one larger string, and share that larger string.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Thank you ! it worked. I do in this way: String a= PostName + Location + Salary + Responsibility + Company + Contact; String contentShare = new String(a); >>> share.putExtra(Intent.EXTRA_TEXT, contentShare); However, when I try to share in Emulator, these 6 texts just "stick togather", instead, I intend to make a break between each text: e.g. first line: A, second line: B, third line: C .... now, it is : first line: A B C .... can you advise ? Thanks – ncc785 Jun 22 '15 at 17:36
  • 1
    @ncc785: "can you advise ?" -- put newline characters in between each piece (`"\n"`). – CommonsWare Jun 22 '15 at 17:49
0

You can Concatenate the individual strings to larger string, and can get it

Intent iin = getIntent();
Bundle b = iin.getExtras();
if (b != null) {
    String j = (String) b.get("name");
    JSONObject name = new JSONObject(j);

    Textv.setText(name.getString("postName"));
    TextvDesc.setText(name.getString("location"));
}
Prabhuraj
  • 928
  • 2
  • 8
  • 14
0

What's wrong with your actual code

Let's first understand why you only get the last piece of data.

Your problem is by convention in Java (and this also applies to the android.content.Intent.html#putExtra(String, String[]) method you are using) the methods "putXXX" replace the actual value (if it exists) with the new one you are passing. This is similar to java.util.Map.html#put(K, V) method.

First possible solution

For your current code to work, you would have needed to use a different key for your extra data each time, that is, something like that:

share.putExtra(SingleJobActivity.EXTRA_TEXT_NAME, PostName);
share.putExtra(SingleJobActivity.EXTRA_TEXT_LOCATION, Location);
share.putExtra(SingleJobActivity.EXTRA_TEXT_SALARY, Salary);
share.putExtra(SingleJobActivity.EXTRA_TEXT_RESPONSIBILITY, Responsibility);
share.putExtra(SingleJobActivity.EXTRA_TEXT_COMPANY, Company);
share.putExtra(SingleJobActivity.EXTRA_TEXT_CONTACT, Contact);

This would work fine (assuming you declare as public static final the keys used, and you respect the Android contract for extra data keys, such as using the full package name for the key (e.g. public static final EXTRA_TEXT_NAME = "com.yourpackage.EXTRA_DATA_NAME";).

Second possible solution

Another way of doing it is to pass one extra with a String[] (see method documentation).

String[] extraParams = new String[6];
extraParams[0] = PostName;
extraParams[1] = Location;
extraParams[2] = Salary;
extraParams[3] = Responsibility;
extraParams[4] = Company;
extraParams[5] = Contact;
share.putExtra(SingleJobActivity.EXTRA_TEXT, extraParams);

Then in your new activity you retrieve this array using android.content.Intent.html#getStringArrayExtra(String) method.

Intent intent = getIntent();
String[] extraParams = intent.getStringArrayExtra(SingleJobActivity.EXTRA_TEXT);
Sir4ur0n
  • 1,753
  • 1
  • 13
  • 24