回答編集履歴
3
コード修正
test
CHANGED
@@ -2,9 +2,13 @@
|
|
2
2
|
|
3
3
|
例えば、会話風にダイアログを表示するとしますと、こんな感じに出来ます。
|
4
4
|
|
5
|
+
"NEXT" をクリックしなくても一定時間(4秒)で自動で進む "AUTOモード" を付けてみました。
|
6
|
+
ダイアログ左下の "AUTO-ON" / "AUTO-OFF" で切り替えられます。
|
7
|
+
|
5
8
|
MainActivity.java
|
6
9
|
```java
|
7
10
|
import android.os.Bundle;
|
11
|
+
import android.util.Log;
|
8
12
|
|
9
13
|
import androidx.appcompat.app.AppCompatActivity;
|
10
14
|
import androidx.fragment.app.FragmentManager;
|
@@ -19,6 +23,8 @@
|
|
19
23
|
"B:建物は古いけど、安くていいよ。",
|
20
24
|
"A:へー。"
|
21
25
|
};
|
26
|
+
private static String REQUEST_KEY = "Conversation";
|
27
|
+
private static String DIALOG_TAG = "Dialog";
|
22
28
|
|
23
29
|
@Override
|
24
30
|
protected void onCreate(Bundle savedInstanceState) {
|
@@ -27,19 +33,22 @@
|
|
27
33
|
|
28
34
|
FragmentManager fm = getSupportFragmentManager();
|
29
35
|
|
30
|
-
fm.setFragmentResultListener(
|
36
|
+
fm.setFragmentResultListener(REQUEST_KEY, this, (requestKey, result) -> {
|
31
|
-
int index = result.getInt(ConversationFragment.RESULT_INDEX);
|
37
|
+
int index = result.getInt(ConversationFragment.RESULT_NEXT_INDEX);
|
32
|
-
index ++;
|
33
38
|
if(index < conversation.length) {
|
34
|
-
|
39
|
+
boolean automode = result.getBoolean(ConversationFragment.RESULT_AUTOMODE);
|
35
|
-
ConversationFragment dialog = ConversationFragment.createInstance(
|
40
|
+
ConversationFragment dialog = ConversationFragment.createInstance(REQUEST_KEY, conversation[index], index, conversation.length, automode);
|
36
|
-
dialog.show(fm,
|
41
|
+
dialog.show(fm, DIALOG_TAG);
|
42
|
+
return;
|
37
43
|
}
|
44
|
+
|
45
|
+
//会話が終わったらここに実行が来るので、終わった時の処理はここに書く
|
46
|
+
Log.d("MainActivity", "会話終了");
|
38
47
|
});
|
39
48
|
|
40
|
-
if(fm.findFragmentByTag(
|
49
|
+
if(fm.findFragmentByTag(DIALOG_TAG) == null) {
|
41
|
-
ConversationFragment dialog = ConversationFragment.createInstance(
|
50
|
+
ConversationFragment dialog = ConversationFragment.createInstance(REQUEST_KEY, conversation[0], 0, conversation.length);
|
42
|
-
dialog.show(fm,
|
51
|
+
dialog.show(fm, DIALOG_TAG);
|
43
52
|
}
|
44
53
|
}
|
45
54
|
}
|
@@ -47,7 +56,9 @@
|
|
47
56
|
ConversationFragment.java
|
48
57
|
```java
|
49
58
|
import android.app.Dialog;
|
59
|
+
import android.content.DialogInterface;
|
50
|
-
import android.os.
|
60
|
+
import android.os.*;
|
61
|
+
import android.view.View;
|
51
62
|
|
52
63
|
import androidx.annotation.*;
|
53
64
|
import androidx.appcompat.app.AlertDialog;
|
@@ -57,17 +68,27 @@
|
|
57
68
|
private static final String ARGS_REQUEST_KEY = "requestKey";
|
58
69
|
private static final String ARGS_MESSAGE = "message";
|
59
70
|
private static final String ARGS_INDEX = "index";
|
60
|
-
private static final String ARGS_
|
71
|
+
private static final String ARGS_END_OF_INDEX = "endOfIndex";
|
72
|
+
private static final String ARGS_AUTOMODE = "automode";
|
61
73
|
|
62
|
-
p
|
74
|
+
private static final int AUTOMODE_DELAY = 4000; //[ms]
|
63
75
|
|
76
|
+
public static final String RESULT_NEXT_INDEX = "nextIndex";
|
77
|
+
public static final String RESULT_AUTOMODE = "automode";
|
78
|
+
|
79
|
+
@NonNull
|
64
|
-
static ConversationFragment createInstance(String requestKey, String message, int index,
|
80
|
+
static ConversationFragment createInstance(@NonNull String requestKey, @NonNull String message, int index, int endOfIndex) {
|
81
|
+
return createInstance(requestKey, message, index, endOfIndex, false);
|
82
|
+
}
|
83
|
+
@NonNull
|
84
|
+
static ConversationFragment createInstance(@NonNull String requestKey, @NonNull String message, int index, int endOfIndex, boolean automode) {
|
65
85
|
ConversationFragment fragment = new ConversationFragment();
|
66
86
|
Bundle args = new Bundle();
|
67
87
|
args.putString(ARGS_REQUEST_KEY, requestKey);
|
68
88
|
args.putString(ARGS_MESSAGE, message);
|
69
89
|
args.putInt(ARGS_INDEX, index);
|
70
|
-
args.put
|
90
|
+
args.putInt(ARGS_END_OF_INDEX, endOfIndex);
|
91
|
+
args.putBoolean(ARGS_AUTOMODE, automode);
|
71
92
|
fragment.setArguments(args);
|
72
93
|
return fragment;
|
73
94
|
}
|
@@ -77,17 +98,78 @@
|
|
77
98
|
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
|
78
99
|
Bundle args = getArguments();
|
79
100
|
String message = args.getString(ARGS_MESSAGE);
|
101
|
+
int index = args.getInt(ARGS_INDEX);
|
80
|
-
|
102
|
+
int endOfIndex = args.getInt(ARGS_END_OF_INDEX);
|
103
|
+
|
81
|
-
r
|
104
|
+
AlertDialog dialog = new AlertDialog.Builder(getContext())
|
82
105
|
.setMessage(message)
|
83
|
-
.setPositiveButton(
|
106
|
+
.setPositiveButton(index+1<endOfIndex ? "Next" : "End", (d, w) -> setFragmentResult(args, index+1, false))
|
84
|
-
String requestKey = args.getString(ARGS_REQUEST_KEY);
|
85
|
-
int index = args.getInt(ARGS_INDEX);
|
86
|
-
Bundle result = new Bundle();
|
87
|
-
result.putInt(RESULT_INDEX, index);
|
88
|
-
|
107
|
+
.setNegativeButton("Skip", (d, w) -> setFragmentResult(args, endOfIndex, false))
|
89
|
-
|
108
|
+
.setNeutralButton("-", null) //NEUTRALボタンを生成するためのダミー
|
90
109
|
.create();
|
110
|
+
|
111
|
+
dialog.setOnShowListener(new AutomodeManager(args));
|
112
|
+
|
113
|
+
return dialog;
|
114
|
+
}
|
115
|
+
|
116
|
+
private class AutomodeManager implements DialogInterface.OnShowListener, View.OnClickListener {
|
117
|
+
private AlertDialog dialog;
|
118
|
+
private Bundle args;
|
119
|
+
private boolean automode;
|
120
|
+
private Handler handler;
|
121
|
+
private Runnable callback;
|
122
|
+
|
123
|
+
AutomodeManager(@NonNull Bundle args) {
|
124
|
+
this.args = args;
|
125
|
+
automode = args.getBoolean(ARGS_AUTOMODE);
|
126
|
+
handler = new Handler(requireActivity().getMainLooper());
|
127
|
+
}
|
128
|
+
|
129
|
+
@Override
|
130
|
+
public void onShow(DialogInterface dialog) {
|
131
|
+
this.dialog = (AlertDialog)dialog;
|
132
|
+
this.dialog.getButton(AlertDialog.BUTTON_NEUTRAL).setOnClickListener(this); //押してもダイアログがクローズしないように, 直接リスナを登録
|
133
|
+
setButtonsState();
|
134
|
+
if(automode) postClickEvent();
|
135
|
+
}
|
136
|
+
|
137
|
+
@Override
|
138
|
+
public void onClick(View view) {
|
139
|
+
automode = !automode;
|
140
|
+
setButtonsState();
|
141
|
+
if(automode) {
|
142
|
+
postClickEvent();
|
143
|
+
} else {
|
144
|
+
cancelClickEvent();
|
145
|
+
}
|
146
|
+
}
|
147
|
+
|
148
|
+
private void setButtonsState() {
|
149
|
+
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(!automode);
|
150
|
+
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setEnabled(!automode);
|
151
|
+
dialog.getButton(AlertDialog.BUTTON_NEUTRAL).setText(automode ? "Auto-OFF" : "Auto-ON");
|
152
|
+
}
|
153
|
+
|
154
|
+
private void postClickEvent() {
|
155
|
+
callback = () -> {
|
156
|
+
setFragmentResult(args, args.getInt(ARGS_INDEX)+1, true);
|
157
|
+
dialog.dismiss();
|
158
|
+
};
|
159
|
+
handler.postDelayed(callback, AUTOMODE_DELAY); //[ms]
|
160
|
+
}
|
161
|
+
|
162
|
+
private void cancelClickEvent() {
|
163
|
+
handler.removeCallbacks(callback);
|
164
|
+
}
|
165
|
+
}
|
166
|
+
|
167
|
+
private void setFragmentResult(@NonNull Bundle args, int nextIndex, boolean automode) {
|
168
|
+
String requestKey = args.getString(ARGS_REQUEST_KEY);
|
169
|
+
Bundle result = new Bundle();
|
170
|
+
result.putInt(RESULT_NEXT_INDEX, nextIndex);
|
171
|
+
result.putBoolean(RESULT_AUTOMODE, automode);
|
172
|
+
getParentFragmentManager().setFragmentResult(requestKey, result);
|
91
173
|
}
|
92
174
|
}
|
93
175
|
```
|
2
全面変更
test
CHANGED
@@ -1,225 +1,93 @@
|
|
1
|
-
|
1
|
+
※コメントにより、趣旨が変わってしまいますが回答がごちゃごちゃになりそうですので書き直します。
|
2
|
+
|
3
|
+
例えば、会話風にダイアログを表示するとしますと、こんな感じに出来ます。
|
2
4
|
|
3
5
|
MainActivity.java
|
4
6
|
```java
|
5
7
|
import android.os.Bundle;
|
6
|
-
import android.widget.EditText;
|
7
8
|
|
8
9
|
import androidx.appcompat.app.AppCompatActivity;
|
9
|
-
import androidx.
|
10
|
+
import androidx.fragment.app.FragmentManager;
|
10
11
|
|
11
12
|
public class MainActivity extends AppCompatActivity {
|
13
|
+
private static final String conversation[] = {
|
14
|
+
"A:休みの日にどこか行きたいんですが、いいところありますか?",
|
15
|
+
"B:うーん、A さんはスポーツが好き?",
|
16
|
+
"A:はい。",
|
17
|
+
"B:××スポーツセンターとかはどう?",
|
18
|
+
"A:スポーツセンター?",
|
19
|
+
"B:建物は古いけど、安くていいよ。",
|
20
|
+
"A:へー。"
|
21
|
+
};
|
12
22
|
|
13
23
|
@Override
|
14
24
|
protected void onCreate(Bundle savedInstanceState) {
|
15
25
|
super.onCreate(savedInstanceState);
|
16
26
|
setContentView(R.layout.activity_main);
|
17
27
|
|
18
|
-
|
28
|
+
FragmentManager fm = getSupportFragmentManager();
|
19
29
|
|
20
|
-
EditText editText = findViewById(R.id.editText);
|
21
|
-
f
|
30
|
+
fm.setFragmentResultListener("Conversation", this, (requestKey, result) -> {
|
31
|
+
int index = result.getInt(ConversationFragment.RESULT_INDEX);
|
32
|
+
index ++;
|
33
|
+
if(index < conversation.length) {
|
34
|
+
String positiveText = index == conversation.length-1 ? "終了" : "次へ";
|
35
|
+
ConversationFragment dialog = ConversationFragment.createInstance("Conversation", conversation[index], index, positiveText);
|
36
|
+
dialog.show(fm, "Conversation");
|
37
|
+
}
|
38
|
+
});
|
22
39
|
|
23
|
-
|
40
|
+
if(fm.findFragmentByTag("Conversation") == null) {
|
41
|
+
ConversationFragment dialog = ConversationFragment.createInstance("Conversation", conversation[0], 0, "次へ");
|
24
|
-
|
42
|
+
dialog.show(fm, "Conversation");
|
25
|
-
.replace(R.id.fragment2, new BFragment())
|
26
|
-
|
43
|
+
}
|
27
44
|
}
|
28
45
|
}
|
29
46
|
```
|
30
|
-
r
|
47
|
+
ConversationFragment.java
|
31
|
-
```
|
48
|
+
```java
|
32
|
-
<?xml version="1.0" encoding="utf-8"?>
|
33
|
-
<androidx.constraintlayout.widget.ConstraintLayout
|
34
|
-
xmlns:android="http://schemas.android.com/apk/res/android"
|
35
|
-
xmlns:app="http://schemas.android.com/apk/res-auto"
|
36
|
-
xmlns:tools="http://schemas.android.com/tools"
|
37
|
-
android:layout_width="match_parent"
|
38
|
-
android:layout_height="match_parent"
|
39
|
-
|
49
|
+
import android.app.Dialog;
|
50
|
+
import android.os.Bundle;
|
40
51
|
|
41
|
-
<LinearLayout
|
42
|
-
android:id="@+id/activity"
|
43
|
-
|
52
|
+
import androidx.annotation.*;
|
44
|
-
android:layout_height="wrap_content"
|
45
|
-
android:orientation="vertical"
|
46
|
-
android:background="#ff80ffff"
|
47
|
-
app:layout_constraintLeft_toLeftOf="parent"
|
48
|
-
app:layout_constraintRight_toLeftOf="@id/arrow"
|
49
|
-
app:layout_constraintTop_toTopOf="parent">
|
50
|
-
<TextView
|
51
|
-
|
53
|
+
import androidx.appcompat.app.AlertDialog;
|
52
|
-
android:layout_height="wrap_content"
|
53
|
-
android:text="ACTIVITY"
|
54
|
-
android:textSize="20dp" />
|
55
|
-
<EditText
|
56
|
-
android:id="@+id/editText"
|
57
|
-
android:layout_width="match_parent"
|
58
|
-
android:layout_height="wrap_content"
|
59
|
-
|
54
|
+
import androidx.fragment.app.DialogFragment;
|
60
|
-
<Button
|
61
|
-
android:id="@+id/setButton"
|
62
|
-
android:text="SET"
|
63
|
-
android:layout_width="wrap_content"
|
64
|
-
android:layout_height="wrap_content" />
|
65
|
-
</LinearLayout>
|
66
55
|
|
67
|
-
<LinearLayout
|
68
|
-
|
56
|
+
public class ConversationFragment extends DialogFragment {
|
69
|
-
android:layout_width="0dp"
|
70
|
-
|
57
|
+
private static final String ARGS_REQUEST_KEY = "requestKey";
|
71
|
-
|
58
|
+
private static final String ARGS_MESSAGE = "message";
|
72
|
-
|
59
|
+
private static final String ARGS_INDEX = "index";
|
73
|
-
|
60
|
+
private static final String ARGS_POSITIVE_TEXT = "positiveText";
|
74
|
-
app:layout_constraintTop_toBottomOf="@id/activity" />
|
75
61
|
|
76
|
-
<TextView
|
77
|
-
android:id="@+id/arrow"
|
78
|
-
android:text=">"
|
79
|
-
android:textSize="20dp"
|
80
|
-
android:layout_width="wrap_content"
|
81
|
-
android:layout_height="wrap_content"
|
82
|
-
app:layout_constraintBottom_toBottomOf="@id/fragment1"
|
83
|
-
app:layout_constraintLeft_toRightOf="@id/activity"
|
84
|
-
app:layout_constraintRight_toLeftOf="@id/fragment2"
|
85
|
-
|
62
|
+
public static final String RESULT_INDEX = "index";
|
86
63
|
|
87
|
-
<LinearLayout
|
88
|
-
android:id="@+id/fragment2"
|
89
|
-
android:layout_width="0dp"
|
90
|
-
android:layout_height="wrap_content"
|
91
|
-
android:orientation="vertical"
|
92
|
-
|
64
|
+
static ConversationFragment createInstance(String requestKey, String message, int index, String positiveText) {
|
93
|
-
app:layout_constraintRight_toRightOf="parent"
|
94
|
-
app:layout_constraintTop_toTopOf="parent" />
|
95
|
-
|
96
|
-
|
65
|
+
ConversationFragment fragment = new ConversationFragment();
|
97
|
-
```
|
98
|
-
SampleViewModel.java
|
99
|
-
```java
|
100
|
-
|
66
|
+
Bundle args = new Bundle();
|
101
|
-
|
102
|
-
|
67
|
+
args.putString(ARGS_REQUEST_KEY, requestKey);
|
103
|
-
p
|
68
|
+
args.putString(ARGS_MESSAGE, message);
|
104
|
-
|
105
|
-
|
69
|
+
args.putInt(ARGS_INDEX, index);
|
70
|
+
args.putString(ARGS_POSITIVE_TEXT, positiveText);
|
71
|
+
fragment.setArguments(args);
|
106
|
-
return
|
72
|
+
return fragment;
|
107
73
|
}
|
108
74
|
|
75
|
+
@NonNull
|
76
|
+
@Override
|
109
|
-
|
77
|
+
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
|
110
|
-
|
78
|
+
Bundle args = getArguments();
|
79
|
+
String message = args.getString(ARGS_MESSAGE);
|
80
|
+
String positiveText = args.getString(ARGS_POSITIVE_TEXT);
|
81
|
+
return new AlertDialog.Builder(getContext())
|
82
|
+
.setMessage(message)
|
83
|
+
.setPositiveButton(positiveText, (dialog, which) -> {
|
84
|
+
String requestKey = args.getString(ARGS_REQUEST_KEY);
|
85
|
+
int index = args.getInt(ARGS_INDEX);
|
86
|
+
Bundle result = new Bundle();
|
87
|
+
result.putInt(RESULT_INDEX, index);
|
88
|
+
getParentFragmentManager().setFragmentResult(requestKey, result);
|
89
|
+
})
|
90
|
+
.create();
|
111
91
|
}
|
112
92
|
}
|
113
93
|
```
|
114
|
-
AFragment.java
|
115
|
-
```java
|
116
|
-
import android.os.Bundle;
|
117
|
-
import android.view.View;
|
118
|
-
import android.widget.EditText;
|
119
|
-
|
120
|
-
import androidx.annotation.*;
|
121
|
-
import androidx.fragment.app.Fragment;
|
122
|
-
import androidx.lifecycle.ViewModelProvider;
|
123
|
-
|
124
|
-
public class AFragment extends Fragment {
|
125
|
-
AFragment() {
|
126
|
-
super(R.layout.fragment_a);
|
127
|
-
}
|
128
|
-
|
129
|
-
@Override
|
130
|
-
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
131
|
-
super.onViewCreated(view, savedInstanceState);
|
132
|
-
|
133
|
-
SampleViewModel viewModel = new ViewModelProvider(requireActivity()).get(SampleViewModel.class);
|
134
|
-
|
135
|
-
EditText editText = view.findViewById(R.id.editText);
|
136
|
-
view.findViewById(R.id.setButton).setOnClickListener(v -> viewModel.setText(editText.getText().toString()));
|
137
|
-
}
|
138
|
-
}
|
139
|
-
```
|
140
|
-
res/layout/fragment_a.xml
|
141
|
-
```xml
|
142
|
-
<?xml version="1.0" encoding="utf-8"?>
|
143
|
-
<LinearLayout
|
144
|
-
xmlns:android="http://schemas.android.com/apk/res/android"
|
145
|
-
xmlns:tools="http://schemas.android.com/tools"
|
146
|
-
android:layout_width="match_parent"
|
147
|
-
android:layout_height="match_parent"
|
148
|
-
android:orientation="vertical"
|
149
|
-
android:background="#ffffC0ff"
|
150
|
-
tools:context=".AFragment">
|
151
|
-
|
152
|
-
<TextView
|
153
|
-
android:layout_width="match_parent"
|
154
|
-
android:layout_height="wrap_content"
|
155
|
-
android:text="A FRAGMENT"
|
156
|
-
android:textSize="20dp" />
|
157
|
-
<EditText
|
158
|
-
android:id="@+id/editText"
|
159
|
-
android:layout_width="match_parent"
|
160
|
-
android:layout_height="wrap_content"
|
161
|
-
android:textSize="15dp"/>
|
162
|
-
<Button
|
163
|
-
android:id="@+id/setButton"
|
164
|
-
android:text="SET"
|
165
|
-
android:layout_width="wrap_content"
|
166
|
-
android:layout_height="wrap_content" />
|
167
|
-
|
168
|
-
</LinearLayout>
|
169
|
-
```
|
170
|
-
BFragment.java
|
171
|
-
```java
|
172
|
-
import android.os.Bundle;
|
173
|
-
import android.view.View;
|
174
|
-
import android.widget.TextView;
|
175
|
-
|
176
|
-
import androidx.annotation.*;
|
177
|
-
import androidx.fragment.app.Fragment;
|
178
|
-
import androidx.lifecycle.ViewModelProvider;
|
179
|
-
|
180
|
-
public class BFragment extends Fragment {
|
181
|
-
BFragment() {
|
182
|
-
super(R.layout.fragment_b);
|
183
|
-
}
|
184
|
-
|
185
|
-
@Override
|
186
|
-
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
187
|
-
super.onViewCreated(view, savedInstanceState);
|
188
|
-
|
189
|
-
SampleViewModel viewModel = new ViewModelProvider(requireActivity()).get(SampleViewModel.class);
|
190
|
-
|
191
|
-
TextView textView = view.findViewById(R.id.textView);
|
192
|
-
viewModel.getText().observe(getViewLifecycleOwner(), text -> textView.setText(text));
|
193
|
-
}
|
194
|
-
}
|
195
|
-
```
|
196
|
-
res/layout/fragment_b.xml
|
197
|
-
```xml
|
198
|
-
<?xml version="1.0" encoding="utf-8"?>
|
199
|
-
<LinearLayout
|
200
|
-
xmlns:android="http://schemas.android.com/apk/res/android"
|
201
|
-
xmlns:tools="http://schemas.android.com/tools"
|
202
|
-
android:layout_width="match_parent"
|
203
|
-
android:layout_height="match_parent"
|
204
|
-
android:orientation="vertical"
|
205
|
-
android:background="#ffffff80"
|
206
|
-
tools:context=".BFragment">
|
207
|
-
|
208
|
-
<TextView
|
209
|
-
android:layout_width="match_parent"
|
210
|
-
android:layout_height="wrap_content"
|
211
|
-
android:text="B FRAGMENT"
|
212
|
-
android:textSize="20dp"/>
|
213
|
-
<TextView
|
214
|
-
android:id="@+id/textView"
|
215
|
-
android:layout_width="match_parent"
|
216
|
-
android:layout_height="wrap_content"
|
217
|
-
android:textSize="20dp"/>
|
218
|
-
|
219
|
-
</LinearLayout>
|
220
|
-
```
|
221
|
-
|
222
|
-
> MainActivity mainActivity = (MainActivity) getActivity();
|
223
|
-
> mainActivity.onReturnValue(0,0,flag_count,0,0);
|
224
|
-
|
225
|
-
これはフラグメントからアクティビティなら出来ますが、アクティビティからアクティビティへは出来ないのではないでしょうか。
|
1
コード追加
test
CHANGED
@@ -1,4 +1,223 @@
|
|
1
1
|
表示中のフラグメントに対し別のフラグメントやアクティビティから値を渡すのであれば、 ViewModel を経由するのが妥当に思います。
|
2
|
+
|
3
|
+
MainActivity.java
|
4
|
+
```java
|
5
|
+
import android.os.Bundle;
|
6
|
+
import android.widget.EditText;
|
7
|
+
|
8
|
+
import androidx.appcompat.app.AppCompatActivity;
|
9
|
+
import androidx.lifecycle.ViewModelProvider;
|
10
|
+
|
11
|
+
public class MainActivity extends AppCompatActivity {
|
12
|
+
|
13
|
+
@Override
|
14
|
+
protected void onCreate(Bundle savedInstanceState) {
|
15
|
+
super.onCreate(savedInstanceState);
|
16
|
+
setContentView(R.layout.activity_main);
|
17
|
+
|
18
|
+
SampleViewModel viewModel = new ViewModelProvider(this).get(SampleViewModel.class);
|
19
|
+
|
20
|
+
EditText editText = findViewById(R.id.editText);
|
21
|
+
findViewById(R.id.setButton).setOnClickListener(v -> viewModel.setText(editText.getText().toString()));
|
22
|
+
|
23
|
+
getSupportFragmentManager().beginTransaction()
|
24
|
+
.replace(R.id.fragment1, new AFragment())
|
25
|
+
.replace(R.id.fragment2, new BFragment())
|
26
|
+
.commit();
|
27
|
+
}
|
28
|
+
}
|
29
|
+
```
|
30
|
+
res/layout/activity_main.xml
|
31
|
+
```xml
|
32
|
+
<?xml version="1.0" encoding="utf-8"?>
|
33
|
+
<androidx.constraintlayout.widget.ConstraintLayout
|
34
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
35
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
36
|
+
xmlns:tools="http://schemas.android.com/tools"
|
37
|
+
android:layout_width="match_parent"
|
38
|
+
android:layout_height="match_parent"
|
39
|
+
tools:context=".MainActivity">
|
40
|
+
|
41
|
+
<LinearLayout
|
42
|
+
android:id="@+id/activity"
|
43
|
+
android:layout_width="0dp"
|
44
|
+
android:layout_height="wrap_content"
|
45
|
+
android:orientation="vertical"
|
46
|
+
android:background="#ff80ffff"
|
47
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
48
|
+
app:layout_constraintRight_toLeftOf="@id/arrow"
|
49
|
+
app:layout_constraintTop_toTopOf="parent">
|
50
|
+
<TextView
|
51
|
+
android:layout_width="match_parent"
|
52
|
+
android:layout_height="wrap_content"
|
53
|
+
android:text="ACTIVITY"
|
54
|
+
android:textSize="20dp" />
|
55
|
+
<EditText
|
56
|
+
android:id="@+id/editText"
|
57
|
+
android:layout_width="match_parent"
|
58
|
+
android:layout_height="wrap_content"
|
59
|
+
android:textSize="15dp"/>
|
60
|
+
<Button
|
61
|
+
android:id="@+id/setButton"
|
62
|
+
android:text="SET"
|
63
|
+
android:layout_width="wrap_content"
|
64
|
+
android:layout_height="wrap_content" />
|
65
|
+
</LinearLayout>
|
66
|
+
|
67
|
+
<LinearLayout
|
68
|
+
android:id="@+id/fragment1"
|
69
|
+
android:layout_width="0dp"
|
70
|
+
android:layout_height="wrap_content"
|
71
|
+
android:orientation="vertical"
|
72
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
73
|
+
app:layout_constraintRight_toRightOf="@id/activity"
|
74
|
+
app:layout_constraintTop_toBottomOf="@id/activity" />
|
75
|
+
|
76
|
+
<TextView
|
77
|
+
android:id="@+id/arrow"
|
78
|
+
android:text=">"
|
79
|
+
android:textSize="20dp"
|
80
|
+
android:layout_width="wrap_content"
|
81
|
+
android:layout_height="wrap_content"
|
82
|
+
app:layout_constraintBottom_toBottomOf="@id/fragment1"
|
83
|
+
app:layout_constraintLeft_toRightOf="@id/activity"
|
84
|
+
app:layout_constraintRight_toLeftOf="@id/fragment2"
|
85
|
+
app:layout_constraintTop_toTopOf="parent" />
|
86
|
+
|
87
|
+
<LinearLayout
|
88
|
+
android:id="@+id/fragment2"
|
89
|
+
android:layout_width="0dp"
|
90
|
+
android:layout_height="wrap_content"
|
91
|
+
android:orientation="vertical"
|
92
|
+
app:layout_constraintLeft_toRightOf="@id/arrow"
|
93
|
+
app:layout_constraintRight_toRightOf="parent"
|
94
|
+
app:layout_constraintTop_toTopOf="parent" />
|
95
|
+
|
96
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
97
|
+
```
|
98
|
+
SampleViewModel.java
|
99
|
+
```java
|
100
|
+
import androidx.lifecycle.*;
|
101
|
+
|
102
|
+
public class SampleViewModel extends ViewModel {
|
103
|
+
private MutableLiveData<String> textLiveData = new MutableLiveData<>("init");
|
104
|
+
|
105
|
+
LiveData<String> getText() {
|
106
|
+
return textLiveData;
|
107
|
+
}
|
108
|
+
|
109
|
+
void setText(String text) {
|
110
|
+
textLiveData.setValue(text);
|
111
|
+
}
|
112
|
+
}
|
113
|
+
```
|
114
|
+
AFragment.java
|
115
|
+
```java
|
116
|
+
import android.os.Bundle;
|
117
|
+
import android.view.View;
|
118
|
+
import android.widget.EditText;
|
119
|
+
|
120
|
+
import androidx.annotation.*;
|
121
|
+
import androidx.fragment.app.Fragment;
|
122
|
+
import androidx.lifecycle.ViewModelProvider;
|
123
|
+
|
124
|
+
public class AFragment extends Fragment {
|
125
|
+
AFragment() {
|
126
|
+
super(R.layout.fragment_a);
|
127
|
+
}
|
128
|
+
|
129
|
+
@Override
|
130
|
+
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
131
|
+
super.onViewCreated(view, savedInstanceState);
|
132
|
+
|
133
|
+
SampleViewModel viewModel = new ViewModelProvider(requireActivity()).get(SampleViewModel.class);
|
134
|
+
|
135
|
+
EditText editText = view.findViewById(R.id.editText);
|
136
|
+
view.findViewById(R.id.setButton).setOnClickListener(v -> viewModel.setText(editText.getText().toString()));
|
137
|
+
}
|
138
|
+
}
|
139
|
+
```
|
140
|
+
res/layout/fragment_a.xml
|
141
|
+
```xml
|
142
|
+
<?xml version="1.0" encoding="utf-8"?>
|
143
|
+
<LinearLayout
|
144
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
145
|
+
xmlns:tools="http://schemas.android.com/tools"
|
146
|
+
android:layout_width="match_parent"
|
147
|
+
android:layout_height="match_parent"
|
148
|
+
android:orientation="vertical"
|
149
|
+
android:background="#ffffC0ff"
|
150
|
+
tools:context=".AFragment">
|
151
|
+
|
152
|
+
<TextView
|
153
|
+
android:layout_width="match_parent"
|
154
|
+
android:layout_height="wrap_content"
|
155
|
+
android:text="A FRAGMENT"
|
156
|
+
android:textSize="20dp" />
|
157
|
+
<EditText
|
158
|
+
android:id="@+id/editText"
|
159
|
+
android:layout_width="match_parent"
|
160
|
+
android:layout_height="wrap_content"
|
161
|
+
android:textSize="15dp"/>
|
162
|
+
<Button
|
163
|
+
android:id="@+id/setButton"
|
164
|
+
android:text="SET"
|
165
|
+
android:layout_width="wrap_content"
|
166
|
+
android:layout_height="wrap_content" />
|
167
|
+
|
168
|
+
</LinearLayout>
|
169
|
+
```
|
170
|
+
BFragment.java
|
171
|
+
```java
|
172
|
+
import android.os.Bundle;
|
173
|
+
import android.view.View;
|
174
|
+
import android.widget.TextView;
|
175
|
+
|
176
|
+
import androidx.annotation.*;
|
177
|
+
import androidx.fragment.app.Fragment;
|
178
|
+
import androidx.lifecycle.ViewModelProvider;
|
179
|
+
|
180
|
+
public class BFragment extends Fragment {
|
181
|
+
BFragment() {
|
182
|
+
super(R.layout.fragment_b);
|
183
|
+
}
|
184
|
+
|
185
|
+
@Override
|
186
|
+
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
187
|
+
super.onViewCreated(view, savedInstanceState);
|
188
|
+
|
189
|
+
SampleViewModel viewModel = new ViewModelProvider(requireActivity()).get(SampleViewModel.class);
|
190
|
+
|
191
|
+
TextView textView = view.findViewById(R.id.textView);
|
192
|
+
viewModel.getText().observe(getViewLifecycleOwner(), text -> textView.setText(text));
|
193
|
+
}
|
194
|
+
}
|
195
|
+
```
|
196
|
+
res/layout/fragment_b.xml
|
197
|
+
```xml
|
198
|
+
<?xml version="1.0" encoding="utf-8"?>
|
199
|
+
<LinearLayout
|
200
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
201
|
+
xmlns:tools="http://schemas.android.com/tools"
|
202
|
+
android:layout_width="match_parent"
|
203
|
+
android:layout_height="match_parent"
|
204
|
+
android:orientation="vertical"
|
205
|
+
android:background="#ffffff80"
|
206
|
+
tools:context=".BFragment">
|
207
|
+
|
208
|
+
<TextView
|
209
|
+
android:layout_width="match_parent"
|
210
|
+
android:layout_height="wrap_content"
|
211
|
+
android:text="B FRAGMENT"
|
212
|
+
android:textSize="20dp"/>
|
213
|
+
<TextView
|
214
|
+
android:id="@+id/textView"
|
215
|
+
android:layout_width="match_parent"
|
216
|
+
android:layout_height="wrap_content"
|
217
|
+
android:textSize="20dp"/>
|
218
|
+
|
219
|
+
</LinearLayout>
|
220
|
+
```
|
2
221
|
|
3
222
|
> MainActivity mainActivity = (MainActivity) getActivity();
|
4
223
|
> mainActivity.onReturnValue(0,0,flag_count,0,0);
|