Mutual communication between Android fragment and activity
Objective
Since the introduction of Android3.0, the main purpose of the fragment is to support dynamic and flexible interface design and screen adaptation problems. Fragmenty cannot exist alone and must rely on activity as part of a view display, a colleague who has its own lifecycle, receives its own events, has more flexible features, and today fragment has been widely used in app development, The most common is the single activity multiple fragment mode. Fragment relies on activity and is unavoidable to interact with the activity, and here is a simple introduction to three common ways of interacting with each other.
One, interface mode
Define an interface in the fragment class and implement it in the activity. Fragment the object that gets the concrete implementation of the interface in the Onattach () callback function. Later, fragment can invoke the methods in the interface to communicate with the activity.
After Android5.0, the method of Onattach (activity activity) is abolished, and the Onattach (context) is used, which means that the parameters passed are changed
public interface showmsglistener{
void showmsg (String str);
}
Private Showmsglistener Msglistener;
@Override public
void Onattach [activity activity] {
Super.onattach (activity);
try {
if (activity!=null) {
msglistener= (mainactivity) activity;
}
catch (ClassCastException e) {
throw new ClassCastException (activity.tostring ()
+ "must implement Showmsglistener");
}
Mbutton.setonclicklistener (New View.onclicklistener () {
@Override public
void OnClick (view view) {
Msglistener.showmsg ("Hello android!" ( interface);
This interface needs to be implemented in the activity
@Override public
void ShowMsg (String str) {
toast.maketext (mainactivity.this, str, toast.length_short). Show ();
}
Second, broadcast
Registering a broadcast receiver in an activity, sending a broadcast in fragment
Private Broadcastreceiver Mbroadcastreceiver = new Broadcastreceiver () {
@Override public
void OnReceive ( Context context, Intent Intent) {
String action = intent.getaction ();
if (Action.equals (Action_name)) {
String msg = Intent.getstringextra ("msg");
Toast.maketext (Mainactivity.this, MSG, Toast.length_short). Show ();}}
;
public void Registerboradcastreceiver () {
Intentfilter myintentfilter = new Intentfilter ();
Myintentfilter.addaction (action_name);
Registerreceiver (Mbroadcastreceiver, myintentfilter);
}
@Override
protected void OnDestroy () {
Super.ondestroy ();
Unregisterreceiver (Mbroadcastreceiver);
}
Third, Fragment from the activity to obtain data
Set the parameters in the getinstance () method of the Fragment () setarguments ()
public static Fragment getinstance (String msg) {
Bundle Bundle = new Bundle ();
Bundle.putstring ("msg", msg);
Mainfragment fragment=new mainfragment ();
Fragment.setarguments (bundle);
return fragment;
}
The Getarguments () method is then used to obtain
public void Initdatas () {
Bundle bundle=getarguments ();
if (bundle!=null) {
String msg=bundle.getstring ("msg");
Toast.maketext (Getactivity (), MSG, Toast.length_short). Show ();
}
Thank you for reading, I hope to help you, thank you for your support for this site!