startActivityForResult(…) immediately returns 0

最后都变了- 提交于 2020-03-06 09:10:22

问题


When I call startActivityForResult(new Intent(getActivity(), VkAuth.class), VK_ID); on Samsung (on HTC and Nexus everything works fine), onActivityResult with requestCode == VK_ID and result code 0 (Cancelled) is called before the VkAuth Activity is created.
The parent Activity has android:launchMode="singleInstance". startActivityForResult is called from a Fragment attached to the parent Activity. Log

E/AuthenticationFragment: vkLogIn
E/AuthenticationFragment: startActivityForResult 9101
E/AuthenticationFragment: onPause
E/MainActivity: onPause
E/MainActivity: onActivityResult before super: request - 271245, result - 0
E/AuthenticationFragment: request - 9101, result - 0
E/MainActivity: onActivityResult after super: request - 271245, result - 0
E/AuthenticationFragment: onPause
E/MainActivity: onPause
E/VkAuth: onCreate
E/VkAuth: onResume

回答1:


after i removed android:launchMode="singleInstance", this misbehaivior stoped




回答2:


I had a similar behaviour on a Samsung Galaxy S4 running Android 4.4.2 API 17. I was starting an Activity with:

Intent intent = new Intent(this, MyActivity.class);
startActivityForResult(intent, CUSTOM_CODE);

However the onActivityResult got called immediately with resultCode=0 (ACTIVITY.RESULT_CANCELED). It was working perfectly fine on other devices.

Based on what @Yarh said, i looked in AndroidManifest and figured out that the Activity that executed the startActivityForResult has android:launchMode="singleInstance".

Removing this line definitely fixed the issue.




回答3:


You have not posted enough code for us to really see what is going on but I would check the following:

When you return from the activity you have just started up using startActivityForResult, please check whether you have setResult to RESULT_OK:

Intent returnIntent = new Intent();
setResult(RESULT_OK, returnIntent);


来源:https://stackoverflow.com/questions/34108901/startactivityforresult-immediately-returns-0

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