簡體   English   中英

ActivityResult 后恢復片段 state

[英]Restore fragment state after ActivityResult

我正在處理一個顯示圖像列表的片段。 它有一個用於“添加圖像”的按鈕,用於啟動具有這些值的結果意圖。

type: "image/*"
action: Intent.ACTION_GET_CONTENT

問題是:在用戶選擇一張圖片並返回到片段后,列表中的所有其他圖片(存儲在代碼上的某個 ArrayList<> 中)都消失了。

我已經覆蓋了onSaveInstanceState(Bundle)方法並將列表保存在包中。 問題是,沒有辦法恢復它。

我想過覆蓋onViewStateRestored(Bundle)但它沒有用。 當我在所有“onXXX”方法上放一些Log.d()時,我發現每次添加文件時只執行這三個(實際順序):

onPause()
onSaveInstanceState(Bundle)
//now the image picker opens up
//user picks the image
onResume()
//image picker closes and fragment is now on screen

我想在onResume()中使用一些“getXXX”方法,但我找不到有用的方法。 我能做些什么?

編輯:這是我的代碼(沒有無關的東西)。

@Override public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    Log.d("debugaff", "oncreate");
    setRetainInstance(true);
    this.attachments = (ArrayList<Attachment>) getActivity().getIntent().getSerializableExtra(ExtraKeys.ATTACHMENTS);
}

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    Log.d("debugaff", "oncreateview");
    rootView = inflater.inflate(R.layout.fragment_attachments_form, container, false);

    //....

    if(savedInstanceState == null){
        getLoaderManager().initLoader(0, null, this);
    } else {
        attachmentsRvAdapter.setItems((List<Attachment>) savedInstanceState.getSerializable(ExtraKeys.TEMP_ATTACHMENTS));
    }

    //....

    return rootView;
}

@Override public void onResume(){
    super.onResume();
    Log.d("debugaff", "onresume");
    hideKeyboard();
}

@Override public void onPause(){
    super.onPause();
    setRetainInstance(true); //called this to make it 100% redundant (already called it at onCreate)
    Log.d("debugaff", "onpause");
}

@Override public void onViewStateRestored(@Nullable Bundle savedInstanceState){
    Log.d("debugaff", "onviewstaterestored");
    if(savedInstanceState == null){
        getLoaderManager().initLoader(0, null, this);
    } else {
        attachments.addAll((List<Attachment>) savedInstanceState.getSerializable(ExtraKeys.TEMP_ATTACHMENTS));
        attachmentsRvAdapter.setItems(attachments);
    }

    super.onViewStateRestored(savedInstanceState);
}

我剛剛模仿了您的情況,沒有發現錯誤(列表將新值與列表中的舊值一起保存)。 我能想到的唯一問題是您沒有正確初始化列表。 我所做的是在 onCreate() 方法中初始化列表,當我選擇另一個圖像時,它將該圖像保存在同一個列表中,而前一個圖像仍在列表中。 無需將列表單獨保存在 savedInstanceBundle 中。 這是我的片段代碼:

public class SelectListFragment extends Fragment {


private static final int PICK_IMAGE_REQUEST = 11;

public SelectListFragment() {
    // Required empty public constructor
}

List listOfImagesSelected;
Button selectButton;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    listOfImagesSelected=new ArrayList<Bitmap>();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_select_list, container, false);

    selectButton = view.findViewById(R.id.selectButton);
    selectButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
        }
    });
    return view;
}


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


    //I am retrieving the Bitmap from the intent and saving into the list 
    if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
        Uri uri = data.getData();

        try {
            //making the bitmap from the link of the file selected
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
            listOfImagesSelected.add(bitmap);
        }catch (Exception e){

        }

    }
}
}

如果需要,我可以分享我的項目的完整工作代碼。 為了更好地理解片段生命周期,請查看此圖片

要使用onViewStateRestored ,您需要調用 'setRetainInstance(true);'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM