Multiple activities calling same activity for result - android

Suppose I have 3 activities.
First
Second
Result.
First and Second both call Result activity by startActivityForResult().
In Result, on basis of calling activity, I want to return different results.
Is there any way to decide which one has called Result Activity and return result to that activity.

You can find the calling activity using getCallingActivity ()

You can't have multiple activities on top at the same time. Are you trying to have them run in order, one after the other?
One way to accomplish this is to start each activity for result:
Intent intent = new Intent(this, MyActivity.class);
startActivityForResult(intent, 0);
Where you use the request code to track when activity is running. Then, in onActivityResult you can start the next one:
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if (requestCode < NUM_ACTIVITIES) {
Intent intent = new Intent(this, MyActivity.class);
startActivityForResult(intent, requestCode + 1);
}
}
If you want to have some of the activities immediatly in the background, you can chain them together by calling startActivity in each Activity's onCreate. If you start a new Activity in onCreate before creating any views, the activity will never be visible.
protected void onCreate (Bundle savedInstanceState) {
int numLeft = getIntent().getIntExtra("numLeft");
if (numLeft > 0) {
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("numLeft", numLeft - 1);
startActivity(intent);
}
}

In you calling activity finish the activity with setresult
Intent in = new Intent();
setResult(100, in);
finish();
In your first activity
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Log.i(Tag, "Result: " + Integer.toString(resultCode));
if (resultCode == 200) {
Log.i(Tag, "second");
} else if (resultCode == 100) {
Log.i(Tag, "activity");
}
}

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

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

Why does onActivityResult return, but I am getting the correct requestCode?

I'm building an application where the first Activity starts another one by startActivityByResult. After some setting is done, I need to send the setting result back.
So I override the second Activity's onPause() method, I get the intent, putExra, and send it back through setResult().
Back to the first Activity. onActivityResult has definitely been called. I got the resultCode set before, but the intent data is null. I can't figure out what's wrong.
Here's my code:
The first Activity
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.e("page1", requestCode + "_");
Log.e("page1", resultCode + "_");
Log.e("page1", (data == null) + "_");
// if ((requestCode == 1) && (data != null)) {
if (data != null) {
String size = data.getStringExtra("size");
Log.e("switch", size + "_");
switch (size.charAt(0)) {
case '1': {
text.setTextSize(10);
}
break;
case '2': {
text.setTextSize(20);
}
break;
case '3': {
text.setTextSize(30);
}
break;
}
}
}
My second Activity
#Override
protected void onPause() {
Intent intent = getIntent();
Log.e("prefs", (intent == null) + "_");
intent.putExtra("size", size);
setResult(3, intent);
super.onPause();
}
I've tested in LogCat. In the second Activity. The intent about to be sent is definitely not null. But when it goes to the first Activity. Null just returned. This is driving me really crazy.
Problem with your code is in String size = data.getStringExtra("size"); this line.
You should change it with String size = data.getExtras.getString("size");
In your first activity:
Intent intent = new Intent(context,SecondActivity.class);
startActivityForResult(intent, REQUEST_CODE);
And in your second activity make a button called SAVE and in its listener;
Intent result = new Intent();
Bundle bundle = new Bundle();
bundle.putString("keyName", "KeyValue");
result.putExtras(bundle);
setResult(RESULT_OK,result);
finish();
And again in your first activity's onActivityresult method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode==RESULT_OK){
if(requestCode == REQUEST_CODE){
Log.d("Activity Result", data.getExtras.getString("keyName");
}
}
}
Can you try this.
you want to finish your activity in all the cases in pause manually and in back press automatically so it will be better to save your intent data in onDestroy
#Override
protected void onPause() {
super.onPause();
finish();
}
#Override
public void onDestroy() {
Intent intent = getIntent();
intent.putExtra("size", size);
setResult(3, intent);
super.onDestroy();
}

Categories

Resources