1

I have a Fragment in wich I have a listview. When I press on the listview position I open fragment dialog in which I send the position of clicked item. There I then can change some data live from the listview. But when I change my orientation (both the fragment and dialog fragment get different layouts), the live changes dont work anymore. If I go to another fragment and than back the change is made but not live.

both have setRetainInstance to true.

setting the adapter in the fragment:

 documentPosListViewAdapter = new DocumentPosListViewAdapter(rootView.getContext());
    listViewDocumentPos.setAdapter(documentPosListViewAdapter);

dialog listener:

@Override
    public void onDataUpdated() {

        currentWorkDocumentPos.calcPos();                                   
        session.getWorkDocumentPosDao().update(currentWorkDocumentPos); 

        currentWorkDocument.headCalc(); 

        session.getWorkDocumentDao().update(currentWorkDocument);   

        refreshWorkDocumentLabels();    

        documentPosListViewAdapter.notifyDataSetChanged();

    }

And what I am doing in the dialog:

currentWorkDocumentPos.setQuantity(quantity + 1);
                String n_q = Double.toString(currentWorkDocumentPos.getQuantity());
                editTextQuantity.setText(n_q);
                editTextQuantity.setSelection(n_q.length());
                myListener.onDataUpdated();

Everything works until I change screen orientation. Then live changes dont happen anymore. I am guessing that documentPosListViewAdapter.notifyDataSetChanged(); doesent do its work anymore but I am not sure why.

EDIT:

In the dialog i have these 2 parts and the dialog layout recreates itself on rotation normaly:

setRetainInstance(true); and 
@Override
public void onDestroyView() {
  if (getDialog() != null && getRetainInstance())
      getDialog().setDismissMessage(null);
  super.onDestroyView();
}

And in the mainFragment I have setRetainInstance(true); aswell and it recreates when I rotate my screen. If I remove setRetainInstance from main fragment then 2 things happen: 1. When I open another fragment and rotate layouts of both fragment seem to overlap istelf :S 2. When I open the fragment dialog from the listview and rotate the dialog looses the listview position on wich i clicked

Tadej Vengust
  • 1,351
  • 4
  • 18
  • 35
  • Just to be clear, you set 'setRetainInstance' to true, but you recreate the layouts manually on rotation? – Qw4z1 Oct 22 '14 at 14:47
  • I have Edited my question. Please check. – Tadej Vengust Oct 23 '14 at 07:52
  • Check this question http://stackoverflow.com/questions/11307390/dialogfragment-disappears-on-rotation-despite-setretaininstancetrue and try to only remove setRetainInstance in the Dialog – Qw4z1 Oct 24 '14 at 07:57
  • Rotation still works but I lose the instance to listview position. I could save it in onSaveInstanceState but that would just result in the same problem I have now. That the listview doesent refresh live. Only if I load the fragment wich contains it again. – Tadej Vengust Oct 24 '14 at 08:28
  • Uhm... Do you create a fragment every time the Activity starts? If so it might actually create a new fragment while the old one lives in the background. – Qw4z1 Oct 24 '14 at 09:09
  • No. I have: if (savedInstanceState == null) displayView(0); and displayView creates the fragment.. – Tadej Vengust Oct 24 '14 at 09:31
  • Ok. And I guess you have verified that savedInstanceState != null when rotating the device. =) Have you found a solution to the problem yet? – Qw4z1 Oct 28 '14 at 13:04
  • I didnt found a soluton only a work around. For time beeing I freze screen orientation when I open the dialog and unfreeze it after it closes. Its a quick fix until I figure out what the cause is. – Tadej Vengust Oct 28 '14 at 13:53

1 Answers1

0

You need to delete setRetainInstance(true) in your fragments! When you rotate your device your app will recreate the activity. But in your fragments there are links to your old activity stored (which have already been destroyed when after rotating), that's why you can't do anything with them.

If you want to save data when orientation is changed - save this data in bundle in onSaveInstanceState and get them, for example, onRestoreInstanceState.

QArea
  • 4,955
  • 1
  • 12
  • 22
  • If I do that then nothing works. If I remove setRetainInstance from my main fragment then when I open another fragment and rotate screen layout go one ontop another :/ And eaven when I open the dialog after rotation I now lose the right listview position because it changed in the main fragment. – Tadej Vengust Oct 23 '14 at 07:00