20

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);
Dheeraj Vepakomma
  • 26,870
  • 17
  • 81
  • 104
tony-p-lee
  • 797
  • 1
  • 7
  • 13

5 Answers5

5

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.

Community
  • 1
  • 1
telkins
  • 10,440
  • 8
  • 52
  • 79
  • This should be marked as answer.This is the only way of doing what you are expecting anyway. – Munim Jan 17 '13 at 19:35
  • 4
    This is not answer. The question is "How to startActivityForResult() from dialog". But not "How to declare activity as dialog." – Cheung Sep 12 '13 at 05:03
3

if your dialog is a dialog fragment you can use

getActivity().startActivityForResult(intent);

in this way the result is sent to the activity that created the dialog

user3044482
  • 401
  • 4
  • 11
2

You can use DialogFragment instead of Dialog. Because The dialog is secondary to its activity. When you start the activity with startActivityForResult(), your dialog gets dismissed

Another Example Use Callback

Create Interface

 public interface DialogCallback {
   void getResults(String results);
 }

Create DialogFragment

public class DialogFragment extends DialogFragment {

DialogCallback dialogCallback;

public DialogFragment setCallBack(DialogCallback dialogCallback){
    this.dialogCallback = dialogCallback;
    return this;
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return super.onCreateDialog(savedInstanceState);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.your_layout, container, false);
    return view;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
    dialogCallback.getResults("hello");
}

}

In your Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     new DialogFragment().setCallBack(dialogCallback).show(getFragmentManager(),"");
}

DialogCallback dialogCallback = new DialogCallback() {
    @Override
    public void getResults(String results) {
        if(results!=null){
            Log.e(TAG,results);
        }
    }
};

Output

When you dismiss the DialogFragment you will see the "hello" Log in your Activity

Munish Kapoor
  • 3,141
  • 2
  • 26
  • 41
1

Use the compatibility package then build your dialog using DialogFragment

Philip Pearl
  • 1,523
  • 16
  • 26
0

On the dialog constructor pass the reference of parent Activity, then you can use in the dialog like this,

parentActivity.startActivityForResult(intent, CODE);
leopragi
  • 441
  • 1
  • 6
  • 16