Android : How To Reuse Existing Fragment instead of recreating it (after rotation)

on Monday, November 3, 2014


I am using an Activity with 2 fragments: 1 Fragment - side menu 2nd Fragment - main window.


For one of my fragments that appears in the main window, it shows an image in a relative layout.


When I rotate the device, the fragment is re-created and now I have 2 of the same images shown on the device since Android re-creates and adds the fragment instead of reusing it.


I have tried doing a trick in my activity:



if (savedInstanceState==null) {
//load and replace to initial fragment
}


however I cannot seem to 'load' or 'save' the existing fragment in my main window correctly.


Can anyone help me with this? I have tried a few solutions on SO but nothing has worked so far.


Thanks


0 comments:

Post a Comment