Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Layout duplicating itself on screen rotation

I have a layout that has an EditText and a Button. I <include> it in my main layout.

I'm having a weird issue with the layout and rotation. It seems to duplicate itself when the device (physical) is rotated, messing up the text and layout.

Here it is on first open, after I add some extra garble:

1

DSC_0013 is in the EditText on launch of the fragment.

Then, I rotate the phone and add some different garble:

2

And you can see the issue pretty clearly. At first, I thought it was just the EditText messing up. But if I add enough text to make a new line:

3

I can see that the button gets messed up too.

I do override onSaveInstanceState, but in it I don't touch the EditText or its value, it's strictly used for something else.

What's happening and how do I fix it?

like image 933
Steven Schoen Avatar asked Sep 10 '12 00:09

Steven Schoen


2 Answers

Fixed it!

Turns out it wasn't the view duplicating itself, or the EditText, or the Button. It was the entire fragment.

In my Activity's onCreate, I add the fragment to an xml layout:

private FileDetails fileDetailsFragment;

public void onCreate(Bundle savedInstanceState) {
        ...
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();
        fileDetailsFragment = new FileDetails(fileData);
        fragmentTransaction.add(R.id.DetailsHolder, fileDetailsFragment);
        fragmentTransaction.commit();

And onCreate was being called every time I rotated the phone (as it's meant to). So I put in a check to see if the activity is being run for the first time, and it works great.

private FileDetails fileDetailsFragment;

public void onCreate(Bundle savedInstanceState) {
    ...
    if (savedInstanceState == null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();
        fileDetailsFragment = new FileDetails(fileData);
        fragmentTransaction.add(R.id.DetailsHolder, fileDetailsFragment);
        fragmentTransaction.commit();
    } else {
        fileDetailsFragment = (FileDetails) getSupportFragmentManager().findFragmentById(R.id.DetailsHolder);
    }
like image 107
Steven Schoen Avatar answered Nov 08 '22 18:11

Steven Schoen


You can also setRetainedInstance(true) on your fragment, then try to get the Fragment form de FragmentManager.findFragmentById(int) or FragmentManager.findFragmentByTag(String), and if it returns null it meant you had to create a new instance of your Fragment.

private FileDetails fileDetailsFragment; 


public void onCreate(Bundle savedInstanceState) {
...
    FragmentManager fragmentManager = getSupportFragmentManager();
    fileDetailsFragment = (FileDetails) getSupportFragmentManager().findFragmentById(R.id.DetailsHolder);
    if (fileDetailsFragment == null) {
        fileDetailsFragment = new FileDetails(FileData); 
    }

    FragmentTransaction fragmentTransaction = fragmentManager
            .beginTransaction();

    fragmentTransaction.add(R.id.DetailsHolder, fileDetailsFragment);
    fragmentTransaction.commit();
}
like image 34
Raziel25 Avatar answered Nov 08 '22 16:11

Raziel25