Amalpriya
Amalpriya

Reputation: 123

Cannot pass two strings from one activity to another activity at same time

I have two activities Select and Upload. I am passing two strings id and scan_id from one activity to another. The value passes correctly when I pass the id alone. But when I try to pass both the values the one value overrides the other value. Is there a separate way to pass the two strings???

SelectActivity.java

public class SelectActivity extends Activity {
String v1;
public static String EXTRA_MESSAGE_ID;
public static String EXTRA_MESSAGE_SCAN;
int flag;
String strflag;

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                                    .permitAll()
                                    .build();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_select);

    Bundle b = getIntent().getExtras();
    EditText ed=(EditText)findViewById(R.id.patient_id);
    ed.setText(b.getCharSequence("Contents"));

    Button button = (Button) findViewById(R.id.button1);
    StrictMode.setThreadPolicy(policy);

    Button CTbutton = (Button) findViewById(R.id.CTScan);
    CTbutton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
            flag=1;
            strflag = String.valueOf(flag);
            Log.e("log_tag", "sel id"+flag);
            Log.e("log_tag", "sel scan"+v1);

            Intent intent = new Intent(SelectActivity.this,MainActivity.class);
            intent.putExtra(EXTRA_MESSAGE_ID,v1);
            intent.putExtra(EXTRA_MESSAGE_SCAN,strflag);
            startActivity(intent);
        }
    });
}
}

MainActivity.java

public class MainActivity extends Activity {    
  private static final String TAG = MainActivity.class.getSimpleName();

  private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;

  public static final int MEDIA_TYPE_IMAGE = 1;

  private Uri fileUri;
  private Button btnCapturePicture;
  public static String EXTRA_MESSAGE_ID;
  public static String EXTRA_MESSAGE_SCAN;
  String message_id;
  String message_scan;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(getResources().getString(R.color.action_bar))));
    Intent intent = getIntent();
    message_id = intent.getStringExtra(SelectActivity.EXTRA_MESSAGE_ID);
    message_scan = intent.getStringExtra(SelectActivity.EXTRA_MESSAGE_SCAN);

    Log.e("log_tag", "id"+message_id);
    Log.e("log_tag", "scan"+message_scan);

    btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture);
   /**
    * Capture image button click event
    */
    btnCapturePicture.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        captureImage();
      }
    });
  }
}

The logcat looks like this:

04-01 10:57:43.671: E/log_tag(12649): connection success 
04-01 10:57:50.374: E/log_tag(12649): sel id1
04-01 10:57:50.374: E/log_tag(12649): sel scan2
04-01 10:57:50.403: E/log_tag(12649): id1
04-01 10:57:50.403: E/log_tag(12649): scan1

Upvotes: 0

Views: 235

Answers (3)

Neal Ahluvalia
Neal Ahluvalia

Reputation: 1548

initialize the strings in both activity with same values

public static String EXTRA_MESSAGE_ID="something1";

public static String EXTRA_MESSAGE_SCAN="something2";

Upvotes: 2

Moinkhan
Moinkhan

Reputation: 12932

Your logic is correct but problem in below code

public static String EXTRA_MESSAGE_ID;
public static String EXTRA_MESSAGE_SCAN;

you don't have assign the value to a both variable. so by default both variable have blank value. so when you put the value, the second one will overwrite it. because both varable same key as blank value So, try to use give unique value to a variable.

public static String EXTRA_MESSAGE_ID = "MSG_ID";
public static String EXTRA_MESSAGE_SCAN = "MSG_SCAN";

I hope you understand.

Upvotes: 1

Vijay
Vijay

Reputation: 3502

You should try like this:

Change in SelectActivity:

 public static String EXTRA_MESSAGE_ID="ExtraMsgId";
 public static String EXTRA_MESSAGE_SCAN="ExtraMsgScan";

Should not do like this. If you give like this

public static String EXTRA_MESSAGE_ID;
public static String EXTRA_MESSAGE_SCAN;

This is look like empty string.

Upvotes: 0

Related Questions