Android implements fragment cross activity callback communication

Source: Internet
Author: User

The individual likes the complex thing simplification, here does not do the theoretical description, realizes this method certainly many, for example can use the broadcast, realizes the activity callback and then calls fragment, here is the direct activity callback to fragment


    1. Effect flowchart

      650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/82/D0/wKiom1dhiADSFxotAAB8leBqojA369.png "title=" Qq20160616005117.png "alt=" Wkiom1dhiadsfxotaab8lebqoja369.png "/>

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/82/D0/wKiom1dhiVbwyeFBAADHxhHSNXY964.png-wh_500x0-wm_3 -wmp_4-s_2351085149.png "title=" Screenshot_2016-06-16-00-41-28.png "alt=" Wkiom1dhivbwyefbaadhxhhsnxy964.png-wh_ "/>

2. Implementation process

2.1 Interface classes, such as Callbackinterface.java

Package Com.cyy.test;public interface Callbackinterface<t> {void OnCallback (T var);}

2.2 Activity Interface Method settings

package com.cyy.test;import android.app.activity;import android.os.bundle;import  android.view.view;import android.widget.button;public class activity2 extends  activity implements view.onclicklistener {     @Override      protected void oncreate (bundle savedinstancestate)  {         super.oncreate (savedinstancestate);         Setcontentview (R.layout.activity2);         button button= (Button) Findviewbyid (R.id.button);         button.setonclicklistener (this);     }     @Override     public void  OnClick (view v)  {        switch  (V.getid ()) {             case R.id.button:                 //Execution Callback                  instance.callbacker.oncallback ("Activity2 callback");                 if  (instance!=null) {                     instance.finish ();                      instance=null;                 }                 Finish ();                 break ;         }    }    ////////////////////////////     //Second Step     //setup Singleton     public static activity2  instance;    public static activity2 getinstance () {         if  (instance==null) {             instance=new activity2 ();        }         return instance;    }    // Set callback method     CallBackInterface <String> callBacker;     Public void oncallback (Callbackinterface<string> callbacker)  {         this.callbacker=callbacker;    }    //// ////////////////////////////} 


2.3 Activity1 Fragment interface method definition, open Actvity2 interface and set callback method

package com.cyy.test;import android.app.fragment;import android.content.intent;import  android.os.bundle;import android.view.layoutinflater;import android.view.view;import  Android.view.viewgroup;import android.widget.button;import android.widget.toast;public class  MyFragment extends Fragment implements View.OnClickListener{     @Override     public view oncreateview (layoutinflater inflater,  viewgroup container,                              Bundle  Savedinstancestate)  {        view view=inflater.inflate ( R.layout.fragment_my, container, false);        button  button= (Button) View.findviewbyid (R.id.button);Nbsp;       button.setonclicklistener (This);         return view;    }     @Override      public void onclick (VIEW&NBSP;V)  {         switch  (V.getid ()) {            case  r.id.button:                // Click to jump to activity2                 startactivity (New intent (Getactivity (),  activity2.class));                 //interface callback method definition                  callback ();                 break;        }    }     ////////////////////////////////////////////    //Step three      //Interface Definition     public void callback ()  {         activity2.getinstance (). OnCallback (new callbackinterface<string> ()  {              @Override              public void oncallback (String var)  {                 toast.maketext ( Getactivity (),  "successful callback"  + var, toast.length_long). Show ();             }        });     }    /////////////////////////////////////////////} 


3.Demo Code, development tool and version number Android Sutdio 1.2.0.0

This article is from the "CYY" blog, make sure to keep this source http://chenyuanyu.blog.51cto.com/7182607/1789746

Android implements fragment cross activity callback communication

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.