java - Is it possible to use common fragment generate function globally? -


i have common fragment generate function following:

// common fragment generation method public void generatefragment(string speechstring, string buttonstring) {      // store data passed fragment in bundle format     bundle args = new bundle();     args.putstring("speech", speechstring);     args.putstring("buttontitle", buttonstring);      // instantiate speech fragment , set arguments     speechfragment newfragment = new speechfragment();     newfragment.setarguments(args);      // dynamically add fragment view using fragment transaction     fragmenttransaction transaction = getsupportfragmentmanager().begintransaction();     transaction.replace(r.id.fragment_container, newfragment);     transaction.commit(); } 

i made function use multiple times in 1 of activity. now, found function can used activity.
have global variable class extends application. yes know global variable or global function should declared in class extends application class.
can't put function contains getsupportfragmentmanager() connected activity global one.

does know such function use between activity?

thank you!

you pass fragment manager method:

public static void generatefragment(fragmentmanager fragmentmanager, string speechstring, string buttonstring) 

i avoid having baseactivity others have suggested, because limits can extend from: e.g. fragmentactivity, actionbaractivity, listactivity, preferenceactivity, e.t.c. google "composition on inheritance" see why bad approach.

really, want split method up.

in speechfragment.java:

public static speechfragment newinstance(string speechstring, string buttonstring) {     // store data passed fragment in bundle format     bundle args = new bundle();     args.putstring("speech", speechstring);     args.putstring("buttontitle", buttonstring);      // instantiate speech fragment , set arguments     speechfragment newfragment = new speechfragment();     newfragment.setarguments(args);     return newfragment; } 

the speechfragment class responsible on how initiate itself. rest of method

fragmenttransaction transaction = getsupportfragmentmanager().begintransaction(); transaction.replace(r.id.fragment_container, speechfragment.newinstance(speechstring, buttonstring); transaction.commit(); 

can part of classes fragment used. have flexibility of changing container id, fragmentmanager use (could use child fragmentmanager of fragment instead), adding / attaching e.t.c instead of replacing - depending on how fragment needs used in activity.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -