Code example for Fragment

Methods: onCreate, setRetainInstance

0
	return new ParentFragment(); 
    } 
 
    @Override 
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setRetainInstance(true);
    } 
 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	return inflater.inflate(R.layout.fragment_parent, container, false);
    } 
 
    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) {
	super.onViewCreated(view, savedInstanceState);
 
	ViewPager mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
	mViewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
    }