导航离开Activity时再次调用onCreate

时间:2014-03-04 00:48:11

标签: java android android-activity android-lifecycle oncreate

代码效果很好。但是,我从Activity Lifecycle的角度看到了一些奇怪的东西。 onCreate代码基本上将适配器设置为相关的GridView,并从数据库中提取项目并填充所述GridView。用户可以点击每个项目,将项目的“playerId”推送到HubActivity。

以下是我的 onCreate 代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Const.err("##> Running PlayerSearchActivity.onCreate");
    setContentView(R.layout.activity_playersearch);
    this.mPlayerSearchBtn=(Button) findViewById(R.id.playerSearchButton);
    this.mPlayerSearchText=(TextView) findViewById(R.id.playerSearchText);
    this.mPlayerSearchGridView=(GridView) findViewById(R.id.psearch_playersearchgrid);
    this.mSavedPlayersGridView=(GridView) findViewById(R.id.psearch_savedplayersgrid);
    mPlayerSearchBtn.setOnClickListener(this);

    this.savedPlayersAdapter = new PlayerGridAdapter(this);
    mSavedPlayersGridView.setAdapter(savedPlayersAdapter);
    mSavedPlayersGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
            Intent intent = new Intent(getApplicationContext(), HubActivity.class);
            intent.putExtra(Keys._PLAYERID,savedPlayersAdapter.getItemPlayerId(position));
            startActivity(intent);
        }
    });

    Const.err("<## Completed PlayerSearchActivity.onCreate");

}

我很好奇每个方法是如何调用的,所以我在函数(Const.err)周围应用了一些日志记录。结果很奇怪:

03-04 00:29:50.942:  ##> Running PlayerSearchActivity.onCreate
03-04 00:29:50.982:  <## Completed PlayerSearchActivity.onCreate
03-04 00:29:50.982:  ##> Running PlayerSearchActivity.onResume
03-04 00:29:51.058:  <## Completed PlayerSearchActivity.onResume

---Clicked item---

03-04 00:29:54.718:  ##> Running PlayerSearchActivity.onCreate
03-04 00:29:54.730:  <## Completed HubActivity.onCreate
..

首先,为什么在点击一个项目后调用onCreate?此外,我希望看到“已完成的PlayerSearchActivity.onCreate”消息,但这绝不会发生。

1 个答案:

答案 0 :(得分:0)

我想这是一个复制粘贴错误,检查你的HubActivity.onCreate(),如果你没有意外粘贴

Const.err("##> Running PlayerSearchActivity.onCreate");

而不是

Const.err("##> Running HubActivity.onCreate");