0

I have a fragment with list:

public class GuestsList extends Fragment implements AdapterView.OnItemClickListener {
    private GuestsListAdapter adapter;
    private FragmentTransaction fTrans;
    private Fragment guestFragment;
    private ArrayList<GuestBean> guests;
    private ListView list;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.guestlist, null);

        guests = new ArrayList<GuestBean>();
        list = (ListView) v.findViewById(R.id.gustLIST);
        adapter = new GuestsListAdapter(getActivity(), guests);
        list.setOnItemClickListener(this);

        new NewThread().execute();

        return v;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        fTrans = getFragmentManager().beginTransaction();
        guestFragment = new Guest();
        fTrans.replace(R.id.content_frame, guestFragment);
        fTrans.addToBackStack(null);
        fTrans.commit();
    }

    public class NewThread extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... strings) {
          //load data to guests
        }

        @Override
        protected void onPostExecute(String s) {
            list.setAdapter(adapter);
            progressDialog.cancel();
        }
    }
}

When you click on an item I create a new fragment Guest and old fragment were placed in backstack. When I'm in a new fragment and click the back button opens fragment GuestsList which was in backstack and again the data downloaded from the Internet. I want them to be saved when creating a new fragment, and when you click on the back button to reload happens.

IP696
  • 181
  • 1
  • 2
  • 11
  • Maybe this can help you: http://stackoverflow.com/questions/17252236/how-to-persist-fragment-data-after-backstack-transactions – manao Oct 27 '14 at 08:34

1 Answers1

0

Easiest solution is to use "add" instead of "replace" in your fragment transaction

 fTrans.add(R.id.content_frame, guestFragment);

It should not create new fragment when you will press back, but this is not very good solution if you have a lot of fragments (when I had many fragments and used "add" application lagged)

You may create your objects cache in custom Application class and get objects from application class.

Dmitry_L
  • 1,004
  • 7
  • 8