android - Communicating between fragments - accessing class inside fragment? -
i attempting communicate between 2 fragments in android program. i've been following guide communicating other fragments , going far. have 1 fragment sending data host activity, , activity correctly acknowledges data through log statements. have referenced second fragment within activity, , set send information fragment, therein lies problem; method in second fragment, need call host activity, inside of class inside of fragment, , what's more, method relies on data defined inside of fragment. i've tried call method directly, cannot seem it. there way of getting past these limitations?
host activity:
package com.example.chris.drawingtest; import android.app.activity; import android.os.bundle; import android.util.log; public class drawingactivity extends activity implements toolbarfragment.toolselectionlistener { public void sendnewvalue(int newvalue) { log.d("data received toolbar fragment: ", "the id of button pressed " + newvalue); drawingfragment drawfrag = (drawingfragment)getfragmentmanager().findfragmentbyid(r.id.drawing); if (drawfrag != null) { //changetool (part of drawview) want call method } } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_drawing); } }
fragment code:
package com.example.chris.drawingtest; import android.app.activity; import android.app.fragment; import android.content.context; import android.graphics.bitmap; import android.graphics.canvas; import android.graphics.paint; import android.graphics.path; import android.graphics.point; import android.os.bundle; import android.util.attributeset; import android.util.log; import android.view.display; import android.view.layoutinflater; import android.view.motionevent; import android.view.view; import android.view.viewgroup; import android.view.windowmanager; import android.widget.imagebutton; import android.widget.linearlayout; import com.example.chris.drawingtest.r; import java.text.attributedcharacteriterator; /** * created chris on 11/28/2014. */ public class drawingfragment extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { log.d("oncreateview: ","this ran successfully"); view v = inflater.inflate(r.layout.fragment_drawing, container, false); drawview drawview = new drawview(v.getcontext()); return v; } public static class drawview extends view { private path drawpath = new path(); private paint drawpaint = new paint(); private paint canvaspaint; private canvas drawcanvas; private bitmap canvasbitmap; private int paintcolor = 0xff000000; //opaque black pencil private int canvascolor = 0xffffffff; //pure white canvas public drawview(context context) { super(context); log.d("drawview: ", "method called"); drawpaint.setcolor(paintcolor); drawpaint.setstrokewidth(5); drawpaint.setstyle(paint.style.stroke); drawpaint.setstrokejoin(paint.join.round); drawpaint.setstrokecap(paint.cap.round); canvaspaint = new paint(paint.dither_flag); point p = getscreensize(context); int w = p.x; int h = p.y; onsizechanged(w,h,0,0); } public drawview(context context, attributeset attrs) { super(context); log.d("drawview: ", "method called"); drawpaint.setcolor(paintcolor); drawpaint.setstrokewidth(20); drawpaint.setstyle(paint.style.stroke); drawpaint.setstrokejoin(paint.join.round); drawpaint.setstrokecap(paint.cap.round); canvaspaint = new paint(paint.dither_flag); point p = getscreensize(context); int w = p.x; int h = p.y; onsizechanged(w,h,0,0); } public drawview(context context, attributeset attrs, int defstyle) { super(context); log.d("drawview: ", "method called"); drawpaint.setcolor(paintcolor); drawpaint.setstrokewidth(10); drawpaint.setstyle(paint.style.stroke); drawpaint.setstrokejoin(paint.join.round); drawpaint.setstrokecap(paint.cap.round); canvaspaint = new paint(paint.dither_flag); point p = getscreensize(context); int w = p.x; int h = p.y; onsizechanged(w,h,0,0); } // ------------------------ method need call ---------------------- public boolean changetool(int toolcode){ switch(toolcode){ case 0: drawpaint.setcolor(paintcolor); break; case 1: drawpaint.setcolor(canvascolor); break; default: return false; } return true; } // ------------------------ method need call ---------------------- protected point getscreensize(context context) { windowmanager wm = (windowmanager) context.getsystemservice(context.window_service); display display = wm.getdefaultdisplay(); point size = new point(); display.getsize(size); return size; } protected void onsizechanged(int w, int h, int oldw, int oldh) { super.onsizechanged(w,h,oldw,oldh); log.d("onsizechanged: ", "method called"); canvasbitmap = bitmap.createbitmap(w,h,bitmap.config.argb_8888); drawcanvas = new canvas(canvasbitmap); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); log.d("ondraw: ", "method called"); canvas.drawbitmap(canvasbitmap,0,0,canvaspaint); canvas.drawpath(drawpath, drawpaint); } @override public boolean ontouchevent(motionevent event) { float touchx = event.getx(); float touchy = event.gety(); log.d("ontouchevent: ", "method called"); switch(event.getaction()) { case motionevent.action_down: drawpath.moveto(touchx,touchy); break; case motionevent.action_move: drawpath.lineto(touchx,touchy); break; case motionevent.action_up: drawcanvas.drawpath(drawpath, drawpaint); drawpath.reset(); break; default: return false; } invalidate(); return true; } } }
thanks can give!
you'll need have method in fragment call activity, , fragment method can pass on view.
public class drawingfragment extends fragment { drawview drawview; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { log.d("oncreateview: ","this ran successfully"); view v = inflater.inflate(r.layout.fragment_drawing, container, false); drawview = new drawview(v.getcontext()); return v; } public boolean changetool(int toolcode){ if (drawview != null) { drawview.changetool(toolcode); } }
if have lot of such methods want call, , gets tedious write of these functions, consider moving of drawview fields drawingfragment, , drawingfragment actual work, while drawview referred fragment's fields needed.
Comments
Post a Comment