android - Fragment code not being run when fragment is displayed on screen -


i have started creating fragments in app inflated using side menu. problem none of fragments, 1 of when automatically generated using android studio template, appear executing code.

this case of me missing obvious, , appreciated.

here mainactivity:

import android.app.activity; import android.os.bundle; import android.support.v4.app.*;  import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbar; import android.support.v7.app.actionbaractivity; import android.util.log; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup;   public class mainactivity extends actionbaractivity     implements navigationdrawerfragment.navigationdrawercallbacks {  /**  * fragment managing behaviors, interactions , presentation of  navigation drawer.  */ private navigationdrawerfragment mnavigationdrawerfragment;  /**  * used store last screen title. use in {@link #restoreactionbar()}.  */ private charsequence mtitle;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      mnavigationdrawerfragment = (navigationdrawerfragment)             getsupportfragmentmanager().findfragmentbyid (r.id.navigation_drawer);     mtitle = gettitle();      // set drawer.     mnavigationdrawerfragment.setup(             r.id.navigation_drawer,             (drawerlayout) findviewbyid(r.id.drawer_layout)); }  /*    @override public void onnavigationdraweritemselected(int position) {     // update main content replacing fragments     fragmentmanager fragmentmanager = getsupportfragmentmanager();     fragmentmanager.begintransaction()             .replace(r.id.container, placeholderfragment.newinstance(position + 1))             .commit(); }*/  @override public void onnavigationdraweritemselected(int position) {     android.support.v4.app.fragmentmanager fragmentmanager = getsupportfragmentmanager();     android.support.v4.app.fragmenttransaction transaction = fragmentmanager.begintransaction();      switch (position) {         case 1:     blogfragment blogfragment = new blogfragment();     transaction.replace(r.id.action_bar, blogfragment);     }  }  public void onsectionattached(int number) {     switch (number) {         case 1:             mtitle = getstring(r.string.title_section1);             break;         case 2:             mtitle = getstring(r.string.title_section2);             break;         case 3:             mtitle = getstring(r.string.title_section3);             break;     } }  public void restoreactionbar() {     actionbar actionbar = getsupportactionbar();     actionbar.setnavigationmode(actionbar.navigation_mode_standard);     actionbar.setdisplayshowtitleenabled(true);     actionbar.settitle(mtitle); }   @override public boolean oncreateoptionsmenu(menu menu) {     if (!mnavigationdrawerfragment.isdraweropen()) {         // show items in action bar relevant screen         // if drawer not showing. otherwise, let drawer         // decide show in action bar.         getmenuinflater().inflate(r.menu.main, menu);         restoreactionbar();         return true;     }     return super.oncreateoptionsmenu(menu); }  @override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();      //noinspection simplifiableifstatement     if (id == r.id.action_settings) {         return true;     }      return super.onoptionsitemselected(item); }  /**  * placeholder fragment containing simple view.  */ public static class placeholderfragment extends fragment {     /**      * fragment argument representing section number      * fragment.      */     private static final string arg_section_number = "section_number";      /**      * returns new instance of fragment given section      * number.      */     public static placeholderfragment newinstance(int sectionnumber) {         placeholderfragment fragment = new placeholderfragment();         bundle args = new bundle();         args.putint(arg_section_number, sectionnumber);         fragment.setarguments(args);         return fragment;     }      public placeholderfragment() {     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         view rootview = null;         rootview = inflater.inflate(r.layout.fragment_home, container, false);         switch(getarguments().getint(arg_section_number)) {             case 1:                 rootview = inflater.inflate(r.layout.fragment_home, container, false);                 break;             case 2:                 rootview = inflater.inflate(r.layout.fragment_blog, container, false);                 break;             case 3:                 rootview = inflater.inflate(r.layout.fragment_test, container, false);                 break;             case 4:                 //rootview = inflater.inflate(r.layout.fragment_info, container, false);                 break;             default:                 log.e("tag", "unrecognized section: " + getarguments().getint(arg_section_number));          }         return rootview;     }      @override     public void onattach(activity activity) {         super.onattach(activity);         ((mainactivity) activity).onsectionattached(                 getarguments().getint(arg_section_number));     } }  } 

here blog fragment, attempts open webview:

import android.app.activity; import android.app.fragment; import android.net.uri; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.webkit.webchromeclient; import android.webkit.websettings; import android.webkit.webview;    /** * simple {@link fragment} subclass. * activities contain fragment must implement * {@link blogfragment.onfragmentinteractionlistener} interface * handle interaction events. * use {@link blogfragment#newinstance} factory method * create instance of fragment. */ public class blogfragment extends fragment { // todo: rename parameter arguments, choose names match // fragment initialization parameters, e.g. arg_item_number private static final string arg_param1 = "param1"; private static final string arg_param2 = "param2";  // todo: rename , change types of parameters private string mparam1; private string mparam2;  private onfragmentinteractionlistener mlistener;   /**  * use factory method create new instance of  * fragment using provided parameters.  *  * @param param1 parameter 1.  * @param param2 parameter 2.  * @return new instance of fragment blog.  */ // todo: rename , change types , number of parameters public static blogfragment newinstance(string param1, string param2) {     blogfragment fragment = new blogfragment();     bundle args = new bundle();     args.putstring(arg_param1, param1);     args.putstring(arg_param2, param2);     fragment.setarguments(args);     return fragment; }  public blogfragment() {     // required empty public constructor }  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     if (getarguments() != null) {         mparam1 = getarguments().getstring(arg_param1);         mparam2 = getarguments().getstring(arg_param2);     } }  @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {     webview wv;      view view=inflater.inflate(r.layout.fragment_blog, null);      wv = (webview) view.findviewbyid(r.id.webview);     websettings settings = wv.getsettings();     wv.setwebchromeclient(new webchromeclient() {     });     final string mimetype = "text/html";     final string encoding = "utf-8";     string html = gethtml();     settings.setjavascriptenabled(true);     wv.loaddatawithbaseurl("", html, mimetype, encoding, "");      return view;  }  // todo: rename method, update argument , hook method ui event public void onbuttonpressed(uri uri) {     if (mlistener != null) {         mlistener.onfragmentinteraction(uri);     } }  @override public void onattach(activity activity) {     super.onattach(activity);     try {         mlistener = (onfragmentinteractionlistener) activity;     } catch (classcastexception e) {         throw new classcastexception(activity.tostring()                 + " must implement onfragmentinteractionlistener");     } }  @override public void ondetach() {     super.ondetach();     mlistener = null; }  public string gethtml() {     string html = "<iframe width=\"100%\" height=\"100%\" src=\"http://www.bbc.co.uk\" frameborder=\"0\" allowfullscreen></iframe>";     return html; }   /**  * interface must implemented activities contain  * fragment allow interaction in fragment communicated  * activity , potentially other fragments contained in  * activity.  * <p/>  * see android training lesson <a href=  * "http://developer.android.com/training/basics/fragments/communicating.html"  * >communicating other fragments</a> more information.  */ public interface onfragmentinteractionlistener {     // todo: update argument type , name     public void onfragmentinteraction(uri uri); } 

}

thanks in advance.

to pinpoint error should add default block switch statement:

    switch(getarguments().getint(arg_section_number)) {         case 1:             rootview = inflater.inflate(r.layout.fragment_home, container, false);             break;         case 2:             rootview = inflater.inflate(r.layout.fragment_blog, container, false);             break;         case 3:             rootview = inflater.inflate(r.layout.fragment_test, container, false);             break;         case 4:             //rootview = inflater.inflate(r.layout.fragment_info, container, false);             break;         default:             log.e("tag", "unrecognized section: " + getarguments().getint(arg_section_number));     } 

it seems me argument not set when oncreateview called. if isn't set, getint() returns 0, doesn't fall of cases in switch statement.

edit:

after closer inspection, believe should fine simpler drawer item selection listener:

@override public void onnavigationdraweritemselected(int position) {     fragmentmanager fragmentmanager = getsupportfragmentmanager();     fragmenttransaction transation = fragmentmanager.begintransaction();     switch (position) {         case 1:             homefragment homefragment = new homefragment();             homefragment.setarguments(someargs);             transation.replace(r.id.container, homefragment);             break;         case 2:             blogfragment blogfragment = new blogfragment();             blogfragment.setarguments(someargs);             transation.replace(r.id.container, blogfragment);             break;         case 3:             testfragment testfragment = new testfragment();             testfragment.setarguments(someargs);             transation.replace(r.id.container, testfragment);             break;         case 4:             //rootview = inflater.inflate(r.layout.fragment_info, container, false);             break;         default:             return;     }     transation.commit(); } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -