Fragment静态传值

来源:互联网 发布:万圣节服装淘宝网 编辑:程序博客网 时间:2024/06/08 12:19
public class ContentFragment extends Fragment {    private View view;    private static final String KEY = "arg";    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        view = inflater.inflate(R.layout.content_fragment, container, false);        return view;    }    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        TextView textView = (TextView) view.findViewById(R.id.text_content);        String str = (String) getArguments().get(KEY);        textView.setText(str);    }    /**     * Fragment静态传值     * @param str     * @return     */    public static ContentFragment newInstance(String str) {        ContentFragment fragment = new ContentFragment();        Bundle bundle = new Bundle();        bundle.putString(KEY,str);        fragment.setArguments(bundle);        return fragment;    }}
原创粉丝点击