savedInstanceState serves another purpose.
onSaveInstanceState (Bundle) : This method is called before the action can be killed, so that when it returns some time in the future, it can restore its state
And in your particular case, this may not even be required. When you click the button, you change the fragments, and do not restart the application.
From what I see, you allow the user to create a postcard: an image on one side (say, side A) and a flip message (say, side B). When the application starts, side A. opens. In some ways, you allow the user to select an image from the gallery. I assume that onActivityResult(int, int, Intent) works as expected and sets the image to ImageView - R.id.imageView . When the button is pressed, the view changes to side B. And when the button is pressed again, the view changes to side A, but the image selected by the user is missing.
One thing you can do inside onActivityResult(int, int, Intent) : save the image path in SharedPreferences.
SharedPreferences preferences; final String PREFS = "your.application.name.prefs"; // Keyword to find the path final String IMAGE_SELECTED_BY_USER = "image_selected_by_user"; // Use a default image when the user starts the app for the first time // or if the retrieved path points to a deleted image etc. final String PATH_TO_A_DEFAULT_IMAGE = "path_to_a_default_image" @Override protected void onCreate(Bundle savedInstanceState) { .... .... preferences = getActivity().getSharedPreferences(PREFS, 0); imagePath = preferences.getString(IMAGE_SELECTED_BY_USER, PATH_TO_A_DEFAULT_IMAGE); frontImageView = (ImageView) findViewById(R.id.imageView); Drawable drawable = null; if (new File(imagePath).exists()) { drawable = Drawable.createFromPath(imagePath); } else { drawable = Drawable.createFromPath(PATH_TO_A_DEFAULT_IMAGE); } frontImageView.setImageDrawable(drawable); getFragmentManager() .beginTransaction() .add(R.id.postcardFrame, new CardFrontFragment()) .commit(); .... .... }
In onActivityResult(int, int, Intent) save the image path:
if(file.exists()){ Log.d(tag, "File Exists"); Drawable d = Drawable.createFromPath(newPath); setImage.setImageDrawable(d); Editor editor = preferences.edit(); editor.putString(IMAGE_SELECTED_BY_USER, newPath); editor.commit(); } else{ try{ Log.d(tag,"File didnt exist"); FileOutputStream out = new FileOutputStream(file); frontImage.compress(Bitmap.CompressFormat.PNG, 90, out); if (file.exists()) { Log.d(tag, "file exists now"); newPath = file.getAbsolutePath(); Drawable b = Drawable.createFromPath(newPath); setImage.setImageDrawable(b); Editor editor = preferences.edit(); editor.putString(IMAGE_SELECTED_BY_USER, newPath); editor.commit(); } } catch (Exception e) { e.printStackTrace(); } }
Thus, when the user launches the application, he will see either the default image or the previously selected image.
Where savedInstanceState will be helpful . Let's say you give the user the opportunity to write a short message on side B. Now, if while writing the message the user turns the device from the Landscape to the portrait (or vice versa), the message he wrote will disappear because the activity will be destroyed and recreated. To save the message, you should use onSaveInstanceState(Bundle) :
@Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("Text_To_Save", someEditText.getText().toString()); }
On rotation, onCreate(Bundle)' will be called. The bundle passed is the same one from activity onCreate(Bundle)' will be called. The bundle passed is the same one from onCreate(Bundle)' will be called. The bundle passed is the same one from onSaveInstanceState (Bundle) `. To get the text:
String savedString = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState != null) { if (savedInstanceState.contains("Text_To_Save")) { savedString = savedInstanceState.getString("Text_To_Save"); } } someEditText.setText(savedString); }