Sunday, August 31, 2014

Programming things to remember 2014 Aug

Screen lock and activity rotation


I wanted to know, what fragment callbacks are called during different scenarios. I tested on Nexus 7 and on Samsung Galaxy SII.
Notable things:
  • inflated view doesn't have proper size till an implicitly added callback is called (ViewTreeObserver). So even during onResume the measured width and height may be 0.
  • the difference between the home press and rotation during fragment build down is the getActivity().isChangingConfigurations() flag
  • the difference between the home press and the back press is the onSaveInstanceState() callback. Except on samsung, which keeps the fragment, no onDestroyView+onDestroy will be called. But because of this, no onGlobalLayout is called either.
  • on samsung, the whole activity - which is behind the lock screen - gets all the callback events during rotation. So on nexus the onStart+onResume don't run until the lock screen is unlocked. On samsung, those callbacks are called right after the screen is turned on (and before it is unlocked). Likewise the rotation events, and because the lock screen on samsung is portrait only, it will recreate the fragment; and after unlocking it will recreate the fragment again (if the activity is fixed to landscape mode).



Nexus 7

Samsung S2
-- start new
onAttach()
onCreate() savedInstanceState: null
onCreateView() savedInstanceState: null
onActivityCreated() savedInstanceState: null
onStart() rootView.getMeasuredWidth(): 0
onResume() rootView.getMeasuredWidth(): 0
onGlobalLayout() rootView.getMeasuredHeight(): 672
-- back
onPause() isChangingConfigurations: false
onStop()
onDestroyView()
onDestroy()
onDetach()
-- start new
onAttach()
onCreate() savedInstanceState: null
onCreateView() savedInstanceState: null
onActivityCreated() savedInstanceState: null
onStart() rootView.getMeasuredHeight(): 0
onResume() rootView.getMeasuredHeight(): 0
onGlobalLayout() rootView.getMeasuredHeight(): 408
-- back
onPause() isChangingConfigurations: false
onStop()
onDestroyView()
onDestroy()
onDetach()
-- start new
onAttach()
onCreate() savedInstanceState: null
onCreateView() savedInstanceState: null
onActivityCreated() savedInstanceState: null
onStart() rootView.getMeasuredWidth(): 0
onResume() rootView.getMeasuredWidth(): 0
onGlobalLayout() rootView.getMeasuredHeight(): 672
-- replace fragment
onPause() isChangingConfigurations: false
onStop()
onDestroyView()
-- reopen fragment
onCreateView() savedInstanceState: null
onActivityCreated() savedInstanceState: null
onStart() rootView.getMeasuredWidth(): 0
onResume() rootView.getMeasuredWidth(): 0
onGlobalLayout() rootView.getMeasuredHeight(): 672
-- start new
onAttach()
onCreate() savedInstanceState: null
onCreateView() savedInstanceState: null
onActivityCreated() savedInstanceState: null
onStart() rootView.getMeasuredHeight(): 0
onResume() rootView.getMeasuredHeight(): 0
onGlobalLayout() rootView.getMeasuredHeight(): 408
-- replace fragment
onPause() isChangingConfigurations: false
onStop()
onDestroyView()
-- reopen fragment
onCreateView() savedInstanceState: null
onActivityCreated() savedInstanceState: null
onStart() rootView.getMeasuredHeight(): 0
onResume() rootView.getMeasuredHeight(): 0
onGlobalLayout() rootView.getMeasuredHeight(): 408
-- rotate
onPause() isChangingConfigurations: true
onSaveInstanceState()
onStop()
onDestroyView()
onDestroy()
onDetach()
onAttach()
onCreate() savedInstanceState: Bundle[...]
onCreateView() savedInstanceState: Bundle[...]
onActivityCreated() savedInstanceState: Bundle[...]
onStart() rootView.getMeasuredWidth(): 0
onResume() rootView.getMeasuredWidth(): 0
onGlobalLayout() rootView.getMeasuredHeight(): 672
-- rotate
onPause() isChangingConfigurations: true
onSaveInstanceState()
onStop()
onDestroyView()
onDestroy()
onDetach()
onAttach()
onCreate() savedInstanceState: Bundle[...]
onCreateView() savedInstanceState: Bundle[...]
onActivityCreated() savedInstanceState: Bundle[...]
onStart() rootView.getMeasuredHeight(): 0
onResume() rootView.getMeasuredHeight(): 0
onGlobalLayout() rootView.getMeasuredHeight(): 728 
-- home press
onPause() isChangingConfigurations: false
onSaveInstanceState()
onStop()
onDestroyView()
onDestroy()
onDetach()
-- reopen
onAttach()
onCreate() savedInstanceState: Bundle[...]
onCreateView() savedInstanceState: Bundle[...]
onActivityCreated() savedInstanceState: Bundle[...]
onStart() rootView.getMeasuredWidth(): 0
onResume() rootView.getMeasuredHeight(): 0
onGlobalLayout() rootView.getMeasuredHeight(): 672 
-- home press
onPause() isChangingConfigurations: false
onSaveInstanceState()
onStop()
-- reopen
onStart() rootView.getMeasuredHeight(): 728
onResume() rootView.getMeasuredHeight(): 728
-- lock
onPause() isChangingConfigurations: false
onSaveInstanceState()
onStop()
-- turn on
-- unlock
onStart() rootView.getMeasuredHeight(): 672
onResume() rootView.getMeasuredHeight(): 672
-- lock
onPause() isChangingConfigurations: false
onSaveInstanceState()
onStop()
-- turn on
onStart() rootView.getMeasuredHeight(): 728
onResume() rootView.getMeasuredHeight(): 728
-- unlock
-- lock
onPause() isChangingConfigurations: false
onSaveInstanceState()
onStop()
-- turn on
-- rotate
onDestroyView()
onDestroy()
onDetach()
onAttach()
onCreate() savedInstanceState: Bundle[...]
onCreateView() savedInstanceState: Bundle[...]
onActivityCreated() savedInstanceState: Bundle[...]
onStart() rootView.getMeasuredHeight(): 0
onResume() rootView.getMeasuredHeight(): 0
onPause() isChangingConfigurations: false
onGlobalLayout() rootView.getMeasuredHeight(): 1141
-- unlock
onResume() rootView.getMeasuredHeight(): 1141
-- lock (from landscape)
onPause() isChangingConfigurations: false
onSaveInstanceState()
onStop()
onDestroyView()
onDestroy()
onDetach()
onAttach()
onCreate() savedInstanceState: Bundle[...]
onCreateView() savedInstanceState: Bundle[...]
onActivityCreated() savedInstanceState: Bundle[...]
onStart() rootView.getMeasuredHeight(): 0
onResume() rootView.getMeasuredHeight(): 0
onPause() isChangingConfigurations: false
onGlobalLayout() rootView.getMeasuredHeight(): 728
-- turn on
onResume() rootView.getMeasuredHeight(): 728
-- unlock
onPause() isChangingConfigurations: true
onSaveInstanceState()
onStop()
onDestroyView()
onDestroy()
onDetach()
onAttach()
onCreate() savedInstanceState: Bundle[...]
onCreateView() savedInstanceState: Bundle[...]
onActivityCreated() savedInstanceState: Bundle[...]
onStart() rootView.getMeasuredHeight(): 0
onResume() rootView.getMeasuredHeight(): 0
onGlobalLayout() rootView.getMeasuredHeight(): 408