Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment is recreate on back press from other Fragment

I am facing a problem in regarding fragment. In my scenario,

There are two fragment associated with FragmentActivity. In FragmentActivity, there are a container layout (Frame Layout) in which all fragment will replace.

public void replaceFragment(Fragment fragmentClass) {
        String selectedFragment = fragmentClass.getClass().getName();


            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager
                    .beginTransaction();
            fragmentTransaction
                    .replace(R.id.content_frame, fragmentClass);            
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();           

    }

First time , I set a List type fragment (Fragment A) in which get the data from web service and papulate over listview. I execute the AsyncTask from onCreateView() method.

In MainActivity: onCreate

SherlockFragment fragment = new FragmentA();
replaceFragment(fragment);

On list item click of Fragment A, Fragment A will callback the activity method to replace it to details type fragment Fragment B.

@Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        callback = (ICallBack) activity;

    }


@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        /*View rootView = inflater.inflate(R.layout.fragment_locate, container,
                false);*/
        View rootView = inflater.inflate(R.layout.fragment_a, container,
                false);

        ListView list = (ListView) rootView
                .findViewById(R.id.listView);
        adapter = new MyListAdapter();
        list.setAdapter(adapter);
        list
                .setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent,
                            View convertView, int position, long id) {
                                SherlockFragment fragment = new SalonDetailFragment();
                                callback.replaceFragment(fragment);
                    }
                });

        ListDataTask task = new  ListDataTask();
        task.execute();

        return rootView;
    }

    class ListDataTask extends AsynTask<Void,Void,List<Data>>{

    public Void doInBackground(Void parems){

        List<Data> = getDataFromServer();
    }
    onPostExecute(List<Data> data){
        adapter.addAllData(data);
        adapter.notifyDataSetChanged();
    }

    }

When I press back button, from Fragment B then Application will show Fragment A but it execute Asynctask again and get the data to papulate the listview.

So I need to know, How to maintain the pervious state of Fragment like Activity.

Is there are any way to not to create Fragment after come back from Other activity

Have a look my pseudo code.

like image 925
Ashish Dwivedi Avatar asked Jul 28 '14 08:07

Ashish Dwivedi


2 Answers

I got solution. Simple.... do the check null value of rootview

public class FragmentA extends Fragment {
    View _rootView;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (_rootView == null) {
            // Inflate the layout for this fragment 
            _rootView = inflater.inflate(R.layout.fragment_a, container, false);
            // Find and setup subviews 
            _listView = (ListView)_rootView.findViewById(R.id.listView);
            ... 
        } else { 
            // Do not inflate the layout again. 
            // The returned View of onCreateView will be added into the fragment. 
            // However it is not allowed to be added twice even if the parent is same. 
            // So we must remove _rootView from the existing parent view group 
            // (it will be added back). 
            ((ViewGroup)_rootView.getParent()).removeView(_rootView);
        } 
        return _rootView
like image 99
Ashish Dwivedi Avatar answered Nov 15 '22 10:11

Ashish Dwivedi


I have the same problem and solved by replacing

 fragmentTransaction
                    .replace(R.id.content_frame, fragmentClass); 
to
 fragmentTransaction
                    .add(R.id.content_frame, fragmentClass); 

Replace will always create new instance on back press while Add is just add a new fragment in Stack. for more information check this link

like image 20
Rajesh Nasit Avatar answered Nov 15 '22 09:11

Rajesh Nasit