0

I am passing a putextra from first activity to second activity. I validate what was passed to second activity. I have this working like I intended. My question is how to pass a string value back to the first activity.

Here is where I call the second activity from first.

public void scannedItem(String barCode,String localArea){
    Intent intent = new Intent(this,selectItemActivity.class);
    intent.putExtra("Barcode",mScan);
    intent.putExtra("Area",myArea);
    startActivityForResult(intent,1);
   // finish();
}

On second activity I am receiving data from first activity like so

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_select_item);
    passedArea = getIntent().getExtras().getString("Area");
    passedScan=getIntent().getExtras().getString("Barcode");

    updateScreen();
}

I perform validation on the extras passed from first activity and would like to pass a single string back to the first activity. On second activity I have loaded a RecyclerViewer and on the onClick I have tried.

    public void onSelectClick(View view){
        String lSelectLocation = mLocations.getLocation();
        Timber.d(lSelectLocation);
        Intent intent = getIntent();
        intent.putExtra("Location",lSelectLocation);
        finish();
    }

My question is at least two parts. 1.) what do I have wrong on second activity to pass the String value back. 2.) On first activity what is needed to receive the String back. I have tried onResume but the following is null.

protected void onResume() {
    super.onResume();
    final String sender=this.getIntent().getExtras().getString("Location");
    if(sender != null)
    {
        this.receiveData();
        Toast.makeText(this, "Received", Toast.LENGTH_SHORT).show();

    }

}

I also have this method on first activity but it doesn't seem to get fired.

protected void onActivityResult(int requestCode,String requestLocation,Intent data){ mLoc.setText(requestLocation); }

I have learned a lot in the last few months in Java. I have years as VB.net programmer and sometimes get lost in the differences.

Thank you all in advance.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
Tom
  • 151
  • 1
  • 12
  • please refer below answer and check – Nirav Bhavsar Aug 08 '18 at 16:39
  • Use `startActivityForResult()` and put your `String` as an "extra" into an `Intent` that you send back in `setResult()`. The originating `Activity` gets the result in the `onActivityResult()` callback. – David Wasser Aug 08 '18 at 16:54
  • The method `protected void onActivityResult(int requestCode,String requestLocation,Intent data)` doesn't get called because it isn't an overriding method. You should use your IDE to help generate signatures for overriding methods and also you should use the `@Override` annotation. This would have warned you that the method has the wrong signature. That's why it isn't being called. – David Wasser Aug 08 '18 at 16:58

2 Answers2

1

Replace getIntent(); in onSelectClick method with new Intent(); and also define setResult as it will Call this to set the result that your activity will return to its caller.

public void onSelectClick(View view){
    String lSelectLocation = mLocations.getLocation();
    Timber.d(lSelectLocation);
    Intent intent = new Intent();
    intent.putExtra("Location",lSelectLocation);
    setResult(1, intent);
    finish();
}

and in first activity use onActivityResult to get the return value from second activity, not in onResume.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == 1) {

        String str_location = null;
        if (data != null) {
            str_location = data.getStringExtra("Location");
            if (str_location != null) {
                Toast.makeText(MainActivity.this, str_location, Toast.LENGTH_LONG).show();
            }
        }

    }
}
Nirav Bhavsar
  • 2,133
  • 2
  • 20
  • 24
  • public void onSelectClick(View view){ String lSelectLocation = mLocations.getLocation(); Timber.d(lSelectLocation); Intent intent = new Intent(); intent.putExtra("Location",lSelectLocation); setResult(1, intent); finish(); } – Tom Aug 08 '18 at 18:13
  • on the OnSelectClick lSelectLocation has expected value during the intent.putExtra bu during the onActivityResult data says has extras but str_location gets set to null. Also resultCode coming back as -1 – Tom Aug 08 '18 at 18:20
0

Use setResult in Activity 2 and the in Activity 1 get the values in onActivityResult....

Manu Mayank
  • 321
  • 1
  • 5
  • startActivityForResult only asks for requestCode which seems to expect integer coming back. I need a String coming back – Tom Aug 08 '18 at 15:56