0

I have showing list of items which is displayed in grid and the data is coming from webservice . And when i click on the item it takes to the new screen but when coming from this screen to the screen having Grid the layout is recreated but i want to save the instance like activity so that the screen is not created again. what i can do to achieve this ?

public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            mfragmentManager = getActivity().getSupportFragmentManager();
                mhandler=new Handler(this);
            mLvAllDeals = (GridView) mview.findViewById(R.id.xLvAllDeals);
            mLvAllDeals.setCacheColorHint(0);
            mLvAllDeals.setOnItemClickListener(this);

                    new MyDealsAsyncTask().execute();
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                mAlDealName = new ArrayList<AllDealsData>();
    }


    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
         if(arg0==mLvAllDeals)
{
             position = arg2;

                        AllDealsData bean;

                            bean = mAlDealName.get(position);

                            Fragment frag = getFragmentManager().findFragmentById(R.id.inner_content2);
                            FragmentTransaction ft = getFragmentManager().beginTransaction();
                            ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right);
                            if (!frag.getTag().equals("dealsinfo")) 
                            {
                                Bundle args = new Bundle();
                                args.putString("dealid", bean.getId());
                                mdealinfo.setArguments(args);
                                ft.replace(R.id.inner_content2, mdealinfo, "dealsinfo");
                                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                                ft.addToBackStack(null);
                                ft.commit();
                            }


        }
}
        }
user2106413
  • 85
  • 1
  • 1
  • 9

1 Answers1

0

When performing transactions in fragments, onCreateView() and onActvityCreated() will be called again but it wont be called onCreate() method. So maintain data in onCreate() method. If data is already loaded, set directly to adapter as well as loading webservices data manage it using some extra flag.

For more information on fragment life cycle refer this link

Renjith
  • 5,783
  • 9
  • 31
  • 42
Ramesh Akula
  • 5,720
  • 4
  • 43
  • 67
  • That is correct but when i am coming back to this screen it nither saves the screen nor the Asynctask(new MyDealsAsyncTask().execute();) is called and hence data is not loaded to the screen. Can you tell me what to do in this case. – user2106413 Apr 15 '13 at 11:21
  • Register this listener after transaction completed(commit completed.). This listener will helpful to get to know that current fragment will be appeared while child frgment is detached from the fragment manager. getSupportFragmentManager().addOnBackStackChangedListener(new OnBackStackChangedListener() { @Override public void onBackStackChanged() { // TODO Auto-generated method stub } }); – Ramesh Akula Apr 15 '13 at 12:26