Duplication of listview rows in list fragment on orientation change
Asked Answered
D

5

9

I have a ListFragment where I use a custom adapter to populate the listview. All is well until I change orientation and scroll. Then it looks like this: ListView after a few orientation changes

I am guessing it has something to do with me fumbling with a view holder, but I can't access the code right now.

Disquisition answered 30/8, 2012 at 19:15 Comment(1)
possible duplicate of Android ListFragment list view overlapping on orientation changeDisquisition
D
14

The reason for the overlapping fragment was that I used FrameLayout and added the fragment with FragmentTransition.add(...). When I changed .add() to .replace() the old fragment was removed and the new one was added and my problem was solved.

Disquisition answered 1/9, 2012 at 10:16 Comment(0)
R
2

I had similar problem and according to that i am telling the solution :- you are getting this blur because every time on orientation change somewhere new instance of listfragment is created (may be it is in oncreate()), so you have to just make an instance of list fragment once and on orientation change replace that fragment rather than adding again.

Restrictive answered 3/4, 2013 at 13:6 Comment(0)
D
0

Changing orientation causes onCreate to restart unless you include this method

public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
}

and put this in the activity section of your manifest

android:configChanges="orientation"
Deedeeann answered 30/8, 2012 at 19:24 Comment(1)
Yes, but the question was why it overlaps the old layout. And the answer to that is in the duplicate question. Thanks for the tip anyway!Disquisition
V
0

Check that this is the first time onCreate() is called, in other words, determine if the callback is not due to screen rotation.

protected void onCreate(Bundle savedInstanceState) {
        if(savedInstanceState == null) {
    // transition.add(Your fragment,...)
    }
}
Veronikaveronike answered 7/3, 2015 at 10:7 Comment(0)
S
0

I had the same problem, and its because of FrameLayout usage, First you have to check if your fragment has already added to the activity :

String TagName = "F";

Adding a fragment without a UI If you want to get the fragment from the activity later, you need to use findFragmentByTag(). "Google" http://developer.android.com/guide/components/fragments.html#Adding

Fragement F = getFragmentManager().findFragmentByTag(TagName);

Then check

if(F == null) {
  F = new F();
  getFragmentManager()
     .beginTransaction()
     .add(R.id.container, F, TagName)
     .commit();
}

the target here is to avoid adding or creating new instance of the fragment, that persist during the configuration change which causes the problem when using the FrameLayout as container.

Solution 2 (Simple): The only thing you have to do here is to change your container to ex:LinearLayout , and that's it. but in my opinion this is not the best solution because of multiple instances of the fragment.

Hope this help;

Stickle answered 20/1, 2016 at 6:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.