Fragment somersaults and losses Image

I completely rewrote the question to highlight the area.

I have two fragments that flip like a map (left, right). When the front fragment disappears, it shows the back. After pressing the button again, it again turns to the foreground, but the ImageView in the foreground is missing.

I tried various methods of saving the data of the selected image.

  • Saving fragment onSaveInstanceState

This gives me a Null Pointer, so I decided that I needed something more permanent after the creation.

  • So, now I save the image to the SDCard after selecting it

This, I believe, will work and just check the path and capture it if it is flipped to the front or activity is recreated.

Here is the code

OnCreate ():

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.new_postcard_activity); //UI call frontImageView = (ImageView) findViewById(R.id.imageView); Log.d(tag, "onCreate() Instance:" + savedInstanceState); //fragment start if (savedInstanceState == null) { Log.d(tag,"Instance Null"); getFragmentManager() .beginTransaction() .add(R.id.postcardFrame, new CardFrontFragment()) .commit(); if(!mShowingBack){ Log.d(tag,"Not showing back"); if(newPath != null && newPath != ""){ Log.d(tag, "entered new path, not empty"); Drawable drawable = Drawable.createFromPath(newPath); Log.d(tag, "Should be setting saved image."); frontImageView.setImageDrawable(drawable); } } } else { mShowingBack = (getFragmentManager().getBackStackEntryCount() > 0); Log.d(tag, "Instance is not Null"); } 

Flip Button Click Listener

 //flip card final Button cardBackButton = (Button) findViewById(R.id.cardBackButton); cardBackButton.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { flipCard(); }); 

FlipCard Method:

 private void flipCard() { Log.d(tag2, "Log after flipCard:" + mShowingBack); if(mShowingBack) { //Flip to front flipFront(); return; } // Flip to back flipBack(); } 

I set Image onActivityResult from my PhotoGallery

 protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); if (resultCode == RESULT_OK) { Uri photoUri = intent.getData(); if (photoUri != null) { try { ImageView setImage = (ImageView)findViewById(R.id.imageView); frontImage = MediaStore.Images.Media.getBitmap(this .getContentResolver(), photoUri); imageSet = true; //save image to SD if(imageSet == true){ Log.d(tag, "Inside Image Set if Statement"); String path = getExternalCacheDir() + "Postcards.png"; if(path != null && path != ""){ Log.d(tag, "Path is:" + path); File file = new File(path); newPath = file.getAbsolutePath(); Log.d(tag, "New Path:" + newPath); if(file.exists()){ Log.d(tag, "File Exists"); Drawable d = Drawable.createFromPath(newPath); setImage.setImageDrawable(d); }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); } }catch(Exception e){ e.printStackTrace(); } } } } } catch (Exception e) { e.printStackTrace(); } 

This is how I access the image and try to install it in my ImageView on reboot ()

 if(imageSet == true){ if(newPath != null && newPath != ""){ ImageView view = (ImageView) findViewById(R.id.imageView); Drawable drawable = Drawable.createFromPath(newPath); view.setImageDrawable(drawable); } } 

This seems like the best way to get the image and setup, but it will not work. What would be the best practice and how can I get it to do what I need?

Thanks so much for any help!

+6
source share
1 answer

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); } 
+2
source

Source: https://habr.com/ru/post/951365/


All Articles