Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inside Android Dialog, how to setup onActivityResult for startActivityForResult?

Tags:

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);
like image 424
tony-p-lee Avatar asked Aug 27 '11 01:08

tony-p-lee


People also ask

How do I call onActivityResult in dialog?

MyDialogFragment myDialogFragment = new MyDialogFragment(); myDialogFragment. show(getActivity(). getFragmentManager(), "DialogFragment");

How does onActivityResult work on Android?

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.

How do you implement startActivityForResult?

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 .

What is request code startActivityForResult?

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.


1 Answers

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.

like image 71
telkins Avatar answered Oct 18 '22 20:10

telkins