Můj kolega a já jsme se pohádali. Jsme překládat aplikaci MVVM+Vázání. Bylo potřeba začít dialog z fragmentu. Dělám to takhle:
Ve fragmentu:
fragmentBinding.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// there's a bunch of code here
alertDialog.show();
}});
Kolega také tvrdí, že je třeba udělat toto:
V xml souboru můžeme napsat takto:
android:onClick="@{v -> viewModel.showDialog(context)}"
A v ViewModel:
fun showDialog(context: Context){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
// there's a bunch of code here
alertDialog.show();
}
Prosím, řekněte mi, který z nich je správný?