我对这个问题感到茫然,我已经阅读了几乎每一个相关的问题,但我对我的问题可能是什么感到难过,Activity运行良好并且在finish()
方法关闭了,但是在我的MainActivity类中没有做任何事情。我确保我的清单不会调用noHistory
和singleton
活动。我放了几个吐司和一个文本集,但没有一个被调用。这是我的代码实现:
在MainActivity上:
Intent placeShips = new Intent(this, SetShips.class); //MainActivity.this works too
startActivityForResult(placeShips, RESULT_OK);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_OK && resultCode == RESULT_OK) {
Toast.makeText(MainActivity.this, "worked", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "worked", Toast.LENGTH_SHORT).show();
}
newGame.setText("PLS "); //these aren't being called either
toast("PLS");
}
在名为Activity上: (请注意按钮工作正常)
setShips.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
setResult(RESULT_OK);
finish(); //return to main activity, giving intent with stored player information
}
});
补充说明; toast在setShips
按钮监听器上正确显示,但是,没有任何内容传回onActivityResult
方法,正如所解释的那样,甚至没有toasts显示。我错过了别的什么吗?
答案 0 :(得分:2)
您似乎无法正确设置请求代码。你需要这样做:
static final int YOUR_CODE = 1;
Intent placeShips = new Intent(this, SetShips.class); //MainActivity.this works too
startActivityForResult(placeShips, YOUR_CODE);
然后你可以这样做:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == YOUR_CODE && resultCode == RESULT_OK) {
Toast.makeText(MainActivity.this, "worked", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "worked", Toast.LENGTH_SHORT).show();
}
newGame.setText("PLS "); //these aren't being called either
toast("PLS");
}
答案 1 :(得分:0)
您的结果代码应该是这样的
if (requestCode == RESULT_OK && resultCode == Activity.RESULT_OK) {
Toast.makeText(MainActivity.this, "worked", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "worked", Toast.LENGTH_SHORT).show();
}