Android fragments and activities questions -


this sort of extension previous question, more questions , less code (android starting activity or calling method class (using fragments)). i'm trying better idea of how fragments , activities work inside android apps while writing sample app of navigation drawer combined location api code.

as understand it, navigation drawer has created extending activity ui components properly. i've done far broken down ui panes different fragments different xml , java files each. java file builds ui xml specified in it, , displays content in content pane while keeping nav drawer in tact, similar this: http://manishkpr.webheavens.com/android-navigation-drawer-example-using-fragments/.

each of fragments, exception of "home", needs code location apis/location activity, based on mainactivity: https://developer.android.com/training/location/retrieve-current.html. having 2 problems:

  1. my location activity class has code oncreate populate textview variables ui. ui handled fragments. every time location activity created, variables null because activity doesn't seem communicate fragment ui.

  2. the buttons in fragments call methods exist in location activity. , don't seem communicate activity, fragment java class.

so first off, sound proper way this? using fragments handle options in navigation drawer , producing content in content pane? if so, how make work location activity?

ive encountered saying here in many aspects, , don't think there right , wrong here.. because work don't give free hand want.

  1. answer: yes right, have kind of "egg & chicken" situation here, so, how solve :

a. lets assume don't want user until finish inflating fragment, first action (ui/ux) showing progress dialog.

b. connivance matter only, assume adding or add fragment inside kind of layout (lets relative layout) .. layout can extended own class see : http://developer.android.com/training/custom-views/create-view.html

now, @ special class can use 1 of 2 (depending moment want enter) either - @override protected void onfinishinflate() { super.onfinishinflate(); } or - @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { super.onmeasure(widthmeasurespec, heightmeasurespec); } (put in mind onmeasure potential called many time , not on start.)

so, have view, being extended, can grep on oncreate of our activity even before being created , inflated. in extended view add public function like:

public void setonfinishinflatingthechikenortheegglistener(ionfinished listenr) ...

the ionfinished simple interface simple function, tell view finish inflating himself on screen.

c. let activity implements ionfinished, , pass setonfinishinflatingthechikenortheegglistener function param, save there, , when 1 of function override before being called can trigger listener.

d. function been trigger in activity, here safe fragment, , him.. cause sure of views in place... (dont forget dismiss dialog @ point).

(if didnt understand, tell me , ill make example you...)

2. answer: lets continue second problem, easier before, in fragment have buttons, them set onclicklistner ... in shoud use following:

activity activity =  getactivity(); if(activity!=null && activity instanceof locationactivity ){ locationactivity mylocationactivity = (locationactivity)activity; //**************************************************************** // call here public function in activity, set listeners, anything..  } 

hope helps understand more fragments...


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -