0

I am developing an Android application, where I am trying to upload an image from the gallery using an Intent in a fragment called settings fragment. But when I chose an image on the settings fragment and return to my application, the activity state is not saved and shows the home fragment instead of the settings fragment due to that I am not able to save my info. I've also tried the following answers and some more for two days but didn't help me:

1)https://stackoverflow.com/questions/9294603/how-do-i-get-the-currently-displayed-fragment

2)enter link description here

3)enter link description here or maybe I am not understanding them correctly.

Below is the code I've used for saving fragment in the activity state as I am replacing fragment in a fragment container so I am trying to save the state by fragment container id:

   @Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);

    fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
    getSupportFragmentManager().putFragment(outState, "fragment_state", fragment);
}

Then I am retrieving the saved fragment in onActivityCreate Method:

  if (savedInstanceState != null)
    {
  Fragment  fragment = getSupportFragmentManager().getFragment(savedInstanceState,"fragment_state");

        //HomeFragment homeFragment = new HomeFragment();
        FragmentTransaction ftt = getSupportFragmentManager().beginTransaction();
        ftt.replace(R.id.fragment_container, fragment);
        ftt.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ftt.addToBackStack(null);
        ftt.commit();
        Toast.makeText(this, "State is Not Null", Toast.LENGTH_SHORT).show();
    }else
        {
            HomeFragment homeFragment = new HomeFragment();
            FragmentTransaction ftt = getSupportFragmentManager().beginTransaction();
            ftt.replace(R.id.fragment_container, homeFragment);
            ftt.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            ftt.addToBackStack(null);
            ftt.commit();
            Toast.makeText(this, "State is Null", Toast.LENGTH_SHORT).show();
        }

And Inside the settings fragment, I am going to pick the image from the gallery like so, I have also tried the commented code:

    private void theGalleryPickMethod()
{
//        Intent galleryPickIntent = new Intent();
//        galleryPickIntent.setAction(Intent.ACTION_GET_CONTENT);
//        galleryPickIntent.setType("image/*");
//        startActivityForResult(galleryPickIntent, Gallerypick);

    Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(galleryIntent, Gallerypick);

}

  @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        if (requestCode == Gallerypick && resultCode == RESULT_OK
                && null != data) {
            imageUri = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getActivity().getContentResolver().query(imageUri,
                    filePathColumn, null, null, null);
            assert cursor != null;
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String imgDecodableString = cursor.getString(columnIndex);
            cursor.close();
            Drawable d = new BitmapDrawable(getResources(), imgDecodableString);
            settingsUserProfileImage.setImageDrawable(d);
        } else {
            Toast.makeText(getActivity(), "You haven't picked Image",
                    Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(getActivity(), "Something went wrong", Toast.LENGTH_LONG)
                .show();
    }
}

settingsUserProfileImage is my ImageView where I want to show the image after the selection is made, both these method "theGalleryPick" and "onActivityResult" are implemented in the settingsFragment. Now I am not sure where is the problem, am I not saving the fragment in the activity state the right way or my image picking code is not correct. Anyone who can help me with this>>> THANKS IN ADVANCE!

0 Answers0