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);
Related
I have two activities. The second activity is called from the first using startActivityForResult(intent,request_code).
In the second activity I have this code:
Intent i = getIntent();
i.putExtra("data" , some data);
setResult(Activity.RESULT_OK,i);
finish();
Then, to get the data in the first activity I use this code:
#Override
protected void onActivityResult(int requestCode,int resultCode,Intent intent)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==this.uploadRequestCode && resultCode == Activity.RESULT_OK)
{
data = intent.get("data")
}
I start the activity this way:
Intent uploadIntent = new Intent(this,uploadActivity.class)
startActivityForResult(uploadIntent,this.uploadRequestCode)
The problem is that the result code I get is RESULT_CANCELLED even though I set RESULT_OK.
NOTE
I am not talking about a situation when back button is pressed.
UPDATE
I found out that the intent I get in onActivityResult() is null even though I sent an intent, that's why the result code was RESULT_CANCELLED.
Anyone knows why the intent is null?
Maybe this can help you out, something from a project that I have
This how I do start my activity
Intent intent = new Intent(FirstActivity.this, ActivityThatReturnInfo.class);
startActivityForResult(intent, 1);
This is from the activity that I want to return some value
Intent returnIntent = new Intent();
returnIntent.putExtra("VAR",someInfo);
setResult(Activity.RESULT_OK,returnIntent);
finish();
This is on the activity that as started the one that is waiting for the information
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch (requestCode)
{
case 1:
if(resultCode == Activity.RESULT_OK)
{
String result = data.getStringExtra("VAR");
// Code to do
}
break;
default:
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null)
{
if(result.getContents() != null)
{
// Code to do
}
}
break;
}
}
Starting SecondActivity from FirstActivity
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 100);
Sending data to FirstActivity from SecondActivity
Intent intent= new Intent();
intent.putExtra("result",result);
setResult(RESULT_OK,intent);
finish();
getting result in FirstActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 100 && resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
Log.e("Result",result);
}
}
Edit:
Change your code to below and try once.
While sending intent from SecondActivity to FirstActivity Intent i = getIntent(); to Intent intent= new Intent();
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();
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.
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