java - Sending string to Activity and then to a Fragment of Activity -


i having trouble simple app. app starts in mainactivity can press camera icon. opens implicit intent taking photo. when photo taken, activity displayimageactivity opened. activity consists of 2 fragments: 1 holds imageview displaying photo , 1 holds textviews displays information photo (filename, size, location etc.). use viewpager having horizontal swipe capabilities.

now problem. should note not consistent problem. app crashes, works fine. problem lies in getting image path onactivityresult in mainactivity 2 fragments can image , info. here onactivityresult method:

@override protected void onactivityresult(int requestcode, int resultcode, intent data) {     super.onactivityresult(requestcode, resultcode, data);      if (requestcode == 42 && resultcode == result_ok) {         log.d(tag, "mainactivity onactivityresult method called.");          intent intentshowpicture = new intent(this, displayimageactivity.class);          intentshowpicture.putextra(picture_key, imgpath);          startactivity(intentshowpicture);     } } 

so put image path taking picture in bundle , start displayimageactivity. in displayimageactivity, this:

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_display_image);     log.d(mainactivity.tag, "displayimageactivity oncreate called.");      imgpath = getintent().getextras().getstring(mainactivity.picture_key);      madapter = new fragmentadapter(getsupportfragmentmanager());     mviewpager = (viewpager) findviewbyid(r.id.pager);     mviewpager.setadapter(madapter); 

then have method return straing imgpath:

public string getimgpath() {     return imgpath; } 

then inside fragment (picturefragment) try retrieve imgpath this:

@override public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {     displayimageactivity activity = (displayimageactivity) getactivity();     imgpath = activity.getimgpath(); 

but mentioned earlier, getimgpath method returns null , app crashes when try retrieve photo. works fine. kinda lost why is. because fragment constructed before imgpath variable assigned in displayimageactivity, variable null?

i kinda new android, might not best approach. did top of head. ideas why happening?

if want pass data activity fragment, use approach:

in activity:

    bundle bundle = new bundle();     string imgpath = "path/to/my/image";     bundle.putstring("imgpath", imgpath );     picturefragment frag = new picturefragment();     frag.setarguments(bundle);     transaction.replace(r.id.fragment_single, frag);     transaction.commit(); 

fragment:

reading value in fragment

    @override     public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {         string myvalue = this.getarguments().getstring("imgpath");         ...         ...         ...     } 

for more information, take @ question how pass variable activity fragment, , pass back?


Comments

  1. This method is extremely useful for those of you who want to build a bigger list of leads on your website. However, if you are looking to save time, you should try to import your Contact Intelligence - Lix manually first.

    ReplyDelete

Post a Comment

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -