java - Dynamically changing Views with functionality in onCreateView -


currently have connected activities handled way, activity --> activity b--activity c. discovered use nested fragments , contained in 1 activity. imagined be,

main activity --fragment -> fragment b -> fragment c 

from code below, manage dynamically populate text every created fragments. curious on how create fragments , use flow above different views.

so possible resolve use if statements in 1 oncreateview check if fragment displayed. thoughts me understand complex concept of dynamic creation of fragments. thanks

    public class collectiondemoactivity extends fragmentactivity {      /**      * {@link android.support.v4.view.pageradapter} provide fragments representing      * each object in collection. use {@link android.support.v4.app.fragmentstatepageradapter}      * derivative, destroy , re-create fragments needed, saving , restoring      * state in process. important conserve memory , best practice when      * allowing navigation between objects in potentially large collection.      */     democollectionpageradapter mdemocollectionpageradapter;      /**      * {@link android.support.v4.view.viewpager} display object collection.      */     viewpager mviewpager;      @targetapi(build.version_codes.honeycomb)     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_collection_demo);          // create adapter when requested, return fragment representing object in         // collection.         //          // viewpager , adapters use support library fragments, must use         // getsupportfragmentmanager.         mdemocollectionpageradapter = new democollectionpageradapter(getsupportfragmentmanager());          // set action bar.         final actionbar actionbar = getactionbar();          // specify home button should show "up" caret, indicating touching         // button take user 1 step in application's hierarchy.         actionbar.setdisplayhomeasupenabled(true);          // set viewpager, attaching adapter.         mviewpager = (viewpager) findviewbyid(r.id.pager);         mviewpager.setadapter(mdemocollectionpageradapter);     }      @override     public boolean onoptionsitemselected(menuitem item) {         switch (item.getitemid()) {             case android.r.id.home:                 // called when home (up) button pressed in action bar.                 // create simple intent starts hierarchical parent activity ,                 // use navutils in support package ensure proper handling of up.                 intent upintent = new intent(this, mainactivity.class);                 if (navutils.shoulduprecreatetask(this, upintent)) {                     // activity not part of application's task, create new task                     // synthesized stack.                     taskstackbuilder.from(this)                             // if there ancestor activities, should added here.                             .addnextintent(upintent)                             .startactivities();                     finish();                 } else {                     // activity part of application's task,                     // navigate hierarchical parent activity.                     navutils.navigateupto(this, upintent);                 }                 return true;         }         return super.onoptionsitemselected(item);     }      /**      * {@link android.support.v4.app.fragmentstatepageradapter} returns fragment      * representing object in collection.      */     public static class democollectionpageradapter extends fragmentstatepageradapter {          public democollectionpageradapter(fragmentmanager fm) {             super(fm);         }          @override         public fragment getitem(int i) {             fragment fragment = new demoobjectfragment();             bundle args = new bundle();             args.putint(demoobjectfragment.arg_object, + 1); // our object integer :-p             fragment.setarguments(args);             return fragment;         }          @override         public int getcount() {             // contrived example, have 100-object collection.             return 100;         }          @override         public charsequence getpagetitle(int position) {             return "object " + (position + 1);         }     }      /**      * dummy fragment representing section of app, displays dummy text.      */      public static class demoobjectfragment extends fragment {          public static final string arg_object = "object";          @override         public view oncreateview(layoutinflater inflater, viewgroup container,                 bundle savedinstancestate) {             view rootview = inflater.inflate(r.layout.load_main_menu_activity, container, false);             bundle args = getarguments();            //((textview) rootview.findviewbyid(android.r.id.text1)).settext(                  //   integer.tostring(args.getint(arg_object)));             return rootview;         }     } } 

public class firstfragment extends fragment implements onclicklistener {     @override     public view oncreateview(layoutinflater inflater, viewgroup container,         bundle savedinstancestate) {         view view = inflater.inflate(r.layout.first_fragment, container, false);         button nextbutton = (button) view.findviewbyid(r.id.button_first);         nextbutton.setonclicklistener(this);         return view;     } } 

Comments