Fragment は FragmentManager によって管理されており、 FragmentManager には管理している Fragment を得るメソッドがあります。既存のフラグメントを見つける
●●の4行上ところで、アクティビティを取得したように、●●の箇所でフラグメントを取得してflag_count_frの値を直接フラグメントに送りたい
ダイアログの結果として、その表示元である Activity か Fragment に true を送るという処理でしょうか。
最近拡張された FragmentManager の機能により、Bundle を介してデータを受け取れるようです。
MainActivity.java
java
1import androidx.appcompat.app.AppCompatActivity;
2import androidx.fragment.app.FragmentManager;
3
4import android.os.Bundle;
5import android.util.Log;
6import android.widget.Button;
7
8public class MainActivity extends AppCompatActivity {
9 private static final String LOGTAG = "MainActivity";
10 private static final String TAG_MAINFRAGMENT = "MAIN_FRAGMENT";
11 private static final String REQUESTKEY_MAINDIALOG = "FROM_ACTIVITY";
12
13 @Override
14 protected void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.activity_main);
17
18 FragmentManager fragmentManager = getSupportFragmentManager();
19
20 fragmentManager.setFragmentResultListener(REQUESTKEY_MAINDIALOG, this, (requestKey, result) -> {
21 Log.d(LOGTAG, "dialog result="+result.getBoolean(MainDialog.RESULTKEY_FLAG_COUNT));
22 });
23
24 Button showDialog = findViewById(R.id.showDialog);
25 showDialog.setOnClickListener(v -> {
26 Log.d(LOGTAG, "click");
27 MainDialog.newInstance(REQUESTKEY_MAINDIALOG).show(fragmentManager, null);
28 });
29
30 //回転等による自動再生成時には replace は行わない
31 if(fragmentManager.findFragmentByTag(TAG_MAINFRAGMENT) == null) {
32 fragmentManager.beginTransaction()
33 .replace(R.id.fragmentBase, new MainFragment(), TAG_MAINFRAGMENT)
34 .commit();
35 }
36 }
37}
res/layout/activity_main.xml
xml
1<?xml version="1.0" encoding="utf-8"?>
2<androidx.constraintlayout.widget.ConstraintLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 xmlns:app="http://schemas.android.com/apk/res-auto"
5 xmlns:tools="http://schemas.android.com/tools"
6 android:layout_width="match_parent"
7 android:layout_height="match_parent"
8 tools:context=".MainActivity">
9
10 <Button
11 android:id="@+id/showDialog"
12 android:layout_width="0dp"
13 android:layout_height="0dp"
14 android:text="showDialog(Activity)"
15 app:layout_constraintBottom_toTopOf="@id/fragmentBase"
16 app:layout_constraintLeft_toLeftOf="parent"
17 app:layout_constraintRight_toRightOf="parent"
18 app:layout_constraintTop_toTopOf="parent" />
19
20 <LinearLayout
21 android:id="@+id/fragmentBase"
22 android:layout_width="0dp"
23 android:layout_height="0dp"
24 android:orientation="vertical"
25 app:layout_constraintBottom_toBottomOf="parent"
26 app:layout_constraintLeft_toLeftOf="parent"
27 app:layout_constraintRight_toRightOf="parent"
28 app:layout_constraintTop_toBottomOf="@id/showDialog" />
29
30</androidx.constraintlayout.widget.ConstraintLayout>
MainFragment.java
java
1import android.os.Bundle;
2import android.util.Log;
3import android.view.View;
4import android.widget.Button;
5
6import androidx.annotation.*;
7import androidx.fragment.app.*;
8
9public class MainFragment extends Fragment {
10 private static final String LOGTAG = "MainFragment";
11 private static final String REQUESTKEY_MAINDIALOG = "FROM_FRAGMENT";
12
13 public MainFragment() {
14 super(R.layout.fragment_main);
15 }
16
17 @Override
18 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
19 super.onViewCreated(view, savedInstanceState);
20
21 FragmentManager fragmentManager = getChildFragmentManager();
22
23 fragmentManager.setFragmentResultListener(REQUESTKEY_MAINDIALOG, getViewLifecycleOwner(), (requestKey, result) -> {
24 Log.d(LOGTAG, "dialog result="+result.getBoolean(MainDialog.RESULTKEY_FLAG_COUNT));
25 });
26
27 Button showDialog = view.findViewById(R.id.showDialog);
28 showDialog.setOnClickListener(v -> {
29 Log.d(LOGTAG, "click");
30 MainDialog.newInstance(REQUESTKEY_MAINDIALOG).show(fragmentManager, null);
31 });
32 }
33}
res/layout/fragment_main.xml
xml
1<?xml version="1.0" encoding="utf-8"?>
2<androidx.constraintlayout.widget.ConstraintLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 xmlns:app="http://schemas.android.com/apk/res-auto"
5 xmlns:tools="http://schemas.android.com/tools"
6 android:layout_width="match_parent"
7 android:layout_height="match_parent"
8 tools:context=".MainFragment">
9
10 <Button
11 android:id="@+id/showDialog"
12 android:layout_width="match_parent"
13 android:layout_height="match_parent"
14 android:text="showDialog(Fragment)"
15 app:layout_constraintBottom_toBottomOf="parent"
16 app:layout_constraintLeft_toLeftOf="parent"
17 app:layout_constraintRight_toRightOf="parent"
18 app:layout_constraintTop_toTopOf="parent" />
19
20</androidx.constraintlayout.widget.ConstraintLayout>
MainDialog.java
java
1import android.app.*;
2import android.os.Bundle;
3
4import androidx.annotation.*;
5import androidx.fragment.app.DialogFragment;
6
7public class MainDialog extends DialogFragment {
8 static final String ARGS_REQUEST_KEY = "REQUEST_KEY";
9 static final String RESULTKEY_FLAG_COUNT = "FLAG_COUNT";
10
11 static MainDialog newInstance(@Nullable String requestKey) {
12 MainDialog dialog = new MainDialog();
13 Bundle args = new Bundle();
14 args.putString(ARGS_REQUEST_KEY, requestKey);
15 dialog.setArguments(args);
16 return dialog;
17 }
18
19 @NonNull
20 @Override
21 public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
22 return new AlertDialog.Builder(getContext())
23 .setTitle("ダイアログ")
24 .setPositiveButton("True", (dialog, which) -> setFragmentResult(true))
25 .setNegativeButton("False", (dialog, which) -> setFragmentResult(false))
26 .create();
27 }
28
29 private void setFragmentResult(boolean flag_count) {
30 Bundle bundle = new Bundle();
31 bundle.putBoolean(RESULTKEY_FLAG_COUNT, flag_count);
32 String requestKey = getArguments().getString(ARGS_REQUEST_KEY, "MainDialog");
33 getParentFragmentManager().setFragmentResult(requestKey, bundle);
34 }
35}
plain
1D/MainActivity: click
2D/MainActivity: dialog result=true
3D/MainActivity: click
4D/MainActivity: dialog result=false
5D/MainFragment: click
6D/MainFragment: dialog result=true
7D/MainFragment: click
8D/MainFragment: dialog result=false