onActivityResult is not call in my app - android

I have two activities "A" and "B".
In my "A" activity I use startActivityForResult:
Intent i = new Intent(A.this, B.class);
setResult(RESULT_OK, i);
startActivityForResult(i, 121245);
finish();
This is the code of my "B" activity:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Toast.makeText(B.this, "onActivityResult", Toast.LENGTH_SHORT).show();
if(requestCode == 121245) {
if (resultCode == RESULT_OK)
//make something
}
}
Why my Toast doesn't show?

onActivityResult will be called on the activity which is starting the activity for result, meaning calls the startActivityForResult method
What that means is if you want to be notified when the B activity finishes in A activity, you would first in A activity start the B activity like you did in your example code
Intent i = new Intent(A.this, B.class);
setResult(RESULT_OK, i);
startActivityForResult(i, 121245);
then when B activity finishes A activity's onActivityResultis called and there you can do whatever you want.
Here's a diagram if it helps you understand the flow of the application

You have a little mistake
at A class should be:
Intent i = new Intent(A.this, B.class);
startActivityForResult(i, 121245);
at B class to return:
Intent i = new Intent();
setResult(RESULT_OK, i);
finish();
and handle it at A
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Toast.makeText(B.this, "onActivityResult", Toast.LENGTH_SHORT).show();
if(requestCode == 121245) {
if (resultCode == RESULT_OK)
//make something
}
}

onActivityResult()
must be the part of your A activity, Your B activity will return some data back to A and then onActivityResult() will be called.

Related

The effect of actions in one activity on another in android studio

In android studio I have two activities, Main and Settings.
The settings activity is called from main activity and there is a button for logging out. When I click on this button the settings activity finishes and the main activity appears again. How can I make the main activity know that the log out button was clicked?
And what if there is many actions that I can perform in settings activity?I don't want to return result to main activity, I want to write that actions somewhere and read from there in Main activity.
In the MainActivity you can start the SettingActivity using
Intent intent = new Intent(this, SettingActivity .class);
startActivityForResult(intent, yourRequestCode);
after that in SettingActivity when you click the button logout, parse the boolean using
Intent intent= new Intent();
intent.putExtra("isClicked", true); // save clicked data
setResult(Activity.RESULT_OK, intent);
finish();
then, in MainActivity you can call onActivityResult like this
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == yourRequestCode) {
Boolean isLogoutClicked = data.getExtras().getBoolean("isClicked");
}
now you get the data from the setting activity when the button logout is clicked.
in mainActivity
Intent intent = new Intent(this, SettingActivity .class);
startActivityForResult(intent, 123);
than
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
try {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 123 && resultCode == RESULT_OK)
{
String requiredValue = data.getStringExtra("key");
}
} catch (Exception ex) {
Toast.makeText(Activity.this, ex.toString(),
Toast.LENGTH_SHORT).show();
}
}
and set thus into your setting screen
Intent intent = getIntent();
intent.putExtra("key", value);
setResult(RESULT_OK, intent);
finish();
and check if value have data or anything you past that its come from setting screen

Sending data from activity back to Fragment with finish function

I had tried to send the data from Activity which is instantiated from a Fragment. Sending data from Activity back to fragment. I tried the following code
Fragment:
Instantiated the Fragment
Intent intent = new Intent(Fragement.this,Activity);
startActivity(intent);
Activity:
Intent intent = new Intent(Activitiy.this, Fragment);
intent.putExtra("Sent", "Something");
setResult(RESULT_OK, intent);
finish();
Send result to fragment
In Fragment trying to implement onActivityResult but not even able to define the method.
Call your activity using startActivityForResult like below
Intent intent = new Intent(Fragement.this, Activity);
startActivityForResult(intent, 1);
To get the result implement onActivityResult in your fragment
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == 1) {
// here you can retrieve your bundle data.
String yourdata = data.getStringExtra("Sent");
}
}
And In your activity do like following.
Intent intent = new Intent();
resultIntent.putExtra("Sent", "String data");
setResult(RESULT_OK, intent );
finish();
very important
You must override onActivityResult in your Activity(in which fragment is loaded)
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
For a complete example, see this
you must start the activity with startActviityForResult passing a request code
Intent intent = new Intent(Fragement.this, Activity);
startActivityForResult(intent, myRequestCode);

How to know if intent is coming from 2 activities after current activity?

I have 3 Activities:
Activity A --> Activity B (No History) --> Activity C
Activity A:
Intent intent = new Intent(getContext(), ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivityForResult(intent, IntentKey.ActivityB);
Activity B:
Intent intent = new Intent(this, ActivityC.class);
startActivityForResult(intent, IntentKey.ActivityC);
or there is a Back button to call this method:
finish();
Activity C:
Intent returnIntent = new Intent();
returnIntent.putExtra("test", "fromActivityC");
setResult(RESULT_OK, returnIntent);
finish();
Activity A:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Only returns from Activity B, never from C
}
Can anyone suggest me?
Since you are starting Activity C for result from Activity B, Activity C will only call onActivityResult() for Activity B.
Same goes for Activity A. Since you are starting Activity B for result from Activity A, Activity B will only call onActivityResult() for Activity A.
You need call setResult() in Activity B for the onActivityResult() in Activity A to be called.
First of all, your ActivityC does not know about ActivityA and onActivityResult() will be called only when you set result from called activty using setResult() method.
In ActivityA, method onActivityResult() will be called only if you set result from ActivityB.
So, you need to get the value of test in onActivityResult() of ActivityB and send this value to ActivityA using intent with setResult() method.
ActitvityB
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data.getExtras() != null) {
String str = data.getStringExtra("test");
Intent returnIntent = new Intent();
returnIntent.putExtra("test", str);
setResult(RESULT_OK, returnIntent);
}
}
Get the value of test from ActitvityB.
ActitvityA
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data.getExtras() != null) {
String str = data.getStringExtra("test");
Toast.makeText(getApplicationContext(), "ActivityA onActivityResult() called value is: " + str, Toast.LENGTH_SHORT).show();
}
}

onActivityResult not being called when going back

I am trying to pass data backwards to an activity, however I can never get my onActivityResult function to be called. When I start the new activity, I create a new intent like regular
Intent intent = new Intent(this, NewLoanCost.class);
intent.putExtra("defaultsArray", jDefaultsArray.toString());
intent.putExtra("loanSelection", loanSelection);
intent.putExtra("buyerSellerSelection", buyerSellerSelection);
startActivity(intent);
and when I want to go back to the previous activity, I am overriding the back button to create a new intent and store the data
#Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("fullCosts", fullCosts.toString());
setResult(RESULT_OK, intent);
super.onBackPressed();
}
but in the first activity, I can't even get a debugging toast to appear. Am I missing something blatant?
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(this, "onActivityResult", Toast.LENGTH_SHORT).show();
if (requestCode == 1) {
if(resultCode == RESULT_OK){
try {
jDefaultsArray = new JSONArray(data.getStringExtra("fullCosts"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
To receive a result you should use startActivityForResult instead of startActivity
Change your code to
Intent intent = new Intent(this, NewLoanCost.class);
intent.putExtra("defaultsArray", jDefaultsArray.toString());
intent.putExtra("loanSelection", loanSelection);
intent.putExtra("buyerSellerSelection", buyerSellerSelection);
startActivityForResult(intent, 1);
the second parameter is an int with the request code you will use in onActivityResult
Use startActivityForResult.
Replace your code with this, then it should work:
Intent intent = new Intent(this, NewLoanCost.class);
intent.putExtra("defaultsArray", jDefaultsArray.toString());
intent.putExtra("loanSelection", loanSelection);
intent.putExtra("buyerSellerSelection", buyerSellerSelection);
startActivityForResult(intent);

Multiple activities calling same activity for result

Suppose I have 3 activities.
First
Second
Result.
First and Second both call Result activity by startActivityForResult().
In Result, on basis of calling activity, I want to return different results.
Is there any way to decide which one has called Result Activity and return result to that activity.
You can find the calling activity using getCallingActivity ()
You can't have multiple activities on top at the same time. Are you trying to have them run in order, one after the other?
One way to accomplish this is to start each activity for result:
Intent intent = new Intent(this, MyActivity.class);
startActivityForResult(intent, 0);
Where you use the request code to track when activity is running. Then, in onActivityResult you can start the next one:
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if (requestCode < NUM_ACTIVITIES) {
Intent intent = new Intent(this, MyActivity.class);
startActivityForResult(intent, requestCode + 1);
}
}
If you want to have some of the activities immediatly in the background, you can chain them together by calling startActivity in each Activity's onCreate. If you start a new Activity in onCreate before creating any views, the activity will never be visible.
protected void onCreate (Bundle savedInstanceState) {
int numLeft = getIntent().getIntExtra("numLeft");
if (numLeft > 0) {
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("numLeft", numLeft - 1);
startActivity(intent);
}
}
In you calling activity finish the activity with setresult
Intent in = new Intent();
setResult(100, in);
finish();
In your first activity
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Log.i(Tag, "Result: " + Integer.toString(resultCode));
if (resultCode == 200) {
Log.i(Tag, "second");
} else if (resultCode == 100) {
Log.i(Tag, "activity");
}
}

Categories

Resources