如果不能从 FragmentActivity 扩展,如何在 MainActivity 中监听 Fragment Dialog 的点击?
How to listen for FragmentDialog clicks in the MainActivity if it cannot extend from FragmentActivity?
我试图在我的应用程序中显示一个初始对话框,以便用户接受条款和条件以及隐私政策,从那里在 SharedPreferences 中设置一个变量。
我已按照 steps here 将对话事件传递给主持人,在本例中为 MainActivity
。
对话框创建正常,但在 Fragment 上仍能听到点击声,在主机上听不到。
与我的代码唯一不同的是,在 Android 示例中,MainActivity
是从 FragmentActivity
:
public class MainActivity extends FragmentActivity implements NoticeDialogFragment.NoticeDialogListener{
在我的情况下我不能这样做,因为我的 MainActivity
已经从 AppCompatActivity
扩展了。
如何监听 MainActivity 中 FragmentDialog 的点击?
这是我的代码:
MainActivity
@AndroidEntryPoint
public class MainActivity extends AppCompatActivity implements AcceptFragment.AcceptDialogListener
{
public void showNoticeDialog() {
// Create an instance of the dialog fragment and show it
DialogFragment dialog = new AcceptFragment();
dialog.show(getSupportFragmentManager(), "NoticeDialogFragment");
}
// The dialog fragment receives a reference to this Activity through the
// Fragment.onAttach() callback, which it uses to call the following methods
// defined by the NoticeDialogFragment.NoticeDialogListener interface
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
//I have debugged and it does not enter here
String a="s";
// User touched the dialog's positive button
//...
}
@Override
public void onDialogNegativeClick(DialogFragment dialog) {
//I have debugged and it does not enter here
String a="s";
// User touched the dialog's negative button
//...
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showNoticeDialog();
}
// ...
}
AcceptFragment
这是 FragmentDialog 的代码。在调试中,正在 class 中监听点击,而不是 MainActivity
:
public class AcceptFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = requireActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_signin, null))
// Add action buttons
.setPositiveButton("R.string.signin", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//Clicks are received here
String a="s";
// sign in the user ...
}
})
.setNegativeButton("R.string.cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Or clicks are received here
String a="s";
AcceptFragment.this.getDialog().cancel();
}
});
return builder.create();
}
public static String TAG = "AcceptFragment";
public interface AcceptDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
// Use this instance of the interface to deliver action events
AcceptDialogListener listener;
// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
@Override
public void onAttach(Context context) {
super.onAttach(context);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the host
listener = (AcceptDialogListener) context;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(getActivity().toString()
+ " must implement NoticeDialogListener");
}
}
}
This is the code for the FragmentDialog. In the debug the clicks are being listened to in this class, not in MainActivity:
您似乎没有调用您的侦听器。
.setPositiveButton("R.string.signin", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//Clicks are received here
String a="s";
// sign in the user ...
// INVOKE LISTENER
listener.onPositiveButtonClick(...);
}
})
.setNegativeButton("R.string.cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Or clicks are received here
String a="s";
AcceptFragment.this.getDialog().cancel();
// INVOKE LISTENER
listener.onNegativeButtonClick(...);
}
});
我试图在我的应用程序中显示一个初始对话框,以便用户接受条款和条件以及隐私政策,从那里在 SharedPreferences 中设置一个变量。
我已按照 steps here 将对话事件传递给主持人,在本例中为 MainActivity
。
对话框创建正常,但在 Fragment 上仍能听到点击声,在主机上听不到。
与我的代码唯一不同的是,在 Android 示例中,MainActivity
是从 FragmentActivity
:
public class MainActivity extends FragmentActivity implements NoticeDialogFragment.NoticeDialogListener{
在我的情况下我不能这样做,因为我的 MainActivity
已经从 AppCompatActivity
扩展了。
如何监听 MainActivity 中 FragmentDialog 的点击?
这是我的代码:
MainActivity
@AndroidEntryPoint
public class MainActivity extends AppCompatActivity implements AcceptFragment.AcceptDialogListener
{
public void showNoticeDialog() {
// Create an instance of the dialog fragment and show it
DialogFragment dialog = new AcceptFragment();
dialog.show(getSupportFragmentManager(), "NoticeDialogFragment");
}
// The dialog fragment receives a reference to this Activity through the
// Fragment.onAttach() callback, which it uses to call the following methods
// defined by the NoticeDialogFragment.NoticeDialogListener interface
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
//I have debugged and it does not enter here
String a="s";
// User touched the dialog's positive button
//...
}
@Override
public void onDialogNegativeClick(DialogFragment dialog) {
//I have debugged and it does not enter here
String a="s";
// User touched the dialog's negative button
//...
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showNoticeDialog();
}
// ...
}
AcceptFragment
这是 FragmentDialog 的代码。在调试中,正在 class 中监听点击,而不是 MainActivity
:
public class AcceptFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = requireActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_signin, null))
// Add action buttons
.setPositiveButton("R.string.signin", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//Clicks are received here
String a="s";
// sign in the user ...
}
})
.setNegativeButton("R.string.cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Or clicks are received here
String a="s";
AcceptFragment.this.getDialog().cancel();
}
});
return builder.create();
}
public static String TAG = "AcceptFragment";
public interface AcceptDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
// Use this instance of the interface to deliver action events
AcceptDialogListener listener;
// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
@Override
public void onAttach(Context context) {
super.onAttach(context);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the host
listener = (AcceptDialogListener) context;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(getActivity().toString()
+ " must implement NoticeDialogListener");
}
}
}
This is the code for the FragmentDialog. In the debug the clicks are being listened to in this class, not in MainActivity:
您似乎没有调用您的侦听器。
.setPositiveButton("R.string.signin", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//Clicks are received here
String a="s";
// sign in the user ...
// INVOKE LISTENER
listener.onPositiveButtonClick(...);
}
})
.setNegativeButton("R.string.cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Or clicks are received here
String a="s";
AcceptFragment.this.getDialog().cancel();
// INVOKE LISTENER
listener.onNegativeButtonClick(...);
}
});