如果您愿意,您可以使用BroadcastReciver
从片段向您的活动发送事件通知。
要注册广播接收器,请FragmentActivity
执行以下操作:
public class MyActivity extends FragmentActivity {
private BroadcastReceiver myBroadcastReceiver =
new BroadcastReceiver() {
@Override
public void onReceive(...) {
//YOU WILL RECEIVE YOUR BROADCAST HERE. WRITE YOUR CODE HERE TO ADD NEW TAB
}
});
...
public void onResume() {
super.onResume();
....
registerReceiver(myBroadcastReceiver, intentFilter);
}
public void onPause() {
super.onPause();
...
unregisterReceiver(myBroadcastReceiver);
}
...
}
现在要从您的片段发送广播,请执行以下操作:
Intent intent=new Intent();
intent.setAction("ANY_UNIQUE_NAME");
intent.putExtra("data",EXTRA_DATA_IF_YOU_WANT);
sendBroadcast(intent);
您将在活动的接收事件中收到此广播。在那里做任何你想做的事。