ListFragments不会自动恢复数据集吗?

时间:2016-03-08 22:22:31

标签: java android android-fragments android-activity android-listfragment

我有一个扩展ListFragment的片段,当我最小化应用程序并返回它时,listview为空。

不会自动恢复吗?

public class LocationListViewFrag extends ListFragment implements AdapterView.OnItemClickListener {

    private static final String TAG = "LocationListViewFrag";

    private ArrayList<LocationObj> mLocations = new ArrayList<>();
    private LocationAdapter mTaxAdapter;
    private LocationListViewListener mListener;

    @Bind(R.id.listLabel)
    TextView listLabel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTaxAdapter = new LocationAdapter(getActivity(), mLocations);
        setListAdapter(mTaxAdapter);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_tax_location_list, container, false);
        ButterKnife.bind(this, v);
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getListView().setOnItemClickListener(this);

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            mListener = (LocationListViewListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() + " must implement LocationListViewListener");
        }
    }

    public void setLocations(ArrayList<LocationObj> Locations) {
        mLocations = Locations;

        mTaxAdapter.clear();
        mTaxAdapter.addAll(mLocations);
        mTaxAdapter.notifyDataSetChanged();

        if(mLocations.isEmpty()){
            listLabel.setVisibility(View.VISIBLE);
        } else {
            listLabel.setVisibility(View.GONE);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        LocationObj aLocation = mLocations.get(position);
        Log.d(TAG, String.format("Clicked - %s", aLocation.toString()));

        mListener.onLocationSelected(aLocation);
    }

    public interface LocationListViewListener {

        void onLocationSelected(LocationObj LocationObj);

    }
}

活动

onCreate

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

        setContentView(R.layout.free_tax_prep_providers_new);
        initDrawer();
        ButterKnife.bind(this);

        if (savedInstanceState != null) {
            //Restore the fragment's instance
            mLocationListViewFrag = (LocationListViewFrag)getSupportFragmentManager().getFragment(savedInstanceState, "mLocationListViewFrag");

        } else if (findViewById(R.id.fragment_container) != null) {
            // Check that the activity is using the layout version with
            // the fragment_container FrameLayout

            // Create a new Fragment to be placed in the activity layout
            mLocationListViewFrag = new LocationListViewFrag();

            // Add the fragment to the 'fragment_container' FrameLayout
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, mLocationListViewFrag).commit();

        }

    }

onSavedInstance

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

        //Save the fragment's instance
        getSupportFragmentManager().putFragment(outState, "mLocationListViewFrag", mLocationListViewFrag);

    }

1 个答案:

答案 0 :(得分:0)

您实际上可以在片段本身而不是活动中覆盖onSaveInstanceState。当我使用列表片段时,这样做对我有用。