From an activity, I can easily setup the onActivityResult()
and call startActivityForResult()
and everything works fine.
Now, I need to call startActivityForResult()
from the Dialog. But I can't setup the onActivityResult()
, I believe Dialog
is not an Activity
.
How do I get the result?
I try something like this inside a dialog but it failed.
//create new Intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, m_PicUri);
((Activity) getContext()).startActivityForResult(intent, Const.TAKE_PIC_ACTIVITY_RET_CODE);
MyDialogFragment myDialogFragment = new MyDialogFragment(); myDialogFragment. show(getActivity(). getFragmentManager(), "DialogFragment");
Receive The Result:When you done with the subsequent activity and returns, the system calls your activity's onActivityResult() method. This method includes three arguments: @The request code you passed to startActivityForResult() . @A result code specified by the second activity.
First you use startActivityForResult() with parameters in the first Activity and if you want to send data from the second Activity to first Activity then pass the value using Intent with the setResult() method and get that data inside the onActivityResult() method in the first Activity .
The request code is any int value. The request code identifies the return result when the result arrives. ( You can call startActivityForResult more than once before you get any results. When results arrive, you use the request code to distinguish one result from another.
You can declare your Activity
to have a Dialog
theme. Look into this SO question: Android Activity as a dialog
You would change this in your AndroidManifest.xml
file:
<activity android:theme="@android:style/Theme.Dialog" />
You should be able to use startActivityForResult()
like normal. I know the BluetoothChat
example Android program uses something similar to return the Bluetooth device that you choose from a Dialog
list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With