我正在处理一个显示图像列表的片段。它具有用于“添加图像”的按钮,用于使用这些值启动结果的意图。
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);
}
答案 0 :(得分:0)
我只是模仿您的情况,没有发现错误(列表将新值与旧值保存在列表中)。我能想到的唯一问题是您没有正确初始化列表。我所做的是在onCreate()方法中初始化了列表,当我选择另一个Image时,它将该图像保存在同一列表中,而先前的图像仍在列表中。 无需将列表单独保存在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){
}
}
}
}
如果需要,我可以共享我的项目的完整工作代码。为了更好地了解Fragment LifeCycle,请查看此图片
答案 1 :(得分:-1)
要使用onViewStateRestored
,您需要调用“ setRetainInstance(true);”