When the mobile phone switches between horizontal and vertical screens, the fragment status is lost, causing a null pointer exception to be reported when the data callback requested by the child thread is displayed. How to solve it
巴扎黑2017-05-31 10:40:57
It feels like screen rotation triggers redrawing. Try adding the following attributes to the current Activity in Manifast: android:configChanges="orientation|screenSize"
为情所困2017-05-31 10:40:57
Whether you are rotating the screen or closing
Activity
, you should cancel the response monitoring of the network interface whenFragment#onDestroy()
orFragment#onDestroyView()
. Otherwise, even if it is solved by settingandroid:configChanges
When the screen is rotated,Activity
is rebuilt and causes the App to crash. Other scenarios may still cause the App to crash.
It is recommended to read the Android source code to understand the following knowledge points:
Activity
manages Fragment
Fragment
manages View
Activity
& Fragment
manage Loader
It is also recommended to use Retrofit + RxAndroid + RxLifecycle to implement network interfaces and calls. For page data management, you can consider Loader
.