startActivityForResult无法正常工作

时间:2015-12-07 10:18:35

标签: java android

我有两个活动Home.java和ApplicationSettings.java

现在两者都在扩展另一个活动BaseActivity.java,它位于一个库项目中,这个BaseActivity.java通过调用startActivityForResult()来调用DeviveListActivity.java; 在获取设备地址后,它返回到BaseActivity.java的onActivityResult()

BaseActivity.java的作用是什么,它连接到蓝牙设备, 我可以从Home.java活动以及ApplicationSettings.java活动创建与该蓝牙设备的连接

现在,如果我从Home.java建立连接然后转到ApplicationSettings.java活动,那么连接工作正常,因为从Home.java建立连接,所以在这种情况下没有问题

但是当我没有从Home.java建立任何连接并点击应用程序设置时,ApplicationSettings.java活动就会启动,当我尝试从这个活动ApplicationSettings.java建立连接时,就会出现问题

我收集了一些活动生命周期的日志,如下所示

这些是我从Home.java活动(工作正常)的连接

Home.java: OnCreate 
BaseActivity.java: OnCreate 
Home.java: OnStart 
BaseActivity.java: OnStart 
Home.java: OnResume 
BaseActivity.java: started activity for result (DeviveListActivity.java)
Home.java(15360): OnPause 
DeviceListActivity.java: OnCreate 
DeviceListActivity.java: OnResume 
DeviceListActivity.java: Result set for device address and called finish() 
DeviceListActivity.java: OnPause 
BaseActivity.java: In onActivityResult 
Home.java: OnResume 
DeviceListActivity.java: OnDestroy

我在上面的日志中注意到,当BaseActivity.java在OnStart中时,调用startactivityforresult(DeviveListActivity.java)并且在调用onActivityResult之前它永远不会进入OnStop

但是在我尝试从ApplicationSettings.java活动建立连接的下面的日志中,日志是

Home.java: OnCreate
BaseActivity.java: OnCreate
Home.java: OnStart
BaseActivity.java: OnStart
Home.java: OnResume
Home.java: OnPause
ApplicationSettings.java: OnCreate
ApplicationSettings.java: OnStart
ApplicationSettings.java: OnResume
Home.java: OnStop
BaseActivity.java: onStop
BaseActivity.java: started activity for result (DeviveListActivity.java) 
ApplicationSettings.java: OnPause
DeviceListActivity.java: OnCreate
DeviceListActivity.java: OnResume
DeviceListActivity.java: Result set for device address and called finish()
DeviceListActivity.java: OnPause
ApplicationSettings.java: OnResume  //here it comes(which is wrong)

在上面的日志中注意到,当创建并启动ApplicationSettings.java时,BaseActivity.java位于OnStop中,然后调用startactivityforresult(DeviveListActivity.java)并且不再启动BaseActivity 然后设置结果但是它应该转到BaseActivity.java:在onActivityResult中 而不是ApplicationSettings.java:OnResume被称为

我不想发生这种情况,因为整个流程在进入ApplicationSettings.java的OnResume时会发生变化

有什么建议我如何将它发送到BaseActivity.java的onActivityResult?

0 个答案:

没有答案