Call startActivityForResult from Fragment doesn't call onActivityResult
Activities that have the attribute noHistory=true
will never have their onActivityResult()
called when launching a new Activity
via startActivityForResult()
. As the documentation mentions, when the noHistory
attribute is set to true
, then finish()
is called on the Activity
when the user navigates away from the Activity
.
So, when startActivityForResult()
is called, the Activity
is navigated away from, causing its finish()
to be called and making it never receive a call to onActivityResult()
. If you remove the noHistory=true
attribute from the Activity
that's calling startActivityForResult()
, then call finish()
in your DialogActivity
's onClick()
, then you should still see the Activity
that launched it, as well as receive a call to onActivityResult()
.
wendigo
Updated on August 20, 2022Comments
-
wendigo over 1 year
I have a
DialogActivity
which is called from aFragment
for show a customDialog
with two image buttons.In
DialogActivity.onCreate
final Dialog dialog = new Dialog(this, R.style.DialogTheme); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.dialog_pause); dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); dialog.show();
In
DialogActivity.onClick
@Override public void onClick(View v) { Log.d(LOGTAG, "onClick CONTINUE"); Intent resultData = new Intent(); resultData.putExtra("TEST", "return data"); setResult(666, resultData); dialog.cancel(); }
In Fragment that calls
startActivityForResult
:Intent dialogActivityIntent = new Intent(getActivity(), DialogActivity.class); startActivityForResult(dialogActivityIntent, 999);
In
Activity
andFragment
that callsstartActivityForResult
:@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); }
When I click the button I only get the dialog cancel and shows the background activity (fragment).
There isn't any call to
onActivityResult
,onResume
, ... in theFragment
or theActivity
contains theFragment
.Things I tried:
To implement
onActivityResult
in both,Fragment
andActivity
that contains myFragment
.Things to know:
I set the attribute
noHistory=true
in everyActivity
I have.If I do
finish()
inonClick
theActivity/Fragment
that callsDialogActivity
is closed too, and the application return to the beforeActivity
.This may be the problem, I DON'T call
finish()
... but if I callfinish()
, it exits to anotherActivity
, not theActivity
that callsstartActivityForResult
.Links I checked:
Cannot get to trigger onActivityResult() android?
startActivityForResult doesn't seem to call onActivityResult
Android onActivityResult NEVER called
onActivityResult() not called when Activity started from Fragment
I hope everything is clearly explained ^^.
Thanks in advance.
-
wendigo almost 11 yearsSorry for the late comment... I solved this problem some days later doing what you say. Thanks!
-
dcool over 10 yearsgreat @ashu.. it solved my two problems first my activity was getting closed as soon as display turned off secondly the same problem mentioned in the question..thanks for the help.
-
Admin over 2 yearsYour answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-
ericmas001 over 2 yearsThis does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review