3

Lets say I'm creating and displaying a GraphView graph in a fragment (for specific example of code, see here: Android/Java creating a helper class to create graphs), where is the correct place to actually create objects like the GraphView object, or the SensorManager object?

I've seen some people put these items (e.g. sensorManager = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE)) in onResume, and others put it in onCreateView. Does it have a big impact on CPU/ram usage?

I understand that onResume and onCreateView get called at different times, but lets say I create these objects in onResume, will I just end up with a whole series of the same object every time the user navigates to the page, or presses back to the page? Or does android overwrite the existing object, therefore keeping RAM usage in check?

Or would it be better to place those lines in onCreateView so when users navigate to the page the device doesn't need to recreate objects continuously (I assume this would happen if the code was in onResume)?

Community
  • 1
  • 1
Simon
  • 9,762
  • 15
  • 62
  • 119
  • No matter where you put the code, it will always be called. `onResume` happens after `onCreateView` – OneCricketeer Feb 29 '16 at 23:55
  • But looking at the fragment lifecycle (https://developer.xamarin.com/guides/android/platform_features/fragments/part_1_-_creating_a_fragment/Images/fragment_lifecycle.png) it seems like `onCreateView` will only be called after the view has been destroyed (e.g. if I close the app). Whereas `onResume` will be called after pause/stop events (e.g. if I press home and use another app for a while, and then go back, or if I navigate to another fragment and then press back button) – Simon Mar 01 '16 at 00:21
  • 1
    I was looking at the official android Fragment lifecycle, not Xamarin – OneCricketeer Mar 01 '16 at 01:12

1 Answers1

1

Your UI initialization should be done on onCreateView in a fragment. Eg initializing your GraphView.

Initialize components who's state you want to retain on pause/resume inside onCreate

geekoraul
  • 2,623
  • 2
  • 21
  • 33
  • So something like the sensor manager, not being UI, would go into onResume and get called every time someone backs/navigates to the fragment? – Simon Mar 01 '16 at 00:37
  • Does it make a difference whether you use `onActivityCreated` and `onCreateView`? It seems strange that android has a way to attach fragments to the parent activity as well as methods for creating views without attaching... – Simon Mar 01 '16 at 01:20
  • totally depends on the use cases. From `onActivityCreated`, you can initialize components that require a `Context` instance to be initialized. Also views can be accessed through `getViewById`. But since the fragment separates the functional concerns, its better to initialize UI `onCreateView` as you have your root view reference of the fragment there instead of doing `getActivity().findViewById` on `onActivityCreated` which is again extra code – geekoraul Mar 01 '16 at 01:35
  • @Simon I did update my answer as per my explanation above – geekoraul Mar 01 '16 at 01:41