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).
Related
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);
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
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();
}
}
I have X, Z activities and Y class. Y is view of activity X and called activity for result from X to Z..its not going to onActivityResult in X.
Activity X:
SetContentView(Y);
and
Intent i=new Intent(x.this,z.class);
startActivityforResult(i,100);
onActivityResult method:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==100)
{
Log.e("Message from Z","Message");
}
}
Activity Z:
setResult(RESULT_OK, (new Intent()).setAction("close"));
this.finish();
How to solve this??
You are creating a new intent; you dont need to do that.
This should work:
setResult(Activity.RESULT_OK);
finish();
Also as a recommendation dont use hard coded request code ( in your case 100). Declare a private static final int variable and use it when starting the activity for result (startActivityforResult) and when accepting the result on onActivityResult method.
Use setResult() like this:
Intent i = getIntent(); //gets the intent that called this intent
setResult(Activity.RESULT_OK, i);
Add your intent as one parameter of setResult()
Try this :
in onActivityResult() update the code
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==100)
{
if(resultCode == RESULT_OK) {
Log.e("Message from Z","Message");
}
}
}
Activity Z:
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
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...
}
}
}