onActivityResult not being called when going back - android

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);

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

onActivityResult is not call in my app

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.

Sending data from one activity to another startactivityforresult

I searched on the forums but couldn't find the right answer for me.
I've included the relevant parts below
ACTIVITY ONE
implicitActivationButton.setOnClickListener(new OnClickListener() {
// Call startImplicitActivation() when pressed
#Override
public void onClick(View v) {
Intent myIntent = new Intent(ActivityLoaderActivity.this,
ExplicitlyLoadedActivity.class);
startActivityForResult(myIntent, GET_TEXT_REQUEST_CODE);
}
});
and a little lower
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Entered onActivityResult()");
String input=data.getStringExtra(TAG);
mUserTextView.setText(input);
}
This is activity 2 after user enters some data
String input=mEditText.getText().toString();
Intent i = new Intent(ExplicitlyLoadedActivity.this, ActivityLoaderActivity.class);
i.putExtra("TAG",input);
startActivity(i);
this.setResult(RESULT_OK);
finish();
No error messages at all but the text on screen doesnt update. it is supposed to
don't need start activity in second class:
you need change your code with:
Intent i = new Intent(); // or // Intent i = getIntent()
i.putExtra("TAG",input);
setResult(RESULT_OK , i);
finish();
and for cancel that,
setResult(RESULT_CANCELED, i);
finish();
On your Activity2 you are launching a new instance of ExplicityLoadedActivity instead of returning into the previous instance.
You should only set the result, and finish your second activity.
Here's the code you can try on your 2nd activity:
Intent returnIntent = new Intent();
returnIntent.putExtra("TAG",input);
setResult(RESULT_OK, returnIntent);
finish();
try like this
String input=data.getStringExtra("TAG");
in place of
String input=data.getStringExtra(TAG);
Try like this to set result code in ExplicitlyLoadedActivity
String input=mEditText.getText().toString();
Intent i = new Intent();
i.putExtra("TAG",input);
this.setResult(RESULT_OK,i);
finish();
and in ActivityLoaderActivity access that result string i.e.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Entered onActivityResult()");
String input=data.getStringExtra("TAG");
mUserTextView.setText(input);
}

How to return string from activity started for result

I have started Activity for result, but how to return string like parameter from that activity ?
just use following code block:
Intent intent=new Intent();
intent.putExtra("RESULT_STRING", string);
setResult(RESULT_OK, intent);
finish();
get value from this intent in onActivtyResult method in calling activity:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CREATE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
//Use Data to get string
String string = data.getStringExtra("RESULT_STRING");
}
}
}
You just need to putExtra in the intent and the call setResult(),
Intent data = new Intent();
data.putExtra("myobj", value);
setResult(Activity.RESULT_OK, data);
The documentation says it all. You set the result by calling setResult and you read it in the onActivityResult method.

Why is onActivityResult not called in Android?

When I my app is launched I am showing a splash screen. That page is been shown for 10 sec, running on a thread.
When it switches over to new activity on a result I want o hit a URL in server and I will be getting a return value which I can use for my further implements.
Here is my code:
private final int SPLASH_DISPLAY_LENGHT = 1000;
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
Log.e("Handler ","run");
Intent myIntent = new Intent(getApplicationContext(), CaptureActivity.class);
startActivityForResult(myIntent, imgDL);
finish();
}
}, SPLASH_DISPLAY_LENGHT);
public void onActivityResult(int requestCode, int resultCode, final Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == imgDL)
{
Log.e("onActivity Result","");
urlHitMethod("http://XXXXXXXXXXXXXXXXXX.com/banner_scan");
}
}
But here the onActivityResult is not called. How to fix this?
Also, please note than if you base activity (the one calling startActivityForResult) can't use the flag noHitory in the manifest.
If you do so, onActivityResult will never be called.
try this
Intent myIntent = new Intent(activity.this, CaptureActivity.class);
and
#Override
public void onActivityResult(int requestCode, int resultCode, final Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == imgDL)
{
Log.e("onActivity Result","");
urlHitMethod("http://XXXXXXXXXXXXXXXXXX.com/banner_scan");
}
if(resultCode==RESULT_OK)
{
Log.e("onActivity Result","come in onactivity result ok");
}
else
{
Log.e("onActivity Result","come in onactivity result with error");
}
}
If you are using onActivityResult, then you should not finish the activity when starting with intent otherwise it will crash the app.
Thanks.
In the CaptureActivity.class you have to set the result and then check in the onActivityResult in the First Activity the result code
In the CaptureActivity.class it should be like the following
Intent in = new Intent();
setResult(1,in);//Here I am Setting the Requestcode 1, you can put according to your requirement
finish();

Categories

Resources