63700

Problems with Fragment lifecycle and onCreate being called on non existent fragment

I am testing fragments in Android and I'm having some confusing behavior with Fragment life cycle. I have a activity that uses layouts in xml for both landscape and portrait modes. I have some code that accesses a EditText object that is defined in one of the fragment layouts. If I start the app in landscape mode things work. I access the component in the onResume() fragment method to update some text.

According to the documentation the onResume() fragment method is called when the fragment is visible to the user. However, if I rotate the screen this method gets called again even though this fragment is not defined in the portrait layout. This call to onCreate causes a NullPointerException becuase this method references the EditText object. Why is onResume method called on a fragment that is not part of the new layout? How and where is the proper place to modify layout data in a fragment. I have tried onStart, onResume, onActivityCreated etc, but all seem to return the same error.

Any help would be most appreciated.

Answer1:

When you rotate the screen, Android saves your fragments in a Bundle and recreates them when it recreates the activity. That is why you get calls to a non-existing (actually just invisible) fragment. You need to handle this situation in the fragment code, or simply have both fragments in the land and port layouts, where you set the fragment visibility to GONE if you don't need it.

A simple way to check if the fragment is visible in code is this:

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (container == null) { return null; } }

If container is null, your fragment is being re-created from a Bundle and won't be shown (since there is no container). You then have to check whether getView() is null and short-circuit your code accordingly. This can get messy though, so beware :)

Recommend

  • Current activities onResume() is not called when another Activity is launched from lock screen via n
  • Android sensor listener does not get unregistered or sensor still retrieves values
  • Facebook API: cannot login on Android
  • Getting media player state in windows phone 7
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • How do I get HTML corresponding to current DOM tree?
  • Typescript - Unable to get 'import' statement to function
  • Disabling Alt-F4 on a Win Forms NotifyIcon
  • req.body is undefined - nodejs
  • Java applet as stand-alone Windows application?
  • Updating server-side rendering client-side
  • MySQL WHERE-condition in procedure ignored
  • Trying to switch camera back to front but getting exception
  • Symfony2: How to get request parameter
  • How to show dropdown in excel using jrxml (jasper api)?
  • Web-crawler for facebook in python
  • Jquery - Jquery Wysiwyg return html as a string
  • Apache 2.4 - remove | delete | uninstall
  • Akka Routing: Reply's send to router ends up as dead letters
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Is there a mandatory requirement to switch app.yaml?
  • Comma separated Values
  • retrieve vertices with no linked edge in arangodb
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • SQL merge duplicate rows and join values that are different
  • trying to dynamically update Highchart column chart but series undefined
  • Benchmarking RAM performance - UWP and C#
  • Free memory of cv::Mat loaded using FileStorage API
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Angular 2 constructor injection vs direct access
  • Understanding cpu registers
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Add sale price programmatically to product variations
  • Programmatically clearing map cache
  • Reading document lines to the user (python)
  • java string with new operator and a literal
  • How to load view controller without button in storyboard?