/** * has a parameter return value *
/** * no parameter with return value *
3 Setting up the manager class
public class Functionmanager {private static Functionmanager instance = NULL; public static final String TAG = FunctionManager.class.getSimpleName () + "-------->"; /** container, which is used to store method names corresponding to methods name value, corresponds to the parameter return object */Private hashmap<string,functionwithparamwithresult> MFUNCTIONWITHP Aramwithresulthashmap = null; Private hashmap<string,functionwithparamonly> mfunctionwithparamsonlyhashmap = null; Private hashmap<string,functionwithresultonly> mfunctionwithresultonlyhashmap = null; Private hashmap<string,functionnoparamnoresult> mfunctionnoparamnoresulthashmap = null; Private Functionmanager () {mfunctionnoparamnoresulthashmap = new hashmap<> (); Mfunctionwithparamwithresulthashmap = new hashmap<> (); Mfunctionwithparamsonlyhashmap = new hashmap<> (); Mfunctionwithresultonlyhashmap = new hashmap<> (); }public static Functionmanager getinstance () {if (null = = Instance) {instance = new Functionmanager (); }return instance; }/** * Add no parameter no return value method * @return */public Functionmanager addfunction (Functionnoparamnoresult function) {mfunct Ionnoparamnoresulthashmap.put (function.mfunctionname,function); return this; }/** * Add a method with return value * @return */public Functionmanager addfunction (functionwithresultonly function) {mfunction Withresultonlyhashmap.put (function.mfunctionname,function); return this; }/** * Add Parameter method * @return */public Functionmanager addfunction (functionwithparamonly function) {MFUNCTIONWI Thparamsonlyhashmap.put (function.mfunctionname,function); return this; }/** * Add a method with the parameter return value * @return */public Functionmanager addfunction (Functionwithparamwithresult function) {MF Unctionwithparamwithresulthashmap.put (function.mfunctionname,function); return this; }/** * Call no return value no parameter method * @param funname */public void Invokenoall (String funname) throws Nullpointerexceptio n {if (textutils.iseMpty (Funname)) {LOG.E (TAG, "Funname is null!");} else {if (null! = Mfunctionnoparamnoresulthashmap) {Functionnoparamnoresult function = Mfunctionnoparamno Resulthashmap.get (Funname); if (null! = function) {function.function (); }else {log.e (TAG, "function is null!");}} else {throw new NullPointerException ("Mfunctionnoparamnoresulthashmap can not is null, please first init Functionmanager!) "); }}}/** * Call the method with parameters * @param funname */public <Param> void Invokewithparamonly (String Funn Ame,param Param) throws NullPointerException {if (Textutils.isempty (Funname)) {LOG.E (TAG, "Funname is null!"); }else {if (null! = Mfunctionwithparamsonlyhashmap) {functionwithparamonly<param> function = Mfunctionwithparamsonlyhashmap.get (Funname); if (null! = function) { Function.function (param); }else {log.e (TAG, "function is null!");}} else {throw new NullPointerException ("Mfunctionwithparamsonlyhashmap can not is null, please first init functionmanager!") ); }}}/** * Call method with return value * @param funname */public <Result> Result invokewithresultonly (String Funname, class<result> c) throws NullPointerException {if (Textutils.isempty (Funname)) {LOG.E (TAG, "funNa Me is null! ");} else {if (null! = Mfunctionwithresultonlyhashmap) {functionwithresultonly function = Mfunctionwithresulto Nlyhashmap.get (Funname); if (null! = function) {if (null! = c) {return C.cast (function.function ()); }else {return (Result) function.function (); }}else {log.e (TAG, "function is null!");}} else {throw new NullPointerException ("Mfunctionwithparamsonlyhashmap can not is null, please first init functionmanager! ");}} return null; }/** * Call a method with a parameter with a return value * @param funname */public <Result,Param> Result Invokewithall (String funname, C Lass<result> C,param Param) throws NullPointerException {if (Textutils.isempty (Funname)) {LOG.E (TAG, "Funn AME is null! ");} else {if (null! = Mfunctionwithparamwithresulthashmap) {functionwithparamwithresult<result,param> fu Nction = Mfunctionwithparamwithresulthashmap.get (funname); if (null! = function) {if (null! = c) {return c.cast (function.function (param)); }else {return function.function (param); }}else {log.e (TAG, "function is null!");}} else {throw new NullPointerException ("Mfunctionwithparamsonlyhashmap can not is null, please first init functionmanager!") ); }}reTurn null; }} 4 Write a method in activity
public void setfunctionforfragment (String tag) {if (Textutils.isempty (tag)) {LOG.E (MainActivity.class.getSimpleName ( ), "tag is null!"); Return } basefragment fragment = (basefragment) fm.findfragmentbytag (tag); Functionmanager Functionmanager = Functionmanager.getinstance (); Functionmanager.addfunction (New Functionnoparamnoresult (functionnoparamnoresult) {@Override public void function () {Toast.maketext (Mainactivity.this, "no parameter no return value", Toast.length_short). Show (); } }); Functionmanager.addfunction (New functionwithresultonly<string> (functionwithresultonly) {@Override public Str ing function () {return "no parameter has a return value"; } }); Functionmanager.addfunction (New functionwithparamonly<string> (functionwithparamonly) {@Override public void F Unction (String o) {toast.maketext (Mainactivity.this, O, Toast.length_short). Show (); } }); Functionmanager.addfunction (New Functionwithparamwithresult< String,string> (functionwithparamwithresult) {@Override public string function (String o) {return o; } }); Fragment.setfunctionmanager (Functionmanager);}5 Edit a fragment base class (it is possible to integrate the class directly when the utility fragment), and bind the interface established above
public class Basefragment extends Fragment{public Functionmanager Mfunctionmanager; Private mainactivity mainactivity; public void Setfunctionmanager (Functionmanager mfunctionmanager) {this.mfunctionmanager = Mfunctionmanager; } @Override public void Onattach (context context) {Super.onattach (context); If (context instanceof mainactivity) {mainactivity = (mainactivity) context; Mainactivity.setfunctionforfragment (Gettag ()); } else {throw new RuntimeException (context.tostring () + "must implement Onfragmentinteractionlistener"); } } @Override public void Ondetach () {Super.ondetach (); mainactivity = null;} }
6 Inheritance Basefragment
public class Noparamnoresultfragment extends Basefragment {private Handler mhandler; Public noparamnoresultfragment (Handler Handler) {//Required empty public constructor this.mhandler = Handler; } @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); if (getarguments () = null) {}} @Override public View oncreateview (Layoutinflater inflater, ViewGroup Contai NER, Bundle savedinstancestate) {//Inflate the layout for this fragment view view = Inflater.inflate (R.layout.fragment_no_param_no_result, container, false); View.findviewbyid (R.id.txt_handle). Setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {Message message = Mhandler.obtainmessage (); Message.what = 0x123; Message.obj = "Handle Communication"; Mhandler.sendmessage (message); } }); View.finDviewbyid (R.id.txt_noall). Setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {Mfunctionmanager.invokenoall (Mainactivity.functionnoparamnoresult); } }); View.findviewbyid (R.id.txt_result). Setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {//Mfunctionmanager.invokenoall (Mainactivity.functionnoparamnoresult); String result = Mfunctionmanager.invokewithresultonly (Mainactivity.functionwithresultonly,string.class); Toast.maketext (getactivity (), result, Toast.length_short). Show (); } }); View.findviewbyid (R.id.txt_param). Setonclicklistener (New View.onclicklistener () {@Override public void OnClick ( View v) {mfunctionmanager.invokewithparamonly (mainactivity.functionwithparamonly, "parameter no return value"); } }); View.findviewbyid (R.id.txt_withall). Setonclicklistener (New View.onclicklistener (){@Override public void OnClick (View v) {String result = Mfunctionmanager.invokewithall (mainact Ivity. Functionwithparamwithresult,string.class, "has a parameter return value"); Toast.maketext (getactivity (), result, Toast.length_short). Show (); } }); return view; }}Description, the above when clicking on the control, will trigger the method added in the activity, so that the activity and fragment real-time communication, near and achieve the communication between fragment
Https://gitee.com/lzbgit/AppInterface
Architect's path to growth-an object-oriented universal interface based on Android fragment communication