I use fragments. when replaced a fragment and it backed onCreateView method is call and data is retrieved from the server. if use if(view==null) for avoid this, that's okay. Is it correct?
public class BlankFragment extends Fragment {
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(view==null){
view= inflater.inflate(R.layout.frag,container,false);
//my code
}
return view;
}
}
Everytime onCreateView
is called it means that there is no any view yet, therfore it is surplus check.
From your comment I suspect that you want to save view reference in eg static context which is something you should not do. Instead, to literally "save state of fragment" you should use provided Bundle savedInstanceState
instance to save whatever needst to be "cached".
You should simply save data in onInstanceSaved
hook - which provides Bundle
as an argument, and restore the state from the bundle provided in onCreateView
hook.
Existence of those bundles in those 2 callbacks are existing because of that solely reason - to save/restore its state if required.
Android docs for developers describes that mechanism in details here https://developer.android.com/guide/fragments/saving-state#savedstate
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.