Is it allowed to have a chain of onActivityResult's? - android

Suppose I have a main activity A and two other activities B and C.
A launches intent B and at some point B launches intent C.
Then C sets setResult(...) and finish()'s, as does B, finally ending at onActivityResult(...) in A.
Is this allowed; will it work?

Yes, it will work. Just finish activity B when recieve the result from C. However the result from activity C will not be propagated when finishing B, you will have to set it manually if necessary.

In activity A:
Intent intent = new Intent(this, B.class);
startActivityForResult(intent, 1);
....
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1){
if (resultCode == RESULT_OK){
Bundle bundle = data.getExtras();
String code = "";
try{
code = bundle.getString("code");
} catch (Exception e){
e.printStackTrace();
}
if (!code.equals("")){
//do something
}
}
}
In Activity B:
Intent intent = new Intent(this, C.class);
startActivityForResult(intent, 1);
...
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1){
if (resultCode == RESULT_OK){
Bundle bundle = data.getExtras();
String code = "";
try{
code = bundle.getString("code");
} catch (Exception e){
e.printStackTrace();
}
if (!code.equals("")){
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("code", code);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
}
}
}
Then in Activity C at some point:
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("code", code);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
I hope this can help you.

Yes it will work. Here's an overview of the process you probably just need:
Activity A {
startActivityForResult() // start activity B
onActivityResult() // receive B's result
}
Activity B {
getIntentFromA()
startActivityForResult(); // start activity C
onActivityResult() {
// receive C's result
setResult(c's result); <-- this will pass back to A
}
}
Activity C {
getIntentFromB()
setResult(c's result); <-- this will pass back to B
}

Related

OnActivityResult get null as the intent passed even though I sent an intent

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

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

how to receive data in previous activity in Android

I want to receive data in previous Activity from next activity like (1 <--- 2 ). I tried but data is not received from second to first activity .
This is First Cativity
Intent i = new Intent(CustomActionActivity.this, Edit_Post.class);
i.putExtra("ActivityId", getItemActivity);
i.putExtra("Vessel", strVesselName);
i.putExtra("HashTag", strHashTag);
i.putExtra("RemarkTitle", strRemark);
i.putExtra("ShortRGN", strShortTypeRGN);
i.putExtra("VessId", strvesselid);
startActivity(i);
This is second Activity
Intent intent = getIntent();
strActivityId = intent.getStringExtra("ActivityId");
strVesselName = intent.getStringExtra("Vessel");
strHashTag = intent.getStringExtra("HashTag");
strRemark = intent.getStringExtra("RemarkTitle");
strShortRGN = intent.getStringExtra("ShortRGN");
strVessId = intent.getStringExtra("VessId");
img_AddPostAudio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Edit_Post.this, EditRecord.class);
i.putExtra("EditVesselId", strVessId);
i.putExtra("EditActivityId" , strActivityId);
startActivity(i);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_CODE)
{
String audioString=data.getStringExtra("AUDIO_STRING");
Log.e(" audioString "," = "+audioString);
}
}
This is Third Activity
Intent intent = getIntent();
vesselId = intent.getStringExtra("EditVesselId");
strActivityId = intent.getStringExtra("EditActivityId");
Intent intent=new Intent(EditRecord.this, Edit_Post.class);
intent.putExtra("AUDIO_STRING",newAudioFile);
setResult(REQUEST_CODE, intent);
finish();
do this when you are calling the second activity
Intent i = new Intent(CustomActionActivity.this, Edit_Post.class);
i.putExtra("HashTag", strHashTag);
startActivityForResult(i, REQUEST_CODE);
Now you need to set the result what you want on CustomActionActivity
e.g.
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(REQUEST_CODE,intent);
finish();
Now you will get this data to the your first activity
e.g.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_CODE)
{
String message=data.getStringExtra("MESSAGE");
textView1.setText(message);
}
}
let me know in case of any issue
Use SharedPreferences
Store the data in a SharedPreference and access it in from the SharedPreference in the other activity.
Add the data to the SharedPreference in the onStop for the 2nd Activity and access it in the onCreate of the other Activity
#Override
public void onStop(){//Where you wish to insert data
SharedPreferences data=getSharedPreferences(PREFS_FILE,0);
SharedPreferences.Editor editor= count.edit();
editor.put("data","DATA");
editor.apply();
super.onStop();
}
in onCreate() of the other Activity:
SharedPreferences data = getApplicationContext().getSharedPreferences(PREFS_FILE, 0);
String dataString=data.get("Data",0);
Hope this helps. Cheers.
I am doing this way its works for me:
Activity 2:
public void onBackPressed() {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("MESSAGE", strtext + "");
setResult(2, intent);
if (isclose) {
finish();
} else {
super.onBackPressed();
}
}
}
Activity1:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String sSuName = data.getStringExtra("MESSAGE");
//txtfavouratecount.setText(sSuName);
}
in Your onclick listener
Intent itemintent = new Intent(context,your target Activity.class);
Bundle b = new Bundle();
b.putStringArray("iarray", Qtag);
b.putInt("mflag", 0);
itemintent.putExtra("android.intent.extra.INTENT", b);
startActivityForResult(itemintent,2);
I would do something like this:
In Activity A:
private static final int REQUEST_CODE = 9001;
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, REQUEST_CODE);
In Activity B:
Intent data = new Intent();
data.putExtra("key", parameter);
setResult(CommonStatusCodes.SUCCESS, data);
finish();
And finally, in the Activity A, receive result:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == CommonStatusCodes.SUCCESS) {
if (data != null) {
String result = data.getStringExtra("key");
}
} else {
//Error to receive data
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
Good luck!

How can I send data to ble device from second or third activity of the same program

I can send data from the first activity but on repeating the same procedure on the second activity to send data to ble device is not successful. How can I send data from second activity?
use this to save
Intent intent = new Intent(FirstScreen.this, SecondScreen.class)
intent .putExtra(strName, keyIdentifer );
use this to get
String newString;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("STRING_I_NEED");
}
} else {
newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}
If you just want to send data to the next activity, use
Intent intent = new Intent(FirstActivity.this, SecondActivity.class)
intent.putExtra("id_for_value", value);
startActivity(intent);
And recover it with
value= getIntent().getExtras().getString("id_for_value");//if it is a string
OR
If you want to send data back from the Second activity back to the Previous, you have to use start activity for results
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent, 2)//where 2 is the request code
finish();
Again in the FirstActivity, overide this
#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 result=data.getStringExtra("ResultId");
}
}
And in your PreviousActivity, you pass the data like this
Intent intent=new Intent();
intent.putExtra("ResultId",message);
setResult(2,intent);
finish();

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.

Categories

Resources