简体   繁体   English

拯救碎片状态

[英]Saving State of Fragment

I have an Activity(Form) with 5 fragments. 我有一个活动(表格)有5个片段。 In my 3rd fragment i have a listview and an image button to get the images from my phone's gallery and put it in the listview. 在我的第3个片段中,我有一个列表视图和一个图像按钮,用于从手机的图库中获取图像并将其放入列表视图中。 I have no problem with that. 对于那件事我没有任何疑问。

Also, the images attached is staying there in the 3rd fragments listview when I swipe to my 2nd and 4th fragment and go back to my 3rd fragment. 此外,当我滑动到我的第2和第4个片段并返回到我的第3个片段时,附加的图像停留在第3个片段列表视图中。

But when i swipe to 1st and 5th fragment and go back to my 3rd fragment, the image attach on my 3rd fragment listview is gone. 但是,当我滑动到第1和第5个片段并返回到我的第3个片段时,我的第3个片段列表视图上的图像附加消失了。 What could be the problem? 可能是什么问题呢? Here's my code: 这是我的代码:

Fragment3: Fragment3:

public class Fragment3 extends Fragment implements OnClickListener {

    ListView lv;
    ArrayList<Uri> array_list = new ArrayList<Uri>();
    ArrayAdapter<Uri> array_adapter;

    final int RQS_LOADIMAGE = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.frag3, container, false);

        ImageButton b = (ImageButton) v.findViewById(R.id.button_ai);
        b.setOnClickListener(this);

        array_adapter = new ArrayAdapter<Uri>(getActivity(), android.R.layout.simple_list_item_1, array_list);
        lv = (ListView) v.findViewById(R.id.list_ai);
        lv.setAdapter(array_adapter);

        return v;
    }

    @Override
    public void onClick(View arg0) {

        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, RQS_LOADIMAGE);
    }

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

      if (requestCode == RQS_LOADIMAGE) {

          if(resultCode == Form.RESULT_OK) {
            Uri imageUri = data.getData();
            array_list.add(imageUri);
            array_adapter.notifyDataSetChanged();
          }
      }
    }

}

ActivityForm: ActivityForm:

public class Form extends FragmentActivity {

    ViewPager viewPager = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_form);

        viewPager = (ViewPager) findViewById(R.id.pager);
        FragmentManager fragmentManager = getSupportFragmentManager();
        viewPager.setAdapter(new MyAdapter(fragmentManager));

    }

    public class MyAdapter extends FragmentStatePagerAdapter {

        public MyAdapter (FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            Fragment fragment = null;

            if (i == 0)
            {
                fragment = new Fragment1();
            }
            if (i == 1)
            {
                fragment = new Fragment2();
            }
            if (i == 2)
            {
                fragment = new Fragment3();
            }
            if (i == 3)
            {
                fragment = new Fragment4();
            }
            if (i == 4)
            {
                fragment = new Fragment5();
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 5;
        }
    }

}

View Pager only cache 3 pages left/current/right by default. View Pager默认只保留3页左/右/右。

you have to change it by adding following line in your code :- 您必须在代码中添加以下行来更改它: -

pager.setOffscreenPageLimit(5);  //in your case, you have 5 fragments 

Viepagers only cache 3 pages: current, left and right. Viepage只缓存3页:当前,左和右。 If your fragment falls off that list it will be recreated later. 如果您的片段从该列表中删除,则稍后将重新创建。 Use a FragmentStatePagerAdapter , this should fix your problems. 使用FragmentStatePagerAdapter ,这应该可以解决您的问题。

You can override onSaveInstanceState(Bundle) and save the state of your fragment in the bundle. 您可以覆盖onSaveInstanceState(Bundle)并在onSaveInstanceState(Bundle)保存片段的状态。 Also override onViewCreated(View, Bundle) . 同时覆盖onViewCreated(View, Bundle) It will have your saved data in the bundle, which you can use to restore your fragments state. 它会将您保存的数据保存在捆绑包中,您可以使用它来恢复碎片状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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