其他分享
首页 > 其他分享> > 调用活动被销毁时,新活动上的Android getIntent为NULL

调用活动被销毁时,新活动上的Android getIntent为NULL

作者:互联网

你们有以下问题的解决方法吗?

在注册活动的onDestroy中(当用户按下后退按钮时),我调用一个新活动,以便用户可以输入当天的某些最终生产数据,然后通过电子邮件发送该报告.问题在于,在刚刚开始的活动中,对getIntent的调用返回null,而我必须从那里获取数据.

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);
}

================================================== =============================
构造函数称为活动:

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

函数getIntent返回null.我认为这是因为调用活动已经死了,但是我认为这很奇怪,因为我明确要求框架在关闭当前活动之前启动新活动.
有谁知道解决此问题的方法?我选择了这种解决方案,因此不必覆盖“后退”按钮.
另一个解决方案是阻塞调用线程,直到它从启动的应用程序获取取消阻塞信号为止,但我认为这也很丑陋.第三类数据管理器也可以解决数据源问题.
感谢您的想法和建议!

解决方法:

实际上,我想我知道您的问题是什么.我相信您正在拨打此电话:

 public AksProductionNumbers()

在活动构造函数中(如您在问题中所指出的),应在活动的onCreate方法中进行设置. getIntent将在构造函数中返回null.

标签:android,java,android-lifecycle
来源: https://codeday.me/bug/20191013/1908469.html