Create one interface in your Activity and pass your data via the interface to the fragment. Implement that interface in your fragment to get data.
For example
MainActivity.class
public class MainActivity extends AppCompatActivity {
DataFromActivityToFragment dataFromActivityToFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentA fr = new FragmentA();
FragmentManager fm = getFragmentManager();
dataFromActivityToFragment = (DataFromActivityToFragment) fr;
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
final Handler handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
dataFromActivityToFragment.sendData("Hi");
}
};
handler.postDelayed(r, 5000);
}
public interface DataFromActivityToFragment {
void sendData(String data);
}
}
FragmentA.class
public class FragmentA extends Fragment implements MainActivity.DataFromActivityToFragment {
TextView text;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.content_main, null);
text = (TextView) rootView.findViewById(R.id.fragment_text);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void sendData(String data) {
if(data != null)
text.setText(data);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
content_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/fragment_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
In above example I have taken Runnable just to send data with delay of 5 seconds after creation of fragment.