GetIntent (). getExtras () passes null values ​​to OnActivityResult

I pass the context from the activity-A adapter to the page and inside this adapter I pass the intent to make the call

Intent intent = new Intent(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:" + number)); intent.putExtra("id",listId.toString()); context.startActivityForResult(intent, 105); 

Activity A is inherited from another activity B and inside this underlying operation B, onActivityResult is defined as:

 @Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); if (requestCode == 104) { if (GetLocation.isLocationEnabled(this)) { <my codes> } } } 

Inside Activity A, I have onActivityResult as:

 @Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); if (requestCode == 105) { Bundle extras=getIntent().getExtras(); String user_Id=extras.getString("id"); } } 

I always get a null value for user_Id, can someone help me on this issue?

+5
source share
4 answers

use intent.getExtras() instead of getIntent().getExtras()

getIntent() will provide you with the intent that started your activity initially.

+5
source

I could be wrong, but instead of using getIntent (). I believe that you just want to use the intent passed in onActivityResult in the method signature.

0
source

I assume that you have a different problem than getIntent instead of intent. Perhaps I misunderstood your question, but I think that everything you have is invalid onActivityResult is wrong.

onActivityResult is called only if the action that you previously started using startActivityForResult is completed. Then you need to check if the returned resultCode RESULT_OK is equal, and if requestCode matches the requestCode, you started this operation with.

To set the result before ending, you need to follow these steps:

 Intent intent = getIntent(); intent.putExtra("id",listId.toString()); //Your ID comes here setResult(RESULT_OK, intent); finish(); 

EDIT

As I read in the comment, you want to report the main activity from the PageAdapter. Therefore you need to declare an interface in your activity

 public interface IdListener{ void onIdSelected(int id); } 

Pass a new instance of this interface into your business. Perhaps in the constructor of your PageAdapter (create your own). Before starting your intent, call the interfaces method.

 idListener.onIdSelected(yourID); context.startActivity(intent); //don't call startActivityForResult(), you dont need it 

I still do not fully understand what you are trying to achieve. But perhaps this suits your needs more than you have tried before.

0
source

Using startActivityForResult () is when you want one action to start another action, but then the second action returns some result to the first when it ends. So, an example would be Activity A starting Activity B, and then when Activity B finishes, it will send a response back to Activity A. So, to achieve this, several steps must be taken.

First you need to start the second activity ...

 Intent intent = new Intent(this, YourSecondActivity.class); Bundle bundle = new Bundle(); bundle.putString("yourStringExtra", theStringExtra); intent.putExtras(bundle); startActivityForResult(intent, 1); 

Then, when you are ready to complete the second action, you need to set the result to return to the first operation. You do it like this ...

 Intent intent = new Intent(); intent.putExtra("string_result_from_second_activity", stringResult); setResult(RESULT_OK, intent); finish(); 

Then, after completing the second action, the first action is restarted, and you can intercept the result from the second action by overriding onActivityResult (), like this ...

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == 1) { if(resultCode == RESULT_OK) { String resultString = data.getStringExtra("string_result_from_second_activity"); } } } 

So, in this example, the second action sends the stringResult string back to the first activity when it ends with RESULT_OK resultCode. That way, you check requestCode (which we set to β€œ1” when we call startActivityForResult () in the first step), then make sure resultCode is set to RESULT_OK , and then we go ahead and access a line other than Intent.

Hope this clarifies the situation!

0
source

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


All Articles