Android onActivityResult() doesn't get called - java

I've got a problem I don't know how to solve.
This is my code of the MainActivity.java which starts a new activity:
Intent intent = new Intent(this, AssociationActivity.class);
intent.putExtra("data", json);
startActivityForResult(intent, 0);
Additionaly, I have the following onActivityResult()-method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.print("works outside!");
if (requestCode == 1) {
if(resultCode == RESULT_OK){
System.out.print("works inside!");
}
if (resultCode == RESULT_CANCELED) {
}
}
}
And this is where the result should be sent to the MainActivity:
Intent returnIntent = new Intent();
returnIntent.putExtra("result", string);
setResult(RESULT_OK, returnIntent);
finish();
It seems that the onActivityResult() doesn't even get called as I don't have any output on the console.
In the AndroidManifest.xml I neither use the singleIntent nor the noHistory parameter.
Any suggestions why it doesn't work?

requestCode change from 1 to 0. as shown below
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.print("works outside!");
if (requestCode == 0) {
if(resultCode == RESULT_OK){
System.out.print("works inside!");
}
if (resultCode == RESULT_CANCELED) {
}
}
}

Okay, I found the issue, the wasn't any issue at all, I replaced the System.out statements with the Log from Android it's working all the time, Android just delayed the System.out, but I don't know why.
Thanks #Rene Juuse for that tip!

Related

How to get Callback share result in android

How to get the intent callback sharing result, i have seen some code and implemented from my side. Below is the sample code
try {
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Body");
startActivityForResult(Intent.createChooser(intent, "Choose"), 1);
} catch (Exception e) {
e.printStackTrace();
}
Code for callback result
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
}
}
}
In this requestCode is always getting 0 not 1 after i successfully share to any social media platform, is there any code which is newer version?
The method you're using is deprecated, The new way is
ActivityResultLauncher<Intent> launcher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
// do something
}
});
Launching the activity
launcher.launch(yourIntent);
sharing result
Intent intent = new Intent();
intent.putExtra(YOUR_KEY, YOUR_DATA);
setResult(RESULT_OK, intent);
Dependency
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation "androidx.activity:activity:1.2.0"
...
}
To learn more about this visit

OnActivityResult() returning null data

I know this question has been asked many times and i followed all the steps correctly but still getting null data in my FirstApplication
FirstApplication starting an intent to get result from a library class using Mainactivity that is initialised using the activity variable in a constructor:
sendIntent.putExtra("data", bundle);
sendIntent.setType("text/plain");
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (sendIntent.resolveActivity(context.getPackageManager()) != null) {
activity.startActivityForResult(sendIntent, requestCode);
}
SecondApplication getting the intent and doing necessary operation
Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("data");
if (bundle!=null){
type = bundle.getString("type");
num1 = Integer.parseInt(bundle.getString("num1"));
num2 = Integer.parseInt(bundle.getString("num2"));
if (type.equalsIgnoreCase("add")){
result = String.valueOf(num1+num2);
}else {
result = String.valueOf(num1-num2);
}
}
Sending result to FirstApplication
Bundle bundle1 = new Bundle();
if (bundle1!=null){
Intent send = new Intent();
bundle1.putString("result", result);
send.putExtra("datasend", bundle1);
setResult(Activity.RESULT_OK, send);
finish();
}
Receiving result in FirstApplication MAinActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
Bundle bundle = data.getBundleExtra("datasend");
result = bundle.getString("result");
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
But here i am receiving resultCode 0 and data null. Any help will be appreciated. Thank you.
The problem is in this line
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
By calling Intent.FLAG_ACTIVITY_NEW_TASK you are setting the second activity on the top of the back stack and cancelling the effect of startActivityForResult()
So it should be removed to get back the result

Four Activities don't display Toast

I have four activities:
Activity A
private void addCard() {
Intent intent = new Intent(MainActivity.this, GetNumberActivity.class);
startActivityForResult(intent, REQUEST_CODE_CREATE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_CREATE) {
if (resultCode == RESULT_OK) {
if (data.hasExtra("data")) {
// Card has been create
Toast.makeText(getApplication(), "Karata została wygenerowana.", Toast.LENGTH_SHORT).show();
}
}
}
}
Activity B
Intent intent = new Intent(GetNumberActivity.this, ScanQrCodeActivity.class);
intent.putExtra(EXTRA_MESSAGE, uunitValue);
startActivityForResult(intent, REQUEST_CODE);
Then in the second activity I have to pass data to the third activity.
Activity C
Card card = new Card(path3, base32, nameCard, intervalTotp, passwordHotp, getDate(), expirationDate, hotpValue);
Intent intent = new Intent(ScanQrCodeActivity.this, Stage3Activity.class);
intent.putExtra("card", card);
startActivity(intent);
finish();
Activity D
Intent data = new Intent(Stage3Activity.this,MainActivity.class);
data.putExtra("data", card);
startActivityForResult(data, RESULT_OK);
When I press the button on Activity A, the Toast is not shown.
You need to update your code as follow
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CODE_CREATE) {
if (data.hasExtra("data")) {
// Card has been create
Toast.makeText(getApplication(), "Karata została wygenerowana.", Toast.LENGTH_SHORT).show();
}
}
}
First check for RESULT_OK and then proceed further
Happy Coding!
Replace getApplication() with this (the context of the current activity)
Toast.makeText(this.class, "Karata została wygenerowana.", Toast.LENGTH_SHORT).show();
Use getApplicationContext() instead of getApplication() in your makeText() method

onActivityResult in Fragment can't access UI elements

I have a button in Fragment when I press it I open a new activity for result but When I return back to my fragment I found all UI element = null
Please find the code
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), MyActivity.class);
getActivity().startActivityForResult(intent, "3030");
}
});
when choose a value from activity I should back to fragment and set data to textview in the activity.
Intent intent = Activity.this.getIntent();
intent.putExtra("categoryId", id);
intent.putExtra("categoryName", name);
setResult(RESULT_OK, intent);
finish();
and I have put that in the activity that contains the fragment
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 3030 && resultCode == RESULT_OK) {
Fragment fragment = mTabFragments.get(MyFragment.class.getName());
if (fragment != null) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
}
and in fragment
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 3030 && resultCode == Activity.RESULT_OK) {
int categoryId = data.getIntExtra("categoryId", 0);
String categoryName = data.getStringExtra("categoryName");
mChooseCategoryTextView.setText(categoryName);
}
}
the problem now that mChooseCategoryTextView is null
Can anyone tell me what is the problem?
To get result in fragment
startActivityForResult(intent,REQ_CODE);
not
getActivity().startActivityForResult(intent,REQ_CODE);
I think the question how you initialise this view?
Since your lunching another activity so your whole fragment my be reconstruct or only onViewCreated recalled again.
So my guess is that you don't reinitialize or override mChooseCategoryTextView reference in one of fragment callbacks.
Try to add more logs and check what's happening for this reference
I believe the error is in the line
Fragment fragment = mTabFragments.get(MyFragment.class.getName());
I assume mTabFragments in an adapter of some sort? I'd have to look at it's code to be sure, but it sounds like it's not returning the right fragment. Make sure that the reference it's returning is the same as the fragment that is being shown on screen.

Activity A calling B and B calling C, Need C to send data back to A

I have a main activity that calls another(2nd) activity "VennDiagram"
Intent intent = new Intent(getApplicationContext(), VennDiagram.class);
startActivityForResult(intent, 200);
the 2nd activity after doing some calculations calls a 3rd activity "ImageMapTest...." acitvity
Intent i = new Intent(getApplicationContext(), ImageMapTestActivity.class);
i.putExtras(sendBundle);
startActivity(i);
//finish();
Now I need help to return the an arrayList from the 3rd activity to the first ??!!
Intent in = new Intent(getApplicationContext(), AndroidClientActivity.class);
in.putExtra("songList", playList);
setResult(200, in);
finish();
I do have an intent listener as below at my 1st activity, but its just that the 3rd activity won't send it back to it
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 100 || resultCode == 200)
{
}
P.S I did read couple of similar posts but they were not what I was looking for...
You should be able to just send the result received by B from C back to A.
protected void onActivityResult(int requestCode,int resultCode, Intent data) {
super.onActivityResult(requestCode, data);
setResult(resultCode,resultData);
finish();
}
Finish C and return your array to B, and then when you return to B finish and return the result back to A.

Categories

Resources