public class fristFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//获取数据的方法
Bundle bundle = getArguments();
String title = bundle.getString("title");
View view = inflater.inflate(R.layout.fragment_frist, null);
TextView textView = (TextView) view.findViewById(R.id.textView);
textView.setText(title);
return view;
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fristFragment frist = new fristFragment();
//获取到低版本兼容的
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.content, frist, "frist");
transaction.commit();
//对fragment进行任何操作都必须提交
//fragment传值
Bundle bundle = new Bundle();
bundle.putString("title", "香菇蓝瘦");
frist.setArguments(bundle);
//3.0 above
// getFragmentManager();
}
}
//activity跳转
// Intent intent = new Intent(MainActivity.this, HomeActivity.class);
// intent.putExtra("home", "woshihome");
// startActivityForResult(intent, 1500);
网友评论