Sending data from a child activity onActivityResult() to parent activity onActivityResult() - android

I have an App with 2 Activities. Activity A (parent) starts Activity B (child) by calling:
startActivityForResult(intentB,B_Request);
Activity B is a barcode scanner. (I use this library: com.journeyapps:zxing-android-embedded:3.2.0#aar).
In this activity I have to override the onActivityResult() method in order to get the scanned code. After that I want to send the data stored in 'code' back to the parent (Activity A) onActivityResult(). I did this like this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
String code = null;
if(result != null){
if(result.getContents() == null){
return;
}
else {
code = result.getContents();
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
Intent intent = new Intent(getApplicationContext(),A.class);
intent.putExtra("scannedCode", code);
setResult(RESULT_OK, intent);
finish();
}
The parent onActivityResult() looks like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAMERA_REQUEST){
if(resultCode == RESULT_OK){
String scannedCode = data.getStringExtra("scannedCode");
toastMessage("Scanned code: " + scannedCode);
}
}
}
I get the following exception and instead of seeing Activity A, I'm redirected to the Activitie's A parent.
E/ActivityManager: Failed to schedule configuration change
android.os.DeadObjectException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:1177)
at android.app.IApplicationThread$Stub$Proxy.scheduleTransaction(IApplicationThread.java:1815)
at android.app.servertransaction.ClientTransaction.schedule(ClientTransaction.java:129)
at com.android.server.am.ClientLifecycleManager.scheduleTransaction(ClientLifecycleManager.java:47)
at com.android.server.am.ClientLifecycleManager.scheduleTransaction(ClientLifecycleManager.java:100)
at com.android.server.am.ActivityManagerService.updateGlobalConfigurationLocked(ActivityManagerService.java:24782)
at com.android.server.am.ActivityManagerService.updateDisplayOverrideConfigurationLocked(ActivityManagerService.java:24902)
at com.android.server.am.ActivityManagerService.updateDisplayOverrideConfigurationLocked(ActivityManagerService.java:24879)
at com.android.server.am.ActivityStackSupervisor.ensureVisibilityAndConfig(ActivityStackSupervisor.java:1671)
at com.android.server.am.ActivityStackSupervisor.realStartActivityLocked(ActivityStackSupervisor.java:1420)
at com.android.server.am.ActivityStackSupervisor.startSpecificActivityLocked(ActivityStackSupervisor.java:1709)
at com.android.server.am.ActivityStack.resumeTopActivityInnerLocked(ActivityStack.java:3043)
at com.android.server.am.ActivityStack.resumeTopActivityUncheckedLocked(ActivityStack.java:2488)
at com.android.server.am.ActivityStackSupervisor.resumeFocusedStackTopActivityLocked(ActivityStackSupervisor.java:2234)
at com.android.server.am.ActivityStack.completePauseLocked(ActivityStack.java:1745)
at com.android.server.am.ActivityStack.activityPausedLocked(ActivityStack.java:1669)
at com.android.server.am.ActivityManagerService.activityPaused(ActivityManagerService.java:9657)
at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:224)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3820)
at android.os.Binder.execTransact(Binder.java:752)
p.s. It's not enough for me to get the code in Activity B, I need the code back in Activity A.
Can you please tell me what am I doing wrong here?
Thank you!

just put new intent() instead of
new Intent(getApplicationContext(),A.class)
in your child activity

Related

onActivityResult Intent is null when passing Intent from Adapter

I am facing a strange issue while returning to an Activity with a Result, I am passing an Intent for startActivityForResult from an Adapter like this :
Intent i = new Intent(activity, EditInfoActivity.class);
i.putExtra("id", list.get(position).getID());
activity.startActivityForResult(i, 100);
and in second Activity i.e. in EditInfoActivity in my case on a Button click I am setting Result for first activity like this:
Intent i = getIntent();
i.putExtra("isDataChange", isDataChange);
setResult(100, i);
finish();
In Activity's onActivityResult method I am able to get result code but getting Intent null.
Why? anybody have any idea on this please share.
in Activity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100) {
//Here data is null and app crash
if (data.getExtras() != null && data.getBooleanExtra("isDataChange", false)) {
recreate();
}
}
}
First, you need to start the Activity with a REQUEST_CODE:
// Here we set a constant for the code.
private final int REQUEST_CODE = 100;
Intent i = new Intent(activity, EditInfoActivity.class);
i.putExtra("id", list.get(position).getID());
activity.startActivityForResult(i, REQUEST_CODE);
Then you need to send RESULT_OK when finishing EditInfoActivity:
Intent i = getIntent();
i.putExtra("isDataChange", isDataChange);
setResult(RESULT_OK, i);
finish();
Then handle the result on your first activity with this:
Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// REQUEST_CODE is defined as 100
if (resultCode == RESULT_OK && requestCode == 100) {
// do process
}
}
setResult TAKES RESULT_CODE instead of REQUEST_CODE.
Replace your code with this, May be it will solve your problem.
setResult(RESULT_OK, i);
And in yout onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
//Here data is null and app crash
if (data != null && data.getBooleanExtra("isDataChange", false)) {
recreate();
}
}
}
Two mistakes. You are passing the intent that was used to launch the activity you are finishing. Use new Intent() instead.
When setting activity result you should use result codes, not a request code setResult(RESULT_OK) alternatively RESULT_CANCELED and handle the response accordingly.

Get result from activity called with Intent [duplicate]

This question already has answers here:
How to manage startActivityForResult on Android
(14 answers)
Closed 8 years ago.
I am new to android development
I call an intent now how can i get result from called activity
can any one tell me how to perform this task ?
i have called intent like.
Intent I = new Intent (this ,abc.class);
startActivity(i);
thanks
Use startActivityForResult and then override onActivityResult in your FirstActivity.
In FirstActivity
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivityForResult(intent, 2);// Activity is started with requestCode 2
Override onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
String message=data.getStringExtra("MESSAGE");
Log.i("Message is",message);
// logs Testing
}
}
In SecondAcivity
Intent intent=new Intent();
intent.putExtra("MESSAGE","Testing");
setResult(2,intent);
finish();//finishing activity
Reference to the docs:
http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)
Example:
http://www.javatpoint.com/android-startactivityforresult-example
For going to second activity use startActivityForResult in your firstclass
Intent callIntent = new Intent(FirstClass.this, SecondClass.class);
startActivityForResult(callIntent, 1);
then override onActivityResult method in your first class like this
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == 1) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// get values from data
}
} }
In your second class do this for returning back, if you want to send
something to first class. Store this in your intent.
Intent result = new Intent(); setResult(Activity.RESULT_OK, result);
finish();
hi you can get result calling activity
Start activity with startActivityForResult(intent, 0);
add below method in calling activity
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
// do your task
} else if (resultCode == RESULT_CANCELED) {
// do your task
}
}
}
In your main Class....
static final int CODE_REQUEST = 1; // The request code
....
Intent pickContactIntent = new Intent(MainClass.this, CallingClassName.class);
startActivityForResult(pickContactIntent, CODE_REQUEST);
..........
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}
In your calling class
Intent result = new Intent();
setResult(Activity.RESULT_OK, result);
finish()

how to get back some text from a calling activity

I have an activity that Call another activity for filling an address and second activity should send back the address for the first activity and show it in a textview in first activity
i used these codes
but i dont know why it s not working
first activity:
Intent in = new Intent(getApplicationContext(),ShippingActivity.class);
startActivity(in);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RESULT_OK){
tvDeliverTo.setText(data.getStringExtra("DeliveryAdressKEY"));
}
}
and second activity
Intent in = new Intent();
in.putExtra("DeliveryAdressKEY", tvAdress.getText().toString());
setResult(RESULT_OK, in);
finish();
thanks in advance
You need to start the second activity with startActivityForResult:
http://developer.android.com/reference/android/app/Activity.html#startActivityForResult%28android.content.Intent,%20int%29
In onActivityResult, you should also check the resultCode is RESULT_OK, rather than the requestCode.
I believe you should be using startActivityForResult() instead of startActivity().
first activity
Intent in = new Intent(getApplicationContext(),ShippingActivity.class);
startActivityForResult(in, 0);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0){
if(resultCode ==1){
tvDeliverTo.setText(data.getStringExtra("DeliveryAdressKEY"));
}
}
}
and second activity
Intent in = new Intent();
in.putExtra("DeliveryAdressKEY", tvAdress.getText().toString());
setResult(1, in);
finish();
and now is working

How i can Start an Activity For Result and get the result from the activity that i started?

can any body explain me How i can Start an Activity For Result and get the result from the activity that i started?
Thanks and Regards
RizN81
use this
in the activity
Intent i = new Intent(this, NextActivity.class);
startActivityForResult(i, SELECT_IMAGE ); //SELECT_IMAGE is an static int value.
this code for result
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch(requestCode) {
case SELECT_IMAGE:
///// write code here for your requirement
}
}
}
let you want to call ActivityB from ActivityA
Follow the following steps
Step-1 in activityB set the result
in ActivityB set the result using setResult() see the sample code for ActivityB
Intent intent = new Intent();
intent.putExtra("hh", hour);
intent.putExtra("mm", min);
intent.putExtra("ss", sec);
intent.putExtra("am", am);
setResult(2, intent);
step-2 call activityB from ActivityA
in ActivityA call activityB from activityA using following code
startActivityForResult(activityBIntent,1);
step-3 write the logic you want to perfom after getting result from activityB in activityA
when ActivityB finished control will come to onActivityResult() method of calling acticity (ActivityA)
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == 2) {
int hour = data.getIntExtra("hh", 0);
int min = data.getIntExtra("mm", 0);
int sec = data.getIntExtra("ss", 0);
int am = data.getIntExtra("am", 0);
}
}
}
Try your activity start with startActivityForResult() method and onActivityResult() method check if your activity got completed.
Try this
IN Activity_A
// Activity Callback Variable
private static final int FROM_ACTIVITY_B = 2;
// Now Start the Activity B from Activity A
startActivityForResult(new Intent(Activity_A.this, Activity_B.class), FROM_ACTIVITY_B);
IN Activity_B
// Now Place the following code when you want to pass the result to caller Activity which in our case is Activity_A
Intent data = new Intent();
// Put some data in the intent if you want those in the Activity_A
setResult(Activity.RESULT_OK, data);
Activity_B.this.finish();
IN Activity_A
Now to get the result in Activity_A you need to override onActivityResult in Activity_A
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == FROM_ACTIVITY_B) {
// Intent data is the one you passed from the Activity_B
// Do whatever you want here...
}
}
}

pass argument to previous activity

I would like to pass arguments from an activity B to A where B has been launched by A. Is this possible to do so ?
Thanks
Yes, if when you launch Activity B from A, you start it using startActivityForResult then you can set a result in Activity B then read the value in A.
In A you would 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
}
To expand a bit on davec's answer:
If you need more data than just RESULT_OK, then you will have to use putExtra() in B and getExtras() in A. You can send primitive data types, e.g 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");
}
}
}
.
Look at startActivityForResult (to be called from A), setResult (to be called from B), and onActivityResult (A's callback that gets called after B exits).

Categories

Resources