我有3个类别:MainActivity,HighScoreActivity和GameScreenActivity。我想通过MainActivity转到GameScreenActivity,然后在游戏结束时将分数传递给HighScoreActivity。一切都成功传递到GameScreenActivity,但没有传递到HighScoreActivity中的onActivityResult。
我相信这是因为MainActivity具有startActivityForResult,因此GameScreenActivity的finishQuiz()希望将其数据返回给MainActivity。如何将GameScreenActivity的数据传递给HighScoreActivity?
主要活动()
MainActivity(){
...
private void startQuiz() {
String difficulty = spinnerDifficulty.getSelectedItem().toString();
Intent intent = new Intent(getBaseContext(), GameScreenActivity.class);
intent.putExtra(EXTRA_DIFFICULTY, difficulty);
startActivityForResult(intent, REQUEST_CODE_QUIZ);
}
}
GameScreenActivity()
GameScreenActivity(){
...
private void finishQuiz(){
Intent resultIntent = new Intent(getBaseContext(),
HighScoreActivity.class);
resultIntent.putExtra(EXTRA_SCORE, score);
resultIntent.putExtra(EXTRA_DIFFICULTY, difficulty);
setResult(RESULT_OK, resultIntent);
startActivity(resultIntent);
}
}
HighScoreActivity()
HighScoreActivity(){
...
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_QUIZ){
if (resultCode == RESULT_OK) {
int score = data.getIntExtra(GameScreenActivity.EXTRA_SCORE, 0);
String difficulty = data.getStringExtra(GameScreenActivity.EXTRA_DIFFICULTY);
if (score > highScore10) {
updateHighscore(score, difficulty);
}
}
}
}
}
-------------------------------------------------- -----------
尝试使用Broadcast方法:
GameScreenActivity()与广播方法
GameScreenActivity(){
...
private void finishQuiz(){
Log.d("sender", "Broadcasting message");
Intent intent = new Intent("scoreEvent");
intent.putExtra(EXTRA_DIFFICULTY, difficulty);
intent.putExtra(EXTRA_SCORE, score);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
sendBroadcast(intent);
Intent goToHighScores = new Intent(getBaseContext(), HighScoreActivity.class);
startActivity(goToHighScores);
}
}
具有广播方法的HighScoreActivity()
HighScoreActivity(){
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_high_score);
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("scoreEvent"));
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_QUIZ){
if (resultCode == RESULT_OK) {
int score = data.getIntExtra(GameScreenActivity.EXTRA_SCORE, 0);
String difficulty = data.getStringExtra(GameScreenActivity.EXTRA_DIFFICULTY);
if (score > highScore10) {
updateHighscore(score, difficulty);
}
}
}
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("message");
Log.d("receiver", "Got message: " + message);
}
};
@Override
protected void onDestroy() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
}
最佳答案
考虑到HighScoreActivity
正在运行,
您可以利用LocalBroadcastManager
向HighScoreActivity
注册IntentFilter
中的本地broadcastReceiver。另外,不要忘记在onDestroy()
中注销
现在,用setResult
代替sendBroadcast()
希望这可以帮助。编码愉快!