鸿洋博客:http://blog.csdn.net/lmj623565791/article/details/42628537,整理下一些关键点

public class ContentFragment extends Fragment
{ private String mArgument;
public static final String ARGUMENT = "argument"; @Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// mArgument = getActivity().getIntent().getStringExtra(ARGUMENT);
Bundle bundle = getArguments();
if (bundle != null)
mArgument = bundle.getString(ARGUMENT); }
/**
* 传入需要的参数,设置给arguments
* @param argument
* @return
*/
public static ContentFragment newInstance(String argument)
{
Bundle bundle = new Bundle();
bundle.putString(ARGUMENT, argument);
ContentFragment contentFragment = new ContentFragment();
contentFragment.setArguments(bundle);
return contentFragment;
}

通过在activity里面调用newInstance来将activity的内容传递给fragment,实现activity和fragment之间的解耦。

项目中也可以通过eventbus来实现activity和fragment直接的消息互通。

05-02 05:44