简体   繁体   中英

How to make image stays in imageview when fragment is changed using viewpager?

I have an activity that's using viewpager to manage 4 pages. The user can go to the next or previous page by clicking the next and back button in action bar. I have an imageview in fragment4. The problem is when i add an image in fragment4 and when i click back button it will bring me to fragment3. And after that when i click next button it will bring me to fragment4 again and the image is still available. But why when i click back and back button and it will bring me to fragment2 then i click next and next button; in the fragment4 the image is no longer available? How i can make the image to be available as long as the activity is not destroyed? Sorry for the poor english and description. I hope i don't confuse you and hopefully you can share some thoughts and solutions on this. Thank you.

在此处输入图片说明

Fragment4 xml:

<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:scaleType="fitXY"
    android:layout_centerInParent="true"
    android:visibility="visible"
    android:background="@drawable/ic_control_point_black_24dp"
    android:layout_marginBottom="10dp"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/tap_add_replace_photo"
    android:textAlignment="center"/>

Fragment4 java:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    setHasOptionsMenu(true);

    View view = inflater.inflate(R.layout.fragment_create_activity_page_four, container, false);

    imageViewPhoto = view.findViewById(R.id.imageView);

    imageViewPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            choosePhoto();
        }
    });

    currentUser = FirebaseAuth.getInstance().getCurrentUser();
    activityId = ref.getId();

    return view;
}

public void choosePhoto(){
    try{

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);

    }catch (Exception e){
        Toast.makeText(getActivity(), e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK && data != null &&
            data.getData() != null){
        try
        {
            uri = data.getData();
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
            imageViewPhoto.setImageBitmap(bitmap);
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    //hide soft keyboard
    InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);

    int id = item.getItemId();

    if (id == android.R.id.home) {
        ((ActivityCreateActivity)getActivity()).getViewPager().setCurrentItem(2);
        return true;
    }else if (id == R.id.action_publish) {
        getValue();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

You should be set mViewPager.setOffscreenPageLimit(3); in your viewpager

Fragments view will be destroyed when it's scrolled too far, you must store the state in a custom field, and re-use that value in onCreateView .

If you want to see how it works exactly, add debug logs to onCreateView and onDestroyView .

But if you don't have too many pages, increasing offscreen limit might be enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM