gpt4 book ai didi

java - 从DialogFragment调用Fragment时没有触发回调函数

转载 作者:行者123 更新时间:2023-12-01 21:26:40 30 4
gpt4 key购买 nike

现在我有:-

1) 1 Activity ,这是从 AppCompactActivity 扩展的主要 Activity 。

2) 1 fragment 类,从 fragment 扩展,这是从主 Activity 调用的 fragment (1) - ProfileTeacherActivity.java

3) 1 fragment 类,从 DialogFragment 扩展,此对话框从 fragment (2) 调用 - ModalBox.java

所以,基本上,这只是一个简单的执行流程。开始时,应用程序显示主 Activity (1) 具有抽屉,其中有一些链接(例如配置文件链接),当单击此链接时,应用程序调用 fragment (2) 通过一个编辑按钮显示配置文件的详细信息。单击编辑按钮后,应用程序将调用 DialogFragment (3),其中包含一些用于编辑用户个人资料的 EditText

我想要实现的是,在编辑用户的个人资料并成功保存到数据库后,我尝试将用户的数据发送回fragment (2)只是为了显示最新更新的信息,不幸的是它没有'不工作。

这是我尝试过的:

1) 在DialogFragment中创建接口(interface)(3) - ModalBox.java

public class ModalBox extends DialogFragment{

....
public interface EditProfileModalBoxInterface {
void onFinishEditProfile( HashMap<String, String> dataPassing );
}
...
...
}

2)在DialogFragment内部我也有.setPositiveButton函数用于OK按钮。 - ModalBox.java

public class ModalBox extends DialogFragment{
...
...
public Dialog onCreateDialog(Bundle savedInstanceState ) {
...
builder
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, int id) {
// At here i'm using retrofit2 http library
// to do updating stuff
// and inside success's callback of retrofit2(asynchronous)
// here i call the below function to send data
// dataToSend is a HashMap value
sendBackResultToParent( dataTosend );
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
.....
}

// Function called inside success's callback of retrofit2
public void sendBackResultToParent( HashMap<String, String> data ) {
// instantiated interface
EditProfileModalBoxInterface ls=(EditProfileModalBoxInterface)getTargetFragment();
// declaring interface's method
ls.onFinishEditProfile( data );
}
}

3) 最后,我在 fragment 中实现了这些接口(interface) (2) - ProfileTeacherActivity.java

public class ProfileTeacherActivity extends Fragment 
implements ModalBox.EditProfileModalBoxInterface{

public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState ) {
.....
.....
}

// At here the interface's method did't triggered
@Override
public void onFinishEditProfile( HashMap dataPassedFromDialog ) {
Toast.makeText(getActivity(), "Testing...." , Toast.LENGTH_LONG).show();
}

}

我现在感到困惑的是,只有当我在retrofit2 success的回调中调用这个函数sendBackResultToParent( dataTosend ); 时才会出现问题,在它之外调用时它会触发。我假设异步调用导致了这一点。如果我可以使用 Promise 或类似的东西,或者有什么解决方法吗?

以下现有解决方案在我的案例中不起作用:

如果上述用例不够清晰或有误解,请向我询问更多信息。感谢您的帮助。问候。

最佳答案

这是一个示例 DialogFragment 代码,用于向选定的联系人发送消息。我也需要捕获 DialogFragment 上的点击事件并重定向。

理想情况下要实现这一目标,这就是需要做的事情

  • 覆盖AlertDialog.Builder的正/负按钮点击并且不执行任何操作
  • 在此之后,使用 getButton 方法提及 AlertDialog.BUTTON_NEGATIVEAlertDialog.BUTTON_POSITIVE 并分配一个操作
<小时/>
public class SMSDialogFrag extends DialogFragment {

private static String one="one";
private EditText messageContent;
private AlertDialog dialog;
private String mobNumber;

public static SMSDialogFrag showDialog(String mobNumber){
SMSDialogFrag customDialogFrag=new SMSDialogFrag();
Bundle bundle=new Bundle();
bundle.putString(one, mobNumber);
customDialogFrag.setArguments(bundle);
return customDialogFrag;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.sms_dialog, null);

alertDialogBuilder.setView(view);
setupUI(view);
alertDialogBuilder.setTitle("");

alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Do nothing here because we override this button later
}
});
alertDialogBuilder.setPositiveButton("Send", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Do nothing here because we override this button later
}
});
dialog = alertDialogBuilder.create();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
//else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
}
});
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage();//IN YOUR USE CASE YOU CAN REDIRECT TO YOUR CALLER FRAGMENT

}
});

return dialog;
}
void setupUI(View view){
TextView textViewMob=(TextView)view.findViewById(R.id.mobNumber);
messageContent=(EditText)view.findViewById(R.id.messageContent);
mobNumber=getArguments().getString(one);
textViewMob.setText("Send message to : "+mobNumber);
}
void sendMessage(){
if( ! TextUtils.isEmpty(messageContent.getText())){
try {
SmsManager smsManager = SmsManager.getDefault();
Log.v(Constants.UI_LOG,"Number >>>>>>> "+mobNumber);
smsManager.sendTextMessage(mobNumber, null, messageContent.getText().toString(), null, null);
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(getActivity(),"Message Sent!",Toast.LENGTH_SHORT).show();
dialog.dismiss();
}else{
Toast.makeText(getActivity(),"Please enter message to send!",Toast.LENGTH_SHORT).show();
}
}
}

关于java - 从DialogFragment调用Fragment时没有触发回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38068674/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com