Android getIntent on new activity is NULL when calling activity is destroyed

独自空忆成欢 提交于 2019-12-31 03:58:08

问题


Do you guys have a workaround for the following problem?

In the onDestroy of a registration activity (when the user presses the back button) I call a new activity so the user can put in some final production data of that day and then the report is being e-mailed. The problem is that on the just started activity the call to getIntent returns null and I have to get the data from there.

public void onDestroy(){
  //unregister listeners, cancel timers etc.
  logOff();
  super.onDestroy();
} 

protected void logOff(){
  // collect data etc.
  // open new activity that asks for final production numbers
  Intent intent = new Intent(getBaseContext(), AksProductionNumbers.class);
  intent.putExtra("TimeSheetList", timeSheetList);
  startActivity(intent);
}

=============================================================================== Constructor called activity:

public AksProductionNumbers(){
    Intent intent = getIntent(); // <-- returns null
    Bundle extras = intent.getExtras();
}

The function getIntent returns null. I think it is because the calling activity is already dead but I think it is weird because I clearly ask the framework to start the new activity before closing the current. Does anyone knows a workaround for this problem? I chose for this solution so I didn't have to override the 'back' button. Another solution is to block the calling thread until it gets the unblock signal from the starting app but I think that is ugly as well. A third data manager class could also solve the data source issue. Thanks for ideas and advice!


回答1:


Actually, I think I know what your problem is. I believe you are making this call:

 public AksProductionNumbers()

in your activity constructor (as you state in your question) when you should be making it in the activity's onCreate method. getIntent will return null in the constructor.




回答2:


Try to put this

 Intent intent = getIntent(); // <-- returns null
    Bundle extras = intent.getExtras();

on "onCreate()" method of new activity



来源:https://stackoverflow.com/questions/25351234/android-getintent-on-new-activity-is-null-when-calling-activity-is-destroyed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!