How to return a result (startActivityForResult) from a TabHost Activity?
Oh, god! After spending several hours and downloading the Android sources, I have finally come to a solution.
If you look at the Activity class, you will see, that finish()
method only sends back the result if there is a mParent
property set to null
. Otherwise the result is lost.
public void finish() {
if (mParent == null) {
int resultCode;
Intent resultData;
synchronized (this) {
resultCode = mResultCode;
resultData = mResultData;
}
if (Config.LOGV) Log.v(TAG, "Finishing self: token=" + mToken);
try {
if (ActivityManagerNative.getDefault()
.finishActivity(mToken, resultCode, resultData)) {
mFinished = true;
}
} catch (RemoteException e) {
// Empty
}
} else {
mParent.finishFromChild(this);
}
}
So my solution is to set result to the parent activity if present, like that:
Intent data = new Intent();
[...]
if (getParent() == null) {
setResult(Activity.RESULT_OK, data);
} else {
getParent().setResult(Activity.RESULT_OK, data);
}
finish();
I hope that will be helpful if someone looks for this problem workaround again.
How to return a result (startActivityForResult) from a Activity to TabHost Activity?
Try using broadcast receivers. You can override onStop() in activity B, broadcast an event from activity B and listen in activity A.
Android Use startActivityForResult from a nested activity in a tab.
Alright, I was able to find a solution to this problem.
First, I created another activity that I started using the basic startActivity() call I called a Result Controller. This does not pass any data back to the tabbed activity which means you don't have to worry about where it's going.
Second, I created a simple static data class which I called DataConnector. The ResultController would get the instance of the DataConnector and insert the Data there
Then, in the original activity(in the tabs) I implemented the onWindowFocusChanged method to determine when the user is back to it. I got the instance of DataConnector and was able to pull the data I needed out of there.
How to manage startActivityForResult on Android
From your FirstActivity
, call the SecondActivity
using the startActivityForResult()
method.
For example:
int LAUNCH_SECOND_ACTIVITY = 1
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, LAUNCH_SECOND_ACTIVITY);
In your SecondActivity
, set the data which you want to return back to FirstActivity
. If you don't want to return back, don't set any.
For example: In SecondActivity
if you want to send back data:
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();
If you don't want to return data:
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
Now in your FirstActivity
class, write the following code for the onActivityResult()
method.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == LAUNCH_SECOND_ACTIVITY) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == Activity.RESULT_CANCELED) {
// Write your code if there's no result
}
}
} //onActivityResult
To implement passing data between two activities in a much better way in Kotlin, please go through 'A better way to pass data between Activities'.
How to get activity result from an Activity in a TabHost?
Again there's no answer to my question. So i have to solve it myself. My solution is ugly but worked. As somebody can guess, we can write a singleton class to store these data. ActivityB will write data and ActivityA will read it. Very easy.
In contrast, i searched in this site, and found some simple solution. Like here
Related Topics
Playing an Arbitrary Tone With Android
Determine If Android App Is Being Used For the First Time
Percentage Width in a Relativelayout
How to Refresh Mediastore on Android
How to Add a Library Project to a Android Project
How to Connect to More Than One Firebase Database from an Android App
Telephonymanager.Getline1Number() Failing
Toggle Airplane Mode in Android
Android Studio Marks R in Red with Error Message "Cannot Resolve Symbol R", But Build Succeeds
How to Disable Recyclerview Scrolling
Show Image View from File Path
Checkbox in Recyclerview Keeps on Checking Different Items
View Contents of Database File in Android Studio
Android Difference Between Two Dates
What Does "Program Type Already Present" Mean