美文网首页
[木木方文安卓学习笔记四]传值取值

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

作者: 丿沧海一粟丿 | 来源:发表于2017-08-16 14:44 被阅读0次
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);

相关文章

网友评论

      本文标题:[木木方文安卓学习笔记四]传值取值

      本文链接:https://www.haomeiwen.com/subject/gzuirxtx.html