Как приостановить выполнение кода при выполнении DialogFragment
У меня есть DialogFragment и фрагмент из которого он запускается. Во фрагмент есть метод, который должен выполнятся при нажатии на определенную кнопку в диалоге. AlertDialog мне не подходит так как я хочу сделать свою разметку, setView в нем тоже, хочу закруглить углы. Я так понимаю DialogFragment выполняется не в UI потоке и поэтому код продолжает выполняться. Пытался выполнить метод внутри DialogFragment, но мне как-то нужно обновить recyclerview после нажатия на кнопку в диалоге. Может это как то можно реализовать через интерфейс или хэндлер? Или как правильно задержать выполнение кода?
Код диалога:
@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
builder = new Dialog(getContext());
builder.setTitle(R.string.ask);
builder.setContentView(R.layout.dialog);
builder.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
builder.getWindow().setLayout(420, 230);
activity = ((MainActivity2) getActivity());
Button btnReopenId = builder.findViewById(R.id.button2);
Button btnCancelId = builder.findViewById(R.id.button4);
ImageView image = builder.findViewById(R.id.im);
image.setImageResource(R.drawable.ic_baseline_warning_24);
btnReopenId.setOnClickListener(this);
btnCancelId.setOnClickListener(this);
return builder;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button4:
SQLiteOpenHelper exe = new ExeDatabase(getContext());
SQLiteDatabase db = exe.getWritableDatabase();
db.delete("EXE", "NAME=?", new String[]{bs});
db.close();
builder.dismiss();
break;
case R.id.button2:
builder.dismiss();
break;
}
}
}
Код фрагмента:
public void onClick5(int position, String bs, View v) {
if(download[position]==null){
CustomDialogFragment dialog = new CustomDialogFragment(2, bs);
dialog.show(activity.getSupportFragmentManager(), "custom");
}
findall(positio);
adapter.changeDatass(nameArray, imageArray, download, favorite, position); // Два метода что должны выполнится после.
Ответы (1 шт):
Вот вырвал из проекта, смотри. Диалог создаётся прям в теле фрагмента, ничего никуда передавать не надо.
/*-------Dialog_Edit_2_Buttons-----*/
final EditText edText;
String sBtn1 = "Ок";
String sBtn2 = "Отмена";
final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setCancelable(false);
final View v1 = View.inflate(this, R.layout.dialog_edit, null);
dialog.setView(v1);
final AlertDialog alert = dialog.create();
TextView tv1 = v1.findViewById(R.id.tv1);
tv1.setText(c6.getText().toString());
edText = v1.findViewById(R.id.edText);
edText.setText(textb);
edText.setSelection(edText.getText().length()); // курсор в конец текста
Button btn1 = v1.findViewById(R.id.btn1);
btn1.setText(sBtn1);
Button btn2 = v1.findViewById(R.id.btn2);
btn2.setText(sBtn2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v1) {
// кнопка 1 ----------
try {
E6 = Double.valueOf(edText.getText().toString().replace(" ", ""));
alert.dismiss();
if (!bDemo)
update_MyOrdersTable();
} catch (NumberFormatException ex) {
toast.setText("Введите число");
toast.show();
}
}
});//----------
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v1) {
// кнопка 2 ---------
alert.dismiss();
}
});//--------
alert.show();
dialog_edit.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:padding="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/qudrat"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:id="@+id/linearLayout_7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:gravity="center|clip_horizontal"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:padding="2dp">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|center_vertical"
android:text="text"
android:textColor="@color/black"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="15dp" />
<LinearLayout
android:id="@+id/lL1edText"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_margin="1dp"
android:background="@drawable/qudrat"
android:gravity="center|clip_horizontal"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/edittextstyle"
android:orientation="horizontal">
<EditText
android:id="@+id/edText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:ems="10"
android:gravity="center"
android:inputType="numberSigned|numberDecimal"
android:maxLines="1"
android:textColor="@color/black"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="15dp" />
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="bottom|clip_horizontal"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:padding="2dp">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:gravity="center|center_vertical"
android:text="Oк"
android:textColor="@color/white"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:padding="2dp">
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:gravity="center|center_vertical"
android:text="Отмена"
android:textColor="@color/white"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
