Last active
November 12, 2016 14:07
-
-
Save tassioauad/8ec58dba3fdd8758162b9423d66a7607 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ShowMyObjectFragment extends Fragment { | |
private MyObject myObject; | |
private static final String MYOBJECT_BUNDLE_KEY = "myobject_bundlekey" | |
private static final String MYOBJECTID_BUNDLE_KEY = "myobjectid_bundlekey" | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.fragmentlayout, container, false); | |
if (savedInstanceState != null) { | |
myObject = (MyObject) savedInstanceState.getParcelable(MYOBJECT_BUNDLE_KEY); | |
if (myObject != null) { | |
show(myObject); | |
} | |
} else { | |
Integer myObjectId = getArguments().getParcelable(MYOBJECTID_BUNDLE_KEY); | |
if (myObjectId != null) { | |
myObject = //Search the MyObject with this ID | |
show(myObject); | |
} | |
} | |
return view; | |
} | |
@Override | |
public void onSaveInstanceState(Bundle outState) { | |
if (myObject != null) { | |
outState.putParcelable(MYOBJECT_BUNDLE_KEY, myObject ); | |
} | |
super.onSaveInstanceState(outState); | |
} | |
public static ShowMyObjectFragment newInstance(Integer myObjectId) { | |
ShowMyObjectFragment showMyObjectFragment = new ShowMyObjectFragment (); | |
Bundle bundle = new Bundle(); | |
bundle.putInt(MYOBJECTID_BUNDLE_KEY, myObjectId); | |
showMyObjectFragment.setArguments(bundle); | |
return showMyObjectFragment; | |
} | |
public void show(MyObject myObject) { | |
//... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment