MainActivity.java
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Fragment fragment=new PageFragment(); Bundle bundle=new Bundle(); bundle.putInt("pageNumber",10); fragment.setArguments(bundle); FragmentManager fragmentManager=getSupportFragmentManager(); FragmentTransaction transaction= fragmentManager.beginTransaction(); transaction.add(R.id.relativeLayoutFragmentContainer,fragment); transaction.commit(); } }
PageFragment.java
public class PageFragment extends Fragment { public PageFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_page, container, false); TextView textViewPage= (TextView) view.findViewById(R.id.textViewPage); Bundle bundle=getArguments(); int pageNumber= bundle.getInt("pageNumber"); String output=String.format("Page %d",pageNumber); textViewPage.setText(output); return view; } }
References
https://github.com/mhdr/AndroidSamples/tree/master/044