I am trying to create an application where I can let the user select an image to display in their profile. I can view and set their selected image in image view mode. But the image is lost after the destruction of activity. I tried to implement onSaveInstanceState, but still it's the same. I am wondering if I am using it correctly. Hope you can help newbies like me. Thanks in advance. Here is the code I'm using:
public class AccountFragment extends Fragment implements OnClickListener { private LoginDataBaseAdapter loginDataBaseAdapter; Bitmap image; Bitmap bitmap; String picture_location; TextView textTargetUri; ImageView targetImage; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_account, container, false); textTargetUri = (TextView) rootView.findViewById(R.id.targeturi); targetImage=(ImageView) rootView.findViewById(R.id.profpic); targetImage.setOnClickListener(new ImageView.OnClickListener(){ @Override public void onClick(View arg0) { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, 0); }}); if (savedInstanceState != null) {
By the way, you may have noticed that instead of using onRestoreInstanceState after oncreate, I tried to use a different approach. I found the answer from another question that you can also implement in oncreate. I have used it since whenever I declare an onRestoreInstanceState function, I am asked to remove the @Override annotation.
@Override protected void onRestoreInstanceState(Bundle savedInstanceState){ image = savedInstanceState.getParcelable("BitmapImage"); targetImage.setImageBitmap(image); textTargetUri.setText(savedInstanceState.getString("path_to_picture")); }
android android-fragments imageview
DEADPOOL
source share