How to differentiate activity recreation is caused by screen rotation, or low memory condition

后端 未结 2 1508
无人及你
无人及你 2021-01-23 04:16

According to http://developer.android.com/training/basics/activity-lifecycle/recreating.html

There are various ways to trigger activity recreation.

  • Screen
相关标签:
2条回答
  • 2021-01-23 04:51

    One of the obvious observations is that, for restore activity from long pressed home, it will destroy and re-create Application as well.

    No, it will not.

    If your process was terminated, then when a new process is created for you (not matter how you launch the app), a new Application is created as part of that new process. This does not directly have anything to do with "restore activity from long pressed home".

    May I know, how can Activity or Fragment differentiate both cases?

    Ideally, they don't care. The only reason they would care is if they are dependent upon static data members, which may or may not be initialized, in which case the way that you are using those static data members is problematic.

    My previous assertion, that savedInstanceState would be non-null, is incorrect, and for that I apologize. The primary scenario for savedInstanceState is a configuration change. However, I was forgetting that launching from the recent-tasks list ("restore activity from long pressed home") will also pass in the last saved instance state. Launching by other means, such as from the home screen launcher, would pass in null for savedInstanceState.

    If you absolutely must distinguish between your-process-was-terminated and other scenarios, check some static data member to see if it is initialized or not.

    0 讨论(0)
  • 2021-01-23 04:52

    check this out -https://developer.android.com/reference/android/app/Activity#isChangingConfigurations()

    You can simply save value returned by isChangingConfigurations() in bundle and then check it in onCreate

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