从它自己开始的活动会跳过OnCreate

时间:2016-02-17 14:10:17

标签: android oncreate

我有这个名为MainPutShipActivity的活动,我想再次启动它,所以它会做同样的事情,但它甚至不会进入OnCreate方法。

这是MainPutShipActivity

public class MainPutShipActivity extends Activity implements OnClickListener{

private static final int MAX = 10;
private String name1,name2;
private Player plr = new Player();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_put_ship);

    Intent intent = getIntent();

    plr = (Player) intent.getSerializableExtra("player");
    name1 = plr.getName1();
    name2 = plr.getName2();

}

public void finished() {
    Intent in;
        }
    if (plr.isTreated() == false) {
        plr.setArr1(arr);
        plr.setShip1(ships);
        this.finish();
        in = new Intent(this,MainPutShipActivity.class);
        in.putExtra("player", this.plr);
    }
    else {
        plr.setArr2(arr);
        plr.setShip2(ships);
        in = new Intent(this, MainGameActivity.class);
        in.putExtra("player", this.plr);
    }
    plr.setTreated(true);
    this.finish();
    startActivity(in);
}

当我第一次进入finished()程序时,它假设再次启动MainPutShipActivity但是当它开始活动时,它会跳过onCreate并直接转到finished()方法原因。

我会很高兴得到任何帮助。

1 个答案:

答案 0 :(得分:2)

注意意图中传递的密钥

而不是:

plr = (Player) i.getSerializableExtra("plr");

plr = (Player) i.getSerializableExtra("player");

因为您正在设置

in.putExtra("player", this.plr);