可以使用DialogFragment,而不是對話的活動。因爲該對話是其次活動。當你開始用startActivityForResult()的活動,您的對話框被駁回
另一個示例使用回調
創建接口
public interface DialogCallback {
void getResults(String results);
}
創建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");
}
}
在您的活動
@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);
}
}
};
輸出
當您關閉DialogFragment你會看到「你好」登錄您的活動
請提供創建和啓動對話框的代碼。 – Jin35
@ tony-p-lee任何解決方案 –