回答編集履歴
2
コード追加
test
CHANGED
@@ -9,3 +9,387 @@
|
|
9
9
|
|
10
10
|
|
11
11
|
アクティビティを実行し、その終了を待って処理を続ける方法があります(以前は startActivityForResult でしたが Deprecated になりまして、 androidx で registerForActivityResult を使うようになったようです)ので、そちらを用いれば、指定したアクティビティの終了後に必要なメソッドを実行することが出来るのでは…と思います。
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
----
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
MainActivity.java
|
20
|
+
|
21
|
+
```java
|
22
|
+
|
23
|
+
package com.teratail.q361159;
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
import android.app.Activity;
|
28
|
+
|
29
|
+
import android.content.Intent;
|
30
|
+
|
31
|
+
import android.os.Bundle;
|
32
|
+
|
33
|
+
import android.util.Log;
|
34
|
+
|
35
|
+
import android.widget.Button;
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
import androidx.activity.result.ActivityResultLauncher;
|
40
|
+
|
41
|
+
import androidx.activity.result.contract.ActivityResultContracts;
|
42
|
+
|
43
|
+
import androidx.appcompat.app.AppCompatActivity;
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
public class MainActivity extends AppCompatActivity {
|
48
|
+
|
49
|
+
private static final String TAG = "MainActivity";
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
private int score = 123;
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
private ActivityResultLauncher<Intent> startForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result->{
|
58
|
+
|
59
|
+
switch(result.getResultCode()) {
|
60
|
+
|
61
|
+
case Activity.RESULT_OK:
|
62
|
+
|
63
|
+
//Activity から戻って必要なメソッドを呼ぶ
|
64
|
+
|
65
|
+
//Intent intent = result.getData();
|
66
|
+
|
67
|
+
Log.d(TAG, "ActivityResultLauncher result="+result);
|
68
|
+
|
69
|
+
break;
|
70
|
+
|
71
|
+
case ResultActivity.RESULT_OWARU:
|
72
|
+
|
73
|
+
Log.d(TAG, "ActivityResultLauncher OWARU");
|
74
|
+
|
75
|
+
finish();
|
76
|
+
|
77
|
+
break;
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
});
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
@Override
|
86
|
+
|
87
|
+
protected void onCreate(Bundle savedInstanceState) {
|
88
|
+
|
89
|
+
super.onCreate(savedInstanceState);
|
90
|
+
|
91
|
+
setContentView(R.layout.activity_main);
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
Button button = findViewById(R.id.button);
|
96
|
+
|
97
|
+
button.setOnClickListener(v -> {
|
98
|
+
|
99
|
+
//保存画面へ
|
100
|
+
|
101
|
+
startForResult.launch(new Intent(this, ResultActivity.class)
|
102
|
+
|
103
|
+
.putExtra(ResultActivity.PARAM_SCORE, score));
|
104
|
+
|
105
|
+
});
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
```
|
112
|
+
|
113
|
+
レイアウト: activity_main.xml
|
114
|
+
|
115
|
+
```xml
|
116
|
+
|
117
|
+
<?xml version="1.0" encoding="utf-8"?>
|
118
|
+
|
119
|
+
<androidx.constraintlayout.widget.ConstraintLayout
|
120
|
+
|
121
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
122
|
+
|
123
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
124
|
+
|
125
|
+
xmlns:tools="http://schemas.android.com/tools"
|
126
|
+
|
127
|
+
android:layout_width="match_parent"
|
128
|
+
|
129
|
+
android:layout_height="match_parent"
|
130
|
+
|
131
|
+
tools:context=".MainActivity">
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
<Button
|
136
|
+
|
137
|
+
android:id="@+id/button"
|
138
|
+
|
139
|
+
android:layout_width="wrap_content"
|
140
|
+
|
141
|
+
android:layout_height="wrap_content"
|
142
|
+
|
143
|
+
android:text="Hello World!"
|
144
|
+
|
145
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
146
|
+
|
147
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
148
|
+
|
149
|
+
app:layout_constraintRight_toRightOf="parent"
|
150
|
+
|
151
|
+
app:layout_constraintTop_toTopOf="parent" />
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
156
|
+
|
157
|
+
```
|
158
|
+
|
159
|
+
ResultActivity.java
|
160
|
+
|
161
|
+
```java
|
162
|
+
|
163
|
+
package com.teratail.q361159;
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
import android.app.Activity;
|
168
|
+
|
169
|
+
import android.content.SharedPreferences;
|
170
|
+
|
171
|
+
import android.os.Bundle;
|
172
|
+
|
173
|
+
import android.widget.*;
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
import androidx.appcompat.app.AppCompatActivity;
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
public class ResultActivity extends AppCompatActivity {
|
182
|
+
|
183
|
+
public static final String PARAM_SCORE = "SCORE";
|
184
|
+
|
185
|
+
public static final int RESULT_OWARU = Activity.RESULT_FIRST_USER;
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
public static final String SHARED_NAME = "GAME_DATA";
|
190
|
+
|
191
|
+
public static final String KEY_HIGHSCORE = "HIGH_SCORE";
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
@Override
|
196
|
+
|
197
|
+
protected void onCreate(Bundle savedInstanceState) {
|
198
|
+
|
199
|
+
super.onCreate(savedInstanceState);
|
200
|
+
|
201
|
+
setContentView(R.layout.activity_result);
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
TextView scoreLabel = findViewById(R.id.scoreLabel);
|
206
|
+
|
207
|
+
TextView highScoreLabel = findViewById(R.id.highScoreLabel);
|
208
|
+
|
209
|
+
Button bt_mouitido = findViewById(R.id.bt_mouitido);
|
210
|
+
|
211
|
+
Button bt_hozon = findViewById(R.id.bt_hozon);
|
212
|
+
|
213
|
+
Button bt_owaru = findViewById(R.id.bt_owaru);
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
int score = getIntent().getIntExtra(PARAM_SCORE, 0);
|
218
|
+
|
219
|
+
scoreLabel.setText(score + "");
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME, MODE_PRIVATE);
|
224
|
+
|
225
|
+
int highScore = sharedPreferences.getInt(KEY_HIGHSCORE, 0);
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
highScoreLabel.setText("High Score : " + Math.max(highScore, score));
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
bt_mouitido.setOnClickListener(v->{
|
234
|
+
|
235
|
+
setResult(Activity.RESULT_OK, null);
|
236
|
+
|
237
|
+
finish();
|
238
|
+
|
239
|
+
});
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
bt_hozon.setEnabled(highScore < score);
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
bt_hozon.setOnClickListener(v->{
|
248
|
+
|
249
|
+
SharedPreferences.Editor editor = sharedPreferences.edit();
|
250
|
+
|
251
|
+
editor.putInt(KEY_HIGHSCORE, score);
|
252
|
+
|
253
|
+
editor.apply();
|
254
|
+
|
255
|
+
bt_hozon.setEnabled(false);
|
256
|
+
|
257
|
+
});
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
bt_owaru.setOnClickListener(v->{
|
262
|
+
|
263
|
+
setResult(RESULT_OWARU, null);
|
264
|
+
|
265
|
+
finish();
|
266
|
+
|
267
|
+
});
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
}
|
272
|
+
|
273
|
+
```
|
274
|
+
|
275
|
+
レイアウト: activity_result.xml
|
276
|
+
|
277
|
+
```xml
|
278
|
+
|
279
|
+
<?xml version="1.0" encoding="utf-8"?>
|
280
|
+
|
281
|
+
<androidx.constraintlayout.widget.ConstraintLayout
|
282
|
+
|
283
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
284
|
+
|
285
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
286
|
+
|
287
|
+
xmlns:tools="http://schemas.android.com/tools"
|
288
|
+
|
289
|
+
android:layout_width="match_parent"
|
290
|
+
|
291
|
+
android:layout_height="match_parent"
|
292
|
+
|
293
|
+
tools:context=".ResultActivity">
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
<TextView
|
298
|
+
|
299
|
+
android:id="@+id/scoreLabel"
|
300
|
+
|
301
|
+
android:layout_width="wrap_content"
|
302
|
+
|
303
|
+
android:layout_height="wrap_content"
|
304
|
+
|
305
|
+
android:text="0"
|
306
|
+
|
307
|
+
app:layout_constraintBottom_toTopOf="@id/highScoreLabel"
|
308
|
+
|
309
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
310
|
+
|
311
|
+
app:layout_constraintRight_toRightOf="parent"
|
312
|
+
|
313
|
+
app:layout_constraintTop_toTopOf="parent" />
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
<TextView
|
318
|
+
|
319
|
+
android:id="@+id/highScoreLabel"
|
320
|
+
|
321
|
+
android:layout_width="wrap_content"
|
322
|
+
|
323
|
+
android:layout_height="wrap_content"
|
324
|
+
|
325
|
+
android:text="0"
|
326
|
+
|
327
|
+
app:layout_constraintBottom_toTopOf="@id/bt_mouitido"
|
328
|
+
|
329
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
330
|
+
|
331
|
+
app:layout_constraintRight_toRightOf="parent"
|
332
|
+
|
333
|
+
app:layout_constraintTop_toBottomOf="@id/scoreLabel" />
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
<Button
|
338
|
+
|
339
|
+
android:id="@+id/bt_mouitido"
|
340
|
+
|
341
|
+
android:layout_width="wrap_content"
|
342
|
+
|
343
|
+
android:layout_height="wrap_content"
|
344
|
+
|
345
|
+
android:text="もう一度"
|
346
|
+
|
347
|
+
app:layout_constraintBottom_toTopOf="@id/bt_hozon"
|
348
|
+
|
349
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
350
|
+
|
351
|
+
app:layout_constraintRight_toRightOf="parent"
|
352
|
+
|
353
|
+
app:layout_constraintTop_toBottomOf="@id/highScoreLabel" />
|
354
|
+
|
355
|
+
<Button
|
356
|
+
|
357
|
+
android:id="@+id/bt_hozon"
|
358
|
+
|
359
|
+
android:layout_width="wrap_content"
|
360
|
+
|
361
|
+
android:layout_height="wrap_content"
|
362
|
+
|
363
|
+
android:text="保存"
|
364
|
+
|
365
|
+
app:layout_constraintBottom_toTopOf="@id/bt_owaru"
|
366
|
+
|
367
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
368
|
+
|
369
|
+
app:layout_constraintRight_toRightOf="parent"
|
370
|
+
|
371
|
+
app:layout_constraintTop_toBottomOf="@id/bt_mouitido" />
|
372
|
+
|
373
|
+
<Button
|
374
|
+
|
375
|
+
android:id="@+id/bt_owaru"
|
376
|
+
|
377
|
+
android:layout_width="wrap_content"
|
378
|
+
|
379
|
+
android:layout_height="wrap_content"
|
380
|
+
|
381
|
+
android:text="終わる"
|
382
|
+
|
383
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
384
|
+
|
385
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
386
|
+
|
387
|
+
app:layout_constraintRight_toRightOf="parent"
|
388
|
+
|
389
|
+
app:layout_constraintTop_toBottomOf="@id/bt_hozon" />
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
394
|
+
|
395
|
+
```
|
1
registerForActivityResult はクラスで無くメソッド
test
CHANGED
@@ -8,4 +8,4 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
-
アクティビティを実行し、その終了を待って処理を続ける方法があります(以前は startActivityForResult でしたが Deprecated になりまして、 androidx で registerForActivityResult
|
11
|
+
アクティビティを実行し、その終了を待って処理を続ける方法があります(以前は startActivityForResult でしたが Deprecated になりまして、 androidx で registerForActivityResult を使うようになったようです)ので、そちらを用いれば、指定したアクティビティの終了後に必要なメソッドを実行することが出来るのでは…と思います。
|