android - Fragment being created again -
i using fragment implement viewpager,the viewpager fragment,i implementing button , clicking on fragment goes previous fragment, on pressing button previous fragment again loading , getting response again. how can prevent that. have added code below.
photo fragment:-
public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.layout_photo, container, false); butterknife.inject(this, view); sharedpref = getactivity().getpreferences(context.mode_private); editor = sharedpref.edit(); if (sharedpref.contains("isviewpagerstarted")) { isviewpagerstarted = sharedpref.getboolean("isviewpagerstarted",false); } log.e("pagerstart", "" + isviewpagerstarted); if (isviewpagerstarted) { backbutton.setvisibility(view.visible); animation rightswipe = animationutils.loadanimation(getactivity(), r.anim.slide_out_left); backbutton.startanimation(rightswipe); backbutton.setvisibility(view.invisible); } else { backbutton.setvisibility(view.invisible); } editor.clear(); editor.commit(); // sharedpref = getactivity().getpreferences(context.mode_private); // options = new displayimageoptions.builder() .showimageforemptyuri(r.drawable.plc_gallery).cacheondisk(true) .build(); return view; } @override public void onactivitycreated(bundle savedinstancestate) { // todo auto-generated method stub super.onactivitycreated(savedinstancestate); mcontext = getactivity(); fonthelper.setfontface(galleryheader, fonttype.font5); getgalleryinfo(); onclickgridview(); } private void onclickgridview() { gridview.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view v, int position, long id) { // string backstatename = fragment.getclass().getname(); callviewpagerstarted(); viewpagerfragment viewpagerfragment = new viewpagerfragment(); bundle bun = new bundle(); bun.putint("position", position); bun.putstringarray("fullimage", imageurls); // bun.putstringarray("desc", description); viewpagerfragment.setarguments(bun); transaction = getactivity().getsupportfragmentmanager() .begintransaction(); transaction.addtobackstack(null); transaction.replace(r.id.framelayoutfragment, viewpagerfragment, "viewfrag"); transaction.commit(); /* * // send intent singleviewactivity intent intent = new * intent(getactivity(),viewpageractivity.class); // pass image * index bundle bundle=new bundle(); * bundle.putint("position",position); * bundle.putstringarray("imagearray",imageurls); * bundle.putstringarray("description",description); * intent.putextras(bundle); startactivity(intent); */ } }); } public void callviewpagerstarted() { editor.putboolean("isviewpagerstarted", true); editor.commit(); } @override public void onresume() { // todo auto-generated method stub super.onresume(); /* getgalleryinfo(); */ } private void getgalleryinfo() { getmesagallery gettempleinfo = new getmesagallery(); // gettempleinfo.setappid("8eb24199-6838-43ac-b9ca-40031ab37e65"); progresbar.start(mcontext); servicemanager.instance().enqueue(gettempleinfo, new servicelistener() { @override public void oncomplete(response arg0) { progresbar.stop(); getmesagallerysresponse response = (getmesagallerysresponse) arg0; if (response.isvalid()) { response.getimagedto(); final arraylist<imagedto> galleryimagelist = response .getimagedto(); thumbnailurls = new string[galleryimagelist.size()]; // description = new string[galleryimagelist.size()]; imageurls = new string[galleryimagelist.size()]; (int = 0; < galleryimagelist.size(); i++) { try { thumbnailurls[i] = galleryimagelist.get(i) .getthumbnailurl(); /* * description[i] = galleryimagelist.get(i) * .getdescription(); */ imageurls[i] = galleryimagelist.get(i).getimageurl(); loadgridview(); } catch (exception e) { e.printstacktrace(); } } } else { } } }); } private void loadgridview() { try { gridviewadapter adapter = new gridviewadapter(getactivity(), thumbnailurls); // set adapter gridview gridview.setadapter(adapter); } catch (exception e) { e.printstacktrace(); } } @override public void onsaveinstancestate(bundle outstate) { // add variable outstate here super.onsaveinstancestate(outstate); }
viewpager fragment:
public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.layout_viewpager, container, false); butterknife.inject(this, view); backbutton.setvisibility(view.visible); animation rightswipe = animationutils.loadanimation(getactivity(), r.anim.slide_in_right); backbutton.startanimation(rightswipe); log.e("insdie", "viewpager"); return view; } @override public void onactivitycreated(bundle savedinstancestate) { // todo auto-generated method stub super.onactivitycreated(savedinstancestate); bundle arguments = getarguments(); position = arguments.getint("position"); log.e("position", "" + position); fullimageuris = arguments.getstringarray("fullimage"); txtdesc = arguments.getstringarray("desc"); fonthelper.setfontface(description, fonttype.font6); description.setpaintflags(description.getpaintflags() | paint.underline_text_flag); /* * list<fragment> imagefragments = getfragments(); */ pageadapter = new mypageradapter(getactivity() .getsupportfragmentmanager(), fullimageuris); mpager.setadapter(pageadapter); mindicator.setviewpager_text(mpager,description,txtdesc); mpager.setcurrentitem(position); } /* * @override public void onresume() { // todo auto-generated method stub * super.onresume(); pageadapter = new mypageradapter(getactivity() * .getsupportfragmentmanager(),fullimageuri); * mpager.setcurrentitem(position); mpager.setadapter(pageadapter); * mindicator.setviewpager_text(mpager, description, txtdesc); } */ @onclick(r.id.left_arrow) public void tapleftarrow() { mpager.setcurrentitem(getitem(-1), true); } private int getitem(int i) { return mpager.getcurrentitem() + i; } @onclick(r.id.right_arrow) public void taprightarrow() { mpager.setcurrentitem(getitem(+1), true); } @onclick(r.id.imgbackbtn) public void movetopreviousfragment() { fragmentmanager fm = getfragmentmanager(); if (fm.getbackstackentrycount() > 0) { log.i("mainactivity", "popping backstack"); fm.popbackstack(); } else { log.i("mainactivity", "nothing on backstack, calling super"); } // getactivity().onbackpressed(); }
i checked instance of fragment , solved problem
public static photofragment instance() { if (photofragment == null) { photofragment = new photofragment(); } return photofragment; }
Comments
Post a Comment