What do you think is a great skill to see the headline? In fact, it is a custom callback function.
First we know that there are several ways to transfer data between activity:
One is Startactivityforresut () initiates an activity, and when the top activity of the stack calls Onactivityresult () and the finish is dropped, the message is passed to the parent activity that initiated the activity.
The second is to use the settargetfragment () and Onactivityresult () methods to achieve data transfer between the two fragment when using fragment.
Both of these methods are useful for manipulating complex data, but for simple data or just waking up a step, and not necessarily in the child activity or fragment (here to the next activity or fragment that the child represents the parent activity) It is helpful to do this when the finish is off.
Well, the vernacular has so many irrelevant, had to go to sleep before writing two lines of code affixed to let everyone feel:
I first created an abstract class that inherits the fragmentactivity from the V4 extension library to manage the creation of each fragment
Package Com.example.icedcap.fragmentcallbackdemo;import Android.os.bundle;import android.support.v4.app.Fragment; Import Android.support.v4.app.fragmentactivity;import android.support.v4.app.fragmentmanager;/** * Created by Icedcap on 14-11-18. */public abstract class Singlefragment extends Fragmentactivity {public abstract Fragment createfragment (); @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_my); Fragmentmanager fm = Getsupportfragmentmanager (); Fragment mfragment = Fm.findfragmentbyid (R.id.container); if (mfragment = = null) { mfragment = createfragment (); Fm.begintransaction (). Add (R.id.container, mfragment). commit ();}}}
So I simply inherit the abstract class in the main activity and implement the Createfragment method to easily create a fragment and add it to the R.id.container container.
@Override public Fragment createfragment () { return new indexfragment (); }
For fragment it's easy. I only add a textview and a button control, and when you click on the button, the callback function is awakened to work with the activity's callback function.
Package Com.example.icedcap.fragmentcallbackdemo;import Android.app.activity;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import android.widget.button;/** * Created by Icedcap on 14-11-18. */public class Indexfragment extends Fragment {private Indexlistener mlistener; @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {Vi EW v = inflater.inflate (R.layout.fragment_index, container, false); Button button = (button) V.findviewbyid (R.id.index_button); Button.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) { Mlistener.onindexlistener ("Call Back to My implementer"); } }); return v; } public interface indexlistener{public void Onindexlistener (String str); }//Initialize Mlistener @OverrIDE public void Onattach (activity activity) {Super.onattach (activity); try {Mlistener = (indexlistener) activity; }catch (ClassCastException e) {throw new ClassCastException (activity.tostring () + "must implement Indexlistene R "); } }}
The task of completing the callback function in the main activity
Package Com.example.icedcap.fragmentcallbackdemo;import Android.support.v4.app.fragment;import Android.util.Log; public class MyActivity extends Singlefragment implements indexfragment.indexlistener{ private static final String TAG = "MyActivity"; @Override public Fragment createfragment () { return new indexfragment (); } @Override public void Onindexlistener (String str) { log.d (TAG, "from the Fragment message:" + str);} }
When you click on the button in fragment, Logcat will print this sentence:
From the Fragment Message:call back to My implementer
Okay, the code's over!
This example may seem meaningless, but it is important for some applications, such as searching for features in the File Manager, and returning results as soon as you type some strings. The user does not have to enter the entire file name to be queried to retrieve the results. It is the class that takes advantage of EditText's Addtextchangelistener event and manually adds a background retrieval method to listen to the Aftertextchange function to get to the end of the torn string.
Well, it's easy to write the introductory code after you figure out the two classes of listener objects and wake-up listeners.
Android Tips: Implementing a callback with bound fragment in activity