Layout duplicating itself while rotating the screen

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

I have a weird problem with layout and rotation. It seems to be duplicated when the device (physical) is rotated, messing up the text and layout.

Here it first opens, after adding some additional distortion:

one

DSC_0013 is in EditText when the fragment starts.

Then, I rotate the phone and add a few different distortions:

2

And you can clearly see the problem. At first I thought it was just an EditText intervention. But if I add enough text to create a new line:

3

I see that the button is also messed up.

I override onSaveInstanceState , but in it I do not touch EditText or its value, it is strictly used for something else.

What is happening and how to fix it?

+7
source share
2 answers

Fixed!

It turns out that it was not duplication, or EditText, or Button. That was the whole fragment.

In my onCreate activity onCreate I add a fragment to the 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 called every time I rotated the phone (as it was). So I put a check to check if the work is being done 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); } 
+13
source

You can also set RetainedInstance (true) to your fragment, then try to get the form Fragment de FragmentManager.findFragmentById (int) or FragmentManager.findFragmentByTag (String), and if it returns null, it means that 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(); } 
+1
source

All Articles