inside Android Dialog, how to setup onActivityResult for startActivityForResult?

后端 未结 5 1456
一个人的身影
一个人的身影 2021-02-18 21:44

From an activity, I can easily setup the onActivityResult() and call startActivityForResult() and everything works fine.

Now, I need to call <

相关标签:
5条回答
  • 2021-02-18 22:12

    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.

    0 讨论(0)
  • 2021-02-18 22:29

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

    parentActivity.startActivityForResult(intent, CODE);
    
    0 讨论(0)
  • 2021-02-18 22:31

    Use the compatibility package then build your dialog using DialogFragment

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-02-18 22:34

    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

    0 讨论(0)
提交回复
热议问题