将Circleimageview的状态保存在片段内

时间:2019-07-11 09:33:06

标签: android imageview onresume onsaveinstancestate

我有一些带有circleimageview的片段,用作个人资料图像,以后可以正确地保存在数据库中。 我的问题是使用onSaveInstanceState和onPause / onStop保存imageview的状态,以便在将它们保存到数据库之前以及例如执行屏幕旋转之后保持其状态。 这是我的代码

public class MyFragment extends Fragment implements View.OnClickListener {
    private CircleImageView mPhoto;
    private byte[] bytes = null;
    private Bitmap photo = null;

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        if(bytes!=null) {
            outState.putByteArray("bytes", bytes);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        LayoutInflater lf = getActivity().getLayoutInflater();
        view =  lf.inflate(R.layout.my_fragment,container,false);
        mPhoto = (CircleImageView) view.findViewById(R.id.photo);

        view.findViewById(R.id.photo).setOnClickListener(this);

        if(savedInstanceState!=null){
            if(savedInstanceState.containsKey("bytes")){
                bytes = savedInstanceState.getByteArray("bytes");
                if(bytes != null) {
                    //but with a screen rotation the image is not restored
                    photo = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                    mPhoto.setImageBitmap(photo);
                }
            }
        }
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.photo:
                Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                try {
                    startActivityForResult(Intent.createChooser(i, "Select Picture"), 0);
                }catch (ActivityNotFoundException ex){
                    ex.printStackTrace();
                }
                break;
        }

    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==0 && resultCode == Activity.RESULT_OK) {
            Uri path = data.getData();
            Bitmap tmp = null;
            try {
                tmp = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), path);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(tmp != null) {
                mPhoto.setImageBitmap(photo);
                bytes = getBitmapAsByteArray(photo);
                //tested, bytes is not null and the photo are restored correctly when saved on the database 
            }

        }
    }
    @Override
    public void onResume() {
        super.onResume();
        bytes = getActivity().getIntent().getByteArrayExtra("bytes");
        if(bytes != null) {
            //here bytes is null
            photo = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
            mPhoto.setImageBitmap(photo);
        }

    }


    @Override
    public void onStop() {
        super.onStop();
        if(bytes!=null)
            getActivity().getIntent().putExtra("bytes",bytes);
    }
}

以这种方式无法在屏幕旋转期间维护图像,如何正确实现onsavedInstanceState,onStop和onResume?

2 个答案:

答案 0 :(得分:0)

最简单的方法将ViewModel与片段一起使用。

引用:https://developer.android.com/topic/libraries/architecture/viewmodel.html

更新

第一步:在build.gradle中添加View Model dependency

this.dbConnectionService.getAllData().subscribe(console.log);

第2步:为片段创建ViewModel

def lifecycle_version = "2.0.0"
implementation "androidx.lifecycle:lifecycle viewmodel:$lifecycle_version" 

第3步:在MyFragment中设置ViewModel

public class MyFragmentViewModel extends ViewModel {
}

答案 1 :(得分:0)

将代码放在super()方法之前。

 @Override
public void onStop() {
    if(bytes!=null)
    getActivity().getIntent().putExtra("bytes",bytes);
    super.onStop();

}