Reputation: 10150
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
)?
Upvotes: 3
Views: 5862
Reputation: 2653
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
Upvotes: 1