android - Fragment's reference to mActivity becomes null after orientation change. Ineffective fragment state maintenance -


my application consists of several fragments. until i've had references them stored in custom application object, beginning think i'm doing wrong.

my problems started when realized fragment's references mactivity becomes null after orientation change. when call getactivity() after orientation change, nullpointerexception thrown. have checked fragment's onattach() called before make call getactivity(), still returns null.

the following stripped version of mainactivity, activity in application.

public class mainactivity extends baseactivity implements onitemclicklistener,         onbackstackchangedlistener, onslidingmenuactionlistener {      private listview mslidingmenulistview;     private slidingmenu mslidingmenu;      private boolean mmenufragmentvisible;     private boolean mcontentfragmentvisible;     private boolean mquickaccessfragmentvisible;      private fragmentmanager mmanager;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          /*          * boolean variables indicating of 3 fragment slots visible @ given time          */         mmenufragmentvisible = findviewbyid(r.id.menufragment) != null;         mcontentfragmentvisible = findviewbyid(r.id.contentfragment) != null;         mquickaccessfragmentvisible = findviewbyid(r.id.quickaccessfragment) != null;          if(!savedinstancestate != null) {             if(!mmenufragmentvisible && mcontentfragmentvisible) {                 setupslidingmenu(true);             } else if(mmenufragmentvisible && mcontentfragmentvisible) {                 setupslidingmenu(false);             }              return;         }          mmanager = getsupportfragmentmanager();         mmanager.addonbackstackchangedlistener(this);          final fragmenttransaction ft = mmanager.begintransaction();         ft.settransition(fragmenttransaction.transit_fragment_open);          if (!mmenufragmentvisible && mcontentfragmentvisible) {             /*              * content fragment visible, enable sliding menu              */             setupslidingmenu(true);             ontoggle();              ft.replace(r.id.contentfragment, getcustomapplication().getsportsfragment(), sportsfragment.tag);          } else if (mmenufragmentvisible && mcontentfragmentvisible) {             setupslidingmenu(false);             /*              * both menu , content fragments visible              */             ft.replace(r.id.menufragment, getcustomapplication().getmenufragment(), menufragment.tag);             ft.replace(r.id.contentfragment, getcustomapplication().getsportsfragment(), sportsfragment.tag);         }          if (mquickaccessfragmentvisible) {             /*              * quick access fragment visible              */             ft.replace(r.id.quickaccessfragment, getcustomapplication().getquickaccessfragment());         }          ft.commit();     }      private void setupslidingmenu(boolean enable) {         /*          * if enable true, enable sliding menu, if false          * disable          */     }      @override     public void onitemclick(adapterview<?> parent, view view, int position, long id) {          // launch fragment clicked menu     }      @override     public void onbackpressed() {         // let user press button when         // sliding menu open display content.         if (mslidingmenu != null && mslidingmenu.ismenushowing()) {             onshowcontent();         } else {             super.onbackpressed();         }     }      @override     public void onbackstackchanged() {         /*          * change selected position when stack changes          */         if(mslidingmenulistview != null) {             mslidingmenulistview.setitemchecked(getcustomapplication().getselectedposition(), true);             }     }      @override     public void ontoggle() {         if (mslidingmenu != null) {             mslidingmenu.toggle();         }     }      @override     public void onshowcontent() {         if (mslidingmenu != null) {             mslidingmenu.showcontent();         }     } } 

the following stripped version of customapplication. thoughts behind implementation guarantee 1 instance of each fragment throughout application's life cycle.

public class customapplication extends application {      private fragment mssportsfragment;     private fragment mcarsfragment;     private fragment mmusicfragment;     private fragment mmoviesfragment;      public fragment getsportsfragment() {         if(mssportsfragment == null) {             mssportsfragment = new sportsfragment();         }          return mssportsfragment;     }      public fragment getcarsfragment() {         if(mcarsfragment == null) {             mcarsfragment = new carsfragment();         }          return mcarsfragment;     }      public fragment getmusicfragment() {         if(mmusicfragment == null) {             mmusicfragment = new musicfragment();         }          return mmusicfragment;     }      public fragment getmoviesfragment() {         if(mmoviesfragment == null) {             mmoviesfragment = new moviesfragment();         }          return mmoviesfragment;     } } 

i interested in tips on how best implement multiple fragments , how maintain states. information, applicaion consists of 15+ fragments far. have done research , seems fragmentmanager.findfragmentbytag() bet, haven't been able implement it.

my implementation seems work except fact mactivity references become null after orientation changes, lets me believe may have memory leak issues well.

if need see more code, please let me know. purposely avoided including fragment code believe issues related activity , application implementations, may wrong.

thanks time.

my thoughts behind implementation guarantee 1 instance of each fragment throughout application's life cycle

this part, if not all, of source of difficulty.

on configuration change, android re-create fragments using public zero-argument constructor create new instance. hence, global-scope fragments not "guarantee 1 instance of each fragment".

please delete custom application class. please allow fragments re-created naturally, or if need live life of single activity, use setretaininstance(true). not attempt reuse fragments across activities.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -