Pass argument to previous activity

I would like to pass the arguments from action B to A, where B was running A. Is it possible to do this? Thanks

+4
source share
3 answers

Yes, if when starting Activity B from A, you start it with startActivityForResult , then you can set the result to Activity B, then read the value in A.

In A, you need to override onActivityResult to get the result value.

In Activity B:

 // do stuff setResult(RESULT_OK); finish(); 

Then in A:

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); //check result } 
+10
source

To break a bit into a davec response:

If you need more data than just RESULT_OK, you have to use putExtra () in B and getExtras () in A. You can send primitive data types, for example, for String:

In B:

 String str1 = "Some Result"; Intent data = new Intent(); data.putExtra("myStringData", str1); setResult(RESULT_OK, data); 

Then, to pick it up in A:

 protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { if (data != null) { Bundle b = data.getExtras(); String str = b.getString("myStringData"); } } } 

.

+5
source

See startActivityForResult (called from A), setResult (for a call from B) and onActivityResult (Callback that is called after exit B).

+2
source

Source: https://habr.com/ru/post/1344400/


All Articles