Sometimes, you have to call a method of a fragment from its parent activity. For example, you have a fragment that displays a list of SMS. When there is a new incoming SMS, the SMS receiver in your activity will call a method of the fragment to upload the SMS list. Calling the method of the fragment from its parent activity is simple. You need to find the fragment by its tag name using the findFragmentByTagName of FragmentManager class. Then call the method of the fragment to do something that you wish.
In the example application below, when a new message arrives, the broadcast receiver (SMSReceiver) defined and registered in the MainActivity will call the showLastSMS() method of the LastSMSFragment to display the last sms.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/txtlastsms"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:singleLine="false"
/>
</LinearLayout>
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.TextView;
public class LastSMSFragment extends Fragment {
private TextView tv;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragmentlayout, container, false);
tv=(TextView)view.findViewById(R.id.txtlastsms);
return view;
}
public static LastSMSFragment newInstance() {
return (new LastSMSFragment());
}
public void showLastSMS(String mess){
tv.setText(mess);
}
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private SMSReceiver smsReceiver;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
smsReceiver = new SMSReceiver();
// Register receiver to receive the last sms
IntentFilter intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
intentFilter.setPriority(999);
registerReceiver(smsReceiver, intentFilter);
// Display fragment
showFragment();
// Sending sms for testing
sendSms("089256724", "This is the last sms.");
}
private class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] smsm = null;
String sms_str = "";
if (bundle != null) {
// Get the SMS message
Object[] pdus = (Object[]) bundle.get("pdus");
smsm = new SmsMessage[pdus.length];
for (int i = 0; i < smsm.length; i++) {
smsm[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
sms_str += "Sent From: " + smsm[i].getOriginatingAddress();
sms_str += "\r\nMessage: ";
String message=smsm[i].getMessageBody().toString();
sms_str +=smsm[i].getMessageBody().toString();
sms_str += "\r\n";
}
LastSMSFragment lastSMSFragment = (LastSMSFragment)getSupportFragmentManager().findFragmentByTag("lastSMSFragment");
if(lastSMSFragment==null) // if the fragment is not showing, show it
showFragment();
// Call fragment method to display sms in the textview
lastSMSFragment.showLastSMS(sms_str);
}
}
}
public void showFragment(){
LastSMSFragment lastSMSFragment=LastSMSFragment.newInstance();
FragmentTransaction transact=getSupportFragmentManager().beginTransaction();
transact.add(R.id.fragment_container, lastSMSFragment, "lastSMSFragment");
transact.commit();
}
public void sendSms(String phoneNo, String sms){
SmsManager smsMgt= SmsManager.getDefault();
smsMgt.sendTextMessage(phoneNo, null, sms, null, null);
Toast.makeText(this,"The message was sent.", Toast.LENGTH_SHORT).show();
}
public void onPause(){
super.onPause();
try {
// Unregister the receiver
unregisterReceiver(this.smsReceiver);
}catch(Exception e){}
}
}
<uses-permission android:name= "android.permission.SEND_SMS" />
<uses-permission android:name= "android.permission.RECEIVE_SMS" />
|
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.