Is onResume() called before onActivityResult()?

前端 未结 4 1799
粉色の甜心
粉色の甜心 2020-12-13 01:25

Here is how my app is laid out:

  1. onResume() user is prompted to login
  2. If user logs in, he can continue using the app 3. If the user logs out at
相关标签:
4条回答
  • 2020-12-13 01:51

    With fragments it isn't even as simple as onActivityResult() being called before the call to onResume(). If the activity that you are returning to was disposed of in the interim, you will find that calls to (for example) getActivity() from onActivityResult() will return null. However, if the activity has not been disposed of, a call to getActivity() will return the containing activity.

    This inconsistency can be a source of hard-to-diagnose defects but you can check the behaviour of your application by enabling the developer option "Don't keep activities". I tend to keep this turned on - I'd rather see a NullPointerException in development than in production.

    0 讨论(0)
  • 2020-12-13 01:52

    Callback methods like onResume are not suitable places to achieve the requested functionality. I would suggest making a class & adding the sign-in/sign-out functionality there. when a signout callback is received, then call the sign-in functionality.

    0 讨论(0)
  • 2020-12-13 02:02

    You may want to consider abstracting away the login state from the activity. For example if a user can post comments, let the onPost action ping for login state and go from there, instead of from the activity state.

    0 讨论(0)
  • 2020-12-13 02:06

    The call to onActivityResult happens before onResume, actually (see the docs). Are you sure you're actually starting the activity you wanted with startActivityForResult and that you're setting the result of the invoked activity to RESULT_OK before returning a value to your activity? Try just putting a Log statement in your onActivityResult to log that value and make sure that gets hit. Also, where are you setting the value of the isLoggedIn preference? It seems like you should be setting that to true in your login activity before it returns anyways, but that's clearly not happening.

    Edit

    The docs say:

    You will receive this call immediately before onResume() when your activity is re-starting.

    0 讨论(0)
提交回复
热议问题