0

Maps in fragment crashes in sliding drawer when clicked twice on Maps item

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.map_fine_tech, container, false);
    fragment =  getFragmentManager().findFragmentById(R.id.map);
    return rootView;
}
Shivam Kumar
  • 1,892
  • 2
  • 21
  • 33
Deep Singh
  • 147
  • 1
  • 3
  • 16

1 Answers1

0

To avoid crashes I'm using global variable which I declare in MyApplication class which extends from Application class

MyApplication app = (MyApplication) getApplicationContext();
boolean mapstate = app.isMapstate();
String bvalue = String.valueOf(mapstate);
Log.e("tag1", bvalue);
Fragment fragment = null;

switch (position) {
case 0:
    fragment = new UserProfileScreen();
    app.setMapstate(false);
    String c = String.valueOf(mapstate);
    Log.e("case0", c);
    break;
case 1:
    if(mapstate==false)
    {
        fragment = new MapFineTech();
        app.setMapstate(true);
        String bvalue2 = String.valueOf(mapstate);
        Log.e("tag2", bvalue2);
    }
    else
    {

    }
    break;
}
Shivam Kumar
  • 1,892
  • 2
  • 21
  • 33
Deep Singh
  • 147
  • 1
  • 3
  • 16
  • I have many Fragments from Slidingdrawer.When i have the HomeFragment pressed and it has a map and when i click it again it crashes.How can i avoid it?? – Setu Kumar Basak Feb 23 '15 at 14:35
  • try{ if (fragment != null) { fragment = getFragmentManager().findFragmentById(R.id.map); FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction(); ft.remove(fragment); ft.commit(); } } catch(Exception e) { e.printStackTrace(); } Thread.interrupted(); super.onPause(); – Deep Singh Feb 24 '15 at 07:41