[木木方文安卓学习笔记四]传值取值

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);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容