如何使用方向更改保存自定义ListFragment状态

时间:2014-09-08 17:48:48

标签: android android-listfragment savestate

我越来越希望这个问题实际上更容易理解。

活动目的:允许用户从图库中选择图片;在ListFragment中显示图像的缩略图以及标题用户给出了图像;用户完成后保存每个图像的uri和标题,用户名称给出了这个图像集合。

问题:当设备被旋转时,FragmentList会丢失用户已经选择的所有图像和标题,即列表中的所有行都丢失了。

尝试解决问题

  • 实现了RetainedFragment以在设备轮换时保存List集合。以前我没有这样做,并且认为“啊,适配器在创建时被送入一个空白的List集合。我将保存List的状态,然后当调用Activity的onCreate时,我可以将保留的List提供给Adapter构造函数,它会工作的。“但它没有。

  • 然后我想,“当然它不起作用,你还没有通知适配器这个改变!”所以我将adapter.notifyDataSetChanged()放在onCreate中。这不起作用。

  • 然后我将adapter.notifyDataSetChanged()移到onStart,以为我可能需要稍后在活动的生命周期中通知适配器。没用。

注意:我在同一个应用程序中有另一个使用相同自定义ListViewFragment的活动,并且正在使用设备方向更改保留ListFragment的状态。该活动有两个主要区别:片段被硬编码到.xml中(我认为这不会产生影响,除了可能Android的本机保存的.xml片段与编程添加的片段不同);并且该活动使用Loader和LoaderManager并从我构建的Provider(从我的SQLite数据库收集数据)中获取其数据。看看这两个活动之间的差异是什么导致我认为“你不是以某种方式处理适当的数据”并激励我在旋转设备时使用RetainedFragment来保存List集合。

...这促使我考虑如何解决,正如Android在他们的Loader页面上关于LoaderManager所说:

  

“与用于管理一个或多个Loader实例的Activity或Fragment相关联的抽象类。这有助于应用程序结合Activity或Fragment生命周期管理长时间运行的操作;最常见的用途是使用CursorLoader,但是应用程序可以自由编写自己的加载器来加载其他类型的数据。“

“加载其他类型的数据”部分让我想到“我可以使用LoaderManager来加载列表数据吗?我害羞的两个原因:1)我已经,至少在概念上,应该工作; 2)我现在所做的并不是一个“长时间运行”,我不认为。

研究

活动 - 删除许多希望无关的内容:

public class AddActivity extends Activity{

    // data collection
    List<ImageBean> beanList;

    // adapter
    AddCollectionAdapter adapter;

    // ListViewFragment tag
    private static final String LVF_TAG = "list fragment tag";

    // fragment handles
    ListViewFragment listFrag;

    // Handles images; LruCache for bitmapes
    ImageHandler imageHandler;

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

        // Create ImageHandler that holds LruCache
        imageHandler = new ImageHandler(this, getFragmentManager());        

        // Obtain retained List<ImageBean> or create new List<ImageBean>.
        RetainedFragment retainFragment = RetainedFragment.findOrCreateRetainFragment(getFragmentManager());

        beanList = retainFragment.list;

        if(beanList == null){

            beanList = new ArrayList<ImageBean>();

            retainFragment.list = beanList;             
        }           

        // create fragments
        if(savedInstanceState == null){

            listFrag = new ListViewFragment();  

            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.add(R.id.add_fragFrame, listFrag, LVF_TAG);

            ft.commit();            

        }else{
            listFrag = (ListViewFragment)getFragmentManager().findFragmentByTag(LVF_TAG);               
        }

        // create adapter
        adapter = new AddCollectionAdapter(this, beanList);

        // set list fragment adapter
        listFrag.setListAdapter(adapter);
    }       

    @Override
    protected void onStart() {

        // TESTING: If device orientation has changed List<ImageBean> was saved
        // with a RetainedFragment. Seed the adapter with the retained
        // List.
        adapter.notifyDataSetChanged();
        super.onStart();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

        // Android automatically saves visible fragments here. (?)

        super.onSaveInstanceState(outState);
    }   

    /*
     * ImageBean.
     */
    public static class ImageBean{
        private String collectionName;  // Title of image collection
        private String imageUri;        // Image URI as a string
        private String imageTitle;      // Title given to image

        public ImageBean(String name, String uri, String title){
            collectionName = name;
            imageUri = uri;
            imageTitle = title;
        }

        public String getCollectionName() {
            return collectionName;
        }

        public String getImageUri() {
            return imageUri;
        }

        public String getImageTitle() {
            return imageTitle;
        }       
    }

    /*
     * Called when user is finished selecting images.
     * 
     * Performs a bulk insert to the Provider.
     */
    private void saveToDatabase() {
        int arraySize = beanList.size();
        final ContentValues[] valuesArray = new ContentValues[arraySize];

        ContentValues values;
        String imageuri;
        String title;
        int counter = 0;


        for(ImageBean image : beanList){

            imageuri = image.getImageUri();
            title = image.getImageTitle();

            values = new ContentValues();   

            values.put(CollectionsTable.COL_NAME, nameOfCollection);
            values.put(CollectionsTable.COL_IMAGEURI, imageuri);
            values.put(CollectionsTable.COL_TITLE, title);
            values.put(CollectionsTable.COL_SEQ, counter +1);

            valuesArray[counter] = values;
            counter++;
        }

        AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... arg0) {
                getContentResolver().bulkInsert(CollectionsContentProvider.COLLECTIONS_URI, valuesArray);   
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {

                // End this activity.
                finish();   
            }           
        };

        task.execute();                 
    }   

    public ImageHandler getImageHandler(){
        return imageHandler;
    }
}

class RetainedFragment extends Fragment{

    private static final String TAG = "RetainedFragment";

    // data to retain
    public List<AddActivity.ImageBean> list;

    public static RetainedFragment findOrCreateRetainFragment(FragmentManager fm){

        RetainedFragment fragment = (RetainedFragment)fm.findFragmentByTag(TAG);

        if(fragment == null){

            fragment = new RetainedFragment();
            fm.beginTransaction().add(fragment, TAG);
        }

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setRetainInstance(true);
    }   
}

ListFragment:

public class ListViewFragment extends ListFragment {

ListFragListener listener;

public interface ListFragListener{
    public void listFragListener(Cursor cursor);
}       

@Override
public void onCreate(Bundle savedInstanceState) {

    // Retain this fragment across configuration change
    setRetainInstance(true);

    super.onCreate(savedInstanceState);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    // Set listener
    if(activity instanceof ListFragListener){

        listener = (ListFragListener)activity;      

    }else{

        //Instantiating activity does not implement ListFragListener.
    }
}

@Override
public void onListItemClick(ListView listView, View v, int position, long id) {

    // no action necessary
}   
}   

适配器:

public class AddCollectionAdapter extends BaseAdapter {

// data collection
List<ImageBean> beanList;

// layout inflator
private LayoutInflater inflater;

// context
Context context;

public AddCollectionAdapter(Context context, List<ImageBean> beanList){
    this.context = context;
    this.beanList = beanList;
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return beanList.size();
}

@Override
public Object getItem(int position) {
    return beanList.get(position);
}

@Override
public long getItemId(int arg0) {
    // collection not from database nor is going directly to database; this is useless.
    return 0;
}

// holder pattern
private class ViewHolder{
    ImageView imageView;
    TextView titleView;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
    View xmlTemplate = convertView;

    if(xmlTemplate == null){

        //inflate xml
        xmlTemplate = inflater.inflate(R.layout.frag_listview_row, null);

        // initilaize ViewHolder
        holder = new ViewHolder();

        // get views that are inside the xml
        holder.imageView = (ImageView)xmlTemplate.findViewById(R.id.add_lvrow_image);
        holder.titleView = (TextView)xmlTemplate.findViewById(R.id.add_lvrow_title);

        // set tag
        xmlTemplate.setTag(holder);

    }else{

        holder = (ViewHolder)xmlTemplate.getTag();
    }

    // Get image details from List<ImageBean>
    ImageBean bean = beanList.get(position);        
    String imageUri = bean.getImageUri();
    String title = bean.getImageTitle();

    // Set Holder ImageView bitmap; Use parent activity's ImageHandler to load image into Holder's ImageView.
    ((AddActivity)context).getImageHandler().loadBitmap(imageUri, holder.imageView, Constants.LISTVIEW_XML_WIDTH, Constants.LISTVIEW_XML_HEIGHT);       

    // Set Holder's TextView.
    holder.titleView.setText(title);

    // return view
    return xmlTemplate;
}
}

1 个答案:

答案 0 :(得分:1)

解决。将日志语句放入战略位置后,我发现RetainedFragment的列表始终为null。在RetainedFragment中注意到这一点之后:

fm.beginTransaction().add(fragment, TAG);

我错过了commit()

我添加后,现在通过配置更改保留状态。

有关保存我在试验和磨难期间发现的ListFragment状态的更多信息:

如果您通过以下方式添加片段:

    if(savedInstanceState == null){

        listFrag = new ListViewFragment();  

        // programmatically add fragment to ViewGroup
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(R.id.add_fragFrame, listFrag, LVF_TAG);

    }

然后其中任何一个都适用于else

1) This one works because Android takes care of saving the Fragment:

   listFrag = (ListViewFragment)getFragmentManager().findFragmentByTag(LVF_TAG);

2) This one works because the fragment was specifically saved into bundle in
   onSaveInstanceState:

   listFrag = (ListViewFragment)getFragmentManager().getFragment(savedInstanceState, LVF_TAG);

要使数字2起作用,这发生在onSaveInstanceState():

@Override
protected void onSaveInstanceState(Bundle outState) {       
    super.onSaveInstanceState(outState);

    getFragmentManager().putFragment(outState, LVF_TAG, listFrag);
}