From an activity, I can easily setup the onActivityResult()
and call startActivityForResult()
and everything works fine.
Now, I need to call <
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.
On the dialog constructor pass the reference of parent Activity, then you can use in the dialog like this,
parentActivity.startActivityForResult(intent, CODE);
Use the compatibility package then build your dialog using DialogFragment
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
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