回答編集履歴
2
コード追加
answer
CHANGED
@@ -3,4 +3,196 @@
|
|
3
3
|
startActivity は「戻る」処理ではありません。あくまで指定したアクティビティを起動するメソッドです。
|
4
4
|
startActivity を実行する度に新たなインスタンスが作られ、古いアクティビティは残ったままメモリを圧迫し続けることになるのではないでしょうか。
|
5
5
|
|
6
|
-
アクティビティを実行し、その終了を待って処理を続ける方法があります(以前は startActivityForResult でしたが Deprecated になりまして、 androidx で registerForActivityResult を使うようになったようです)ので、そちらを用いれば、指定したアクティビティの終了後に必要なメソッドを実行することが出来るのでは…と思います。
|
6
|
+
アクティビティを実行し、その終了を待って処理を続ける方法があります(以前は startActivityForResult でしたが Deprecated になりまして、 androidx で registerForActivityResult を使うようになったようです)ので、そちらを用いれば、指定したアクティビティの終了後に必要なメソッドを実行することが出来るのでは…と思います。
|
7
|
+
|
8
|
+
----
|
9
|
+
|
10
|
+
MainActivity.java
|
11
|
+
```java
|
12
|
+
package com.teratail.q361159;
|
13
|
+
|
14
|
+
import android.app.Activity;
|
15
|
+
import android.content.Intent;
|
16
|
+
import android.os.Bundle;
|
17
|
+
import android.util.Log;
|
18
|
+
import android.widget.Button;
|
19
|
+
|
20
|
+
import androidx.activity.result.ActivityResultLauncher;
|
21
|
+
import androidx.activity.result.contract.ActivityResultContracts;
|
22
|
+
import androidx.appcompat.app.AppCompatActivity;
|
23
|
+
|
24
|
+
public class MainActivity extends AppCompatActivity {
|
25
|
+
private static final String TAG = "MainActivity";
|
26
|
+
|
27
|
+
private int score = 123;
|
28
|
+
|
29
|
+
private ActivityResultLauncher<Intent> startForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result->{
|
30
|
+
switch(result.getResultCode()) {
|
31
|
+
case Activity.RESULT_OK:
|
32
|
+
//Activity から戻って必要なメソッドを呼ぶ
|
33
|
+
//Intent intent = result.getData();
|
34
|
+
Log.d(TAG, "ActivityResultLauncher result="+result);
|
35
|
+
break;
|
36
|
+
case ResultActivity.RESULT_OWARU:
|
37
|
+
Log.d(TAG, "ActivityResultLauncher OWARU");
|
38
|
+
finish();
|
39
|
+
break;
|
40
|
+
}
|
41
|
+
});
|
42
|
+
|
43
|
+
@Override
|
44
|
+
protected void onCreate(Bundle savedInstanceState) {
|
45
|
+
super.onCreate(savedInstanceState);
|
46
|
+
setContentView(R.layout.activity_main);
|
47
|
+
|
48
|
+
Button button = findViewById(R.id.button);
|
49
|
+
button.setOnClickListener(v -> {
|
50
|
+
//保存画面へ
|
51
|
+
startForResult.launch(new Intent(this, ResultActivity.class)
|
52
|
+
.putExtra(ResultActivity.PARAM_SCORE, score));
|
53
|
+
});
|
54
|
+
}
|
55
|
+
}
|
56
|
+
```
|
57
|
+
レイアウト: activity_main.xml
|
58
|
+
```xml
|
59
|
+
<?xml version="1.0" encoding="utf-8"?>
|
60
|
+
<androidx.constraintlayout.widget.ConstraintLayout
|
61
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
62
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
63
|
+
xmlns:tools="http://schemas.android.com/tools"
|
64
|
+
android:layout_width="match_parent"
|
65
|
+
android:layout_height="match_parent"
|
66
|
+
tools:context=".MainActivity">
|
67
|
+
|
68
|
+
<Button
|
69
|
+
android:id="@+id/button"
|
70
|
+
android:layout_width="wrap_content"
|
71
|
+
android:layout_height="wrap_content"
|
72
|
+
android:text="Hello World!"
|
73
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
74
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
75
|
+
app:layout_constraintRight_toRightOf="parent"
|
76
|
+
app:layout_constraintTop_toTopOf="parent" />
|
77
|
+
|
78
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
79
|
+
```
|
80
|
+
ResultActivity.java
|
81
|
+
```java
|
82
|
+
package com.teratail.q361159;
|
83
|
+
|
84
|
+
import android.app.Activity;
|
85
|
+
import android.content.SharedPreferences;
|
86
|
+
import android.os.Bundle;
|
87
|
+
import android.widget.*;
|
88
|
+
|
89
|
+
import androidx.appcompat.app.AppCompatActivity;
|
90
|
+
|
91
|
+
public class ResultActivity extends AppCompatActivity {
|
92
|
+
public static final String PARAM_SCORE = "SCORE";
|
93
|
+
public static final int RESULT_OWARU = Activity.RESULT_FIRST_USER;
|
94
|
+
|
95
|
+
public static final String SHARED_NAME = "GAME_DATA";
|
96
|
+
public static final String KEY_HIGHSCORE = "HIGH_SCORE";
|
97
|
+
|
98
|
+
@Override
|
99
|
+
protected void onCreate(Bundle savedInstanceState) {
|
100
|
+
super.onCreate(savedInstanceState);
|
101
|
+
setContentView(R.layout.activity_result);
|
102
|
+
|
103
|
+
TextView scoreLabel = findViewById(R.id.scoreLabel);
|
104
|
+
TextView highScoreLabel = findViewById(R.id.highScoreLabel);
|
105
|
+
Button bt_mouitido = findViewById(R.id.bt_mouitido);
|
106
|
+
Button bt_hozon = findViewById(R.id.bt_hozon);
|
107
|
+
Button bt_owaru = findViewById(R.id.bt_owaru);
|
108
|
+
|
109
|
+
int score = getIntent().getIntExtra(PARAM_SCORE, 0);
|
110
|
+
scoreLabel.setText(score + "");
|
111
|
+
|
112
|
+
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NAME, MODE_PRIVATE);
|
113
|
+
int highScore = sharedPreferences.getInt(KEY_HIGHSCORE, 0);
|
114
|
+
|
115
|
+
highScoreLabel.setText("High Score : " + Math.max(highScore, score));
|
116
|
+
|
117
|
+
bt_mouitido.setOnClickListener(v->{
|
118
|
+
setResult(Activity.RESULT_OK, null);
|
119
|
+
finish();
|
120
|
+
});
|
121
|
+
|
122
|
+
bt_hozon.setEnabled(highScore < score);
|
123
|
+
|
124
|
+
bt_hozon.setOnClickListener(v->{
|
125
|
+
SharedPreferences.Editor editor = sharedPreferences.edit();
|
126
|
+
editor.putInt(KEY_HIGHSCORE, score);
|
127
|
+
editor.apply();
|
128
|
+
bt_hozon.setEnabled(false);
|
129
|
+
});
|
130
|
+
|
131
|
+
bt_owaru.setOnClickListener(v->{
|
132
|
+
setResult(RESULT_OWARU, null);
|
133
|
+
finish();
|
134
|
+
});
|
135
|
+
}
|
136
|
+
}
|
137
|
+
```
|
138
|
+
レイアウト: activity_result.xml
|
139
|
+
```xml
|
140
|
+
<?xml version="1.0" encoding="utf-8"?>
|
141
|
+
<androidx.constraintlayout.widget.ConstraintLayout
|
142
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
143
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
144
|
+
xmlns:tools="http://schemas.android.com/tools"
|
145
|
+
android:layout_width="match_parent"
|
146
|
+
android:layout_height="match_parent"
|
147
|
+
tools:context=".ResultActivity">
|
148
|
+
|
149
|
+
<TextView
|
150
|
+
android:id="@+id/scoreLabel"
|
151
|
+
android:layout_width="wrap_content"
|
152
|
+
android:layout_height="wrap_content"
|
153
|
+
android:text="0"
|
154
|
+
app:layout_constraintBottom_toTopOf="@id/highScoreLabel"
|
155
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
156
|
+
app:layout_constraintRight_toRightOf="parent"
|
157
|
+
app:layout_constraintTop_toTopOf="parent" />
|
158
|
+
|
159
|
+
<TextView
|
160
|
+
android:id="@+id/highScoreLabel"
|
161
|
+
android:layout_width="wrap_content"
|
162
|
+
android:layout_height="wrap_content"
|
163
|
+
android:text="0"
|
164
|
+
app:layout_constraintBottom_toTopOf="@id/bt_mouitido"
|
165
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
166
|
+
app:layout_constraintRight_toRightOf="parent"
|
167
|
+
app:layout_constraintTop_toBottomOf="@id/scoreLabel" />
|
168
|
+
|
169
|
+
<Button
|
170
|
+
android:id="@+id/bt_mouitido"
|
171
|
+
android:layout_width="wrap_content"
|
172
|
+
android:layout_height="wrap_content"
|
173
|
+
android:text="もう一度"
|
174
|
+
app:layout_constraintBottom_toTopOf="@id/bt_hozon"
|
175
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
176
|
+
app:layout_constraintRight_toRightOf="parent"
|
177
|
+
app:layout_constraintTop_toBottomOf="@id/highScoreLabel" />
|
178
|
+
<Button
|
179
|
+
android:id="@+id/bt_hozon"
|
180
|
+
android:layout_width="wrap_content"
|
181
|
+
android:layout_height="wrap_content"
|
182
|
+
android:text="保存"
|
183
|
+
app:layout_constraintBottom_toTopOf="@id/bt_owaru"
|
184
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
185
|
+
app:layout_constraintRight_toRightOf="parent"
|
186
|
+
app:layout_constraintTop_toBottomOf="@id/bt_mouitido" />
|
187
|
+
<Button
|
188
|
+
android:id="@+id/bt_owaru"
|
189
|
+
android:layout_width="wrap_content"
|
190
|
+
android:layout_height="wrap_content"
|
191
|
+
android:text="終わる"
|
192
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
193
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
194
|
+
app:layout_constraintRight_toRightOf="parent"
|
195
|
+
app:layout_constraintTop_toBottomOf="@id/bt_hozon" />
|
196
|
+
|
197
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
198
|
+
```
|
1
registerForActivityResult はクラスで無くメソッド
answer
CHANGED
@@ -3,4 +3,4 @@
|
|
3
3
|
startActivity は「戻る」処理ではありません。あくまで指定したアクティビティを起動するメソッドです。
|
4
4
|
startActivity を実行する度に新たなインスタンスが作られ、古いアクティビティは残ったままメモリを圧迫し続けることになるのではないでしょうか。
|
5
5
|
|
6
|
-
アクティビティを実行し、その終了を待って処理を続ける方法があります(以前は startActivityForResult でしたが Deprecated になりまして、 androidx で registerForActivityResult
|
6
|
+
アクティビティを実行し、その終了を待って処理を続ける方法があります(以前は startActivityForResult でしたが Deprecated になりまして、 androidx で registerForActivityResult を使うようになったようです)ので、そちらを用いれば、指定したアクティビティの終了後に必要なメソッドを実行することが出来るのでは…と思います。
|