Fragment中的getActivity()為null的問(wèn)題
向Fragment傳遞參數(shù)
1. 向Fragment傳遞參數(shù)第一步在AFragment中
java 代碼:AFragment.class
public class AFragment extends Fragment {
private TextView mTvTitle;
/**
* 調(diào)用這個(gè)方法陨舱,可以的到一個(gè)已經(jīng)傳好參數(shù)的fragment
* @param title
* @return
*/
public static AFragment newInstance(String title){
AFragment fragment=new AFragment();
Bundle bundle=new Bundle();
bundle.putString("title",title);
fragment.setArguments(bundle);
//即使fragment重構(gòu),也會(huì)通過(guò)反射的機(jī)制苛吱,把重新實(shí)例化的fragment設(shè)上參數(shù)title
return fragment;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//相當(dāng)于activity的onCreate里的setcontentView,給它一個(gè)布局文件返回一個(gè)視圖文件
View view=inflater.inflate(R.layout.fragment_a,container,false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
//當(dāng)View創(chuàng)建完成后虫啥,這里有一個(gè)回調(diào)方法
super.onViewCreated(view, savedInstanceState);
mTvTitle=(TextView)view.findViewById(R.id.tv_title);
if(getArguments()!=null){
//獲取參數(shù)
mTvTitle.setText(getArguments().getString("title"));
}
}
@Override
public void onAttach(@NonNull Context context) {
//當(dāng)Fragment與Activity發(fā)生關(guān)聯(lián)時(shí)調(diào)用
super.onAttach(context);
}
@Override
public void onDetach() {
//與onAttach相對(duì)應(yīng)镊逝,當(dāng)Fragment與Activity關(guān)聯(lián)被取消時(shí)調(diào)用
//當(dāng)調(diào)用這個(gè)方法后触创,會(huì)出現(xiàn)getactivity()為空
super.onDetach();
}
@Override
public void onDestroy() {
super.onDestroy();
//取消異步
}
}
關(guān)鍵代碼1
1. 設(shè)一個(gè)靜態(tài)的構(gòu)造方法 AFragment newInstance(String title),返回一個(gè)已經(jīng)傳好參數(shù)的fragment
2. 通過(guò)Bundle傳參數(shù)
3. 調(diào)用setArguments()
public static AFragment newInstance(String title){
AFragment fragment=new AFragment();
Bundle bundle=new Bundle();
bundle.putString("title",title);
fragment.setArguments(bundle);
//即使fragment重構(gòu)薪丁,也會(huì)通過(guò)反射的機(jī)制,把重新實(shí)例化的fragment設(shè)上參數(shù)title
return fragment;
}
關(guān)鍵代碼 2
1. 在id為tv_title的textview控件中獲取參數(shù)灭衷,即等會(huì)會(huì)在這個(gè)控件里顯示傳入的字符串title
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
//當(dāng)View創(chuàng)建完成后次慢,這里有一個(gè)回調(diào)方法
super.onViewCreated(view, savedInstanceState);
mTvTitle=(TextView)view.findViewById(R.id.tv_title);
if(getArguments()!=null){
//獲取參數(shù)
mTvTitle.setText(getArguments().getString("title"));
}
}
java 代碼:ContainerActivity.class
public class ContainerActivity extends AppCompatActivity {
private AFragment aFragment;
private BFragment bFragment;
private Button btn_change;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_container);
btn_change = (Button) findViewById(R.id.btn_change);
btn_change.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(bFragment == null){
bFragment=new BFragment();
}
getSupportFragmentManager().beginTransaction().replace(R.id.fl_container,bFragment).commitAllowingStateLoss();
}
});
//R.id.fl_container:裝在fragment的容器
//aFragment:要加載的fragment
// aFragment=new AFragment();
//調(diào)用Fragment的一個(gè)靜態(tài)方法newInstance
aFragment=AFragment.newInstance("我是參數(shù)");
//把AFragment添加到Activity中,記得調(diào)用commit
getSupportFragmentManager().beginTransaction().add(R.id.fl_container,aFragment).commitAllowingStateLoss();
}
2. 在Fragment中傳遞參數(shù)第二步:在ContainerActivity中調(diào)用AFragment的靜態(tài)方法newInstance(),寫入?yún)?shù)
關(guān)鍵代碼
//傳參數(shù)的時(shí)候:
aFragment=AFragment.newInstance("我是參數(shù)");
//不傳參數(shù)的時(shí)候
aFragment=new AFragment();