Android tips: implement the bound fragment callback in the activity
When you see the title, you may think about how tall a skill is? In fact, it is generally a custom callback function.
First, we know that there are several ways to transmit data between activities:
First, startActivityForResut () starts an activity. When the activity at the top of the stack calls onActivityResult () and finishes, a message is sent to the parent activity that starts the activity.
Second, when using Fragment, use the setTargetFragment () and onActivityResult () Methods to transmit data between two fragment.
These two methods are helpful for transferring complex data, but for simple data or simply waking up a certain step of operation, it is not necessarily helpful when the child activity or fragment (here the child represents the next activity or fragment started by the parent activity) is completed. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PGJyPgo8L3A + kernel/coprocessor vcD4KPHA + kernel/zwOC8zLPQ19RWNMCp1bm/kernel + PHByZSBjbGFzcz0 = "brush: java;"> 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 ();}}}
In this way, I only need to inherit this abstract class in the main activity and implement the createFragment method to easily create a Fragment and add it to the R. id. container.
@Override public Fragment createFragment() { return new IndexFragment(); }
For Fragment, I added only one TextView and one Button control. When a Button is clicked, I wake up the callback function so that the callback function of the activity can work.
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) {View 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 IndexListener ");}}}
Complete the callback function task 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 the Button in Fragment, Logcat prints the following sentence:
From the Fragment message: Call Back to My Implementer
Okay, the code is over!
This example seems meaningless, but it is very important for some applications. For example, the search function in the file manager, when you type some strings, the user does not have to enter the name of the entire file to be queried to retrieve the results, the addTextChangeListener event of EditText is used to manually add a class of the background Retrieval Method to listen to the incomplete string obtained by the afterTextchange function.
After understanding the listening object and waking up the listening object, you can easily write simple and easy-to-understand code.