英文:
Fragment gets added each time after restart (Android)
问题
The fragments that I add to FragmentManager gets doubled each time I restart the activity, without destroying the previous fragments from the FragmentManager.
Here is my code:
private FragmentManager fm = getSupportFragmentManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
fm.beginTransaction().add(R.id.main_container, fragment5, "5").hide(fragment5).commit();
fm.beginTransaction().add(R.id.main_container, fragment4, "4").hide(fragment4).commit();
fm.beginTransaction().add(R.id.main_container, fragment3, "3").hide(fragment3).commit();
fm.beginTransaction().add(R.id.main_container, fragment2, "2").hide(fragment2).commit();
fm.beginTransaction().add(R.id.main_container, fragment1, "1").commit();
}
I understood that the fragment get added repeatedly when the methods in the fragments were called twice. But to check if it is actually the problem with the FragmentManager, I called the getFragments method
TextView someText = findViewById(R.id.text);
someText.setText("" + fm.getFragments());
and set it to TextView, right after the last transaction. The first time when the app is loaded, the TextView is empty. The second time when I use the 'Apply changes and restart activity', the TextView shows the fragments array list only once.. but the fragments get doubled. The third time the TextView shows the fragments array list twice and the fragments get tripled.
Why is this happening? Am I doing something wrong? Hope you will answer. Regards.
<details>
<summary>英文:</summary>
The fragments that I add to FragmentManager gets doubled each time I restart the activity, without destroying the previous fragments from the FragmentManager.
Here is my code:
private FragmentManager fm = getSupportFragmentManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
fm.beginTransaction().add(R.id.main_container, fragment5, "5").hide(fragment5).commit();
fm.beginTransaction().add(R.id.main_container, fragment4, "4").hide(fragment4).commit();
fm.beginTransaction().add(R.id.main_container, fragment3, "3").hide(fragment3).commit();
fm.beginTransaction().add(R.id.main_container, fragment2, "2").hide(fragment2).commit();
fm.beginTransaction().add(R.id.main_container, fragment1, "1").commit();
}
I understood that the fragment get added repeatedly when the methods in the fragments were called twice. But to check if it is actually the problem with the FragmentManager, I called the getFragments method
TextView someText = findViewById(R.id.text);
someText.setText(""+fm.getFragments());
and set it to TextView, right after the last transaction. The first time when the app is loaded, the TextView is empty. The second time when I use the 'Apply changes and restart activity', the TextView shows the fragments array list only once.. but the fragments get doubled. The third time the TextView shows the fragments array list twice and the fragments get tripled.<br/> Why is this happening? Am I doing something wrong? Hope you will answer. Regards.
</details>
# 答案1
**得分**: 1
尝试以下解决方案:
1. 在onCreate方法中这些行之前初始化片段管理器。
2. 尝试在返回按钮按下时弹出片段。
3. 不要一次性添加所有片段。只有在需要添加片段时才添加它。
<details>
<summary>英文:</summary>
try these solutions
1. Initialize fragment manager before these lines in onCreate method.
2. try to popup the fragment on backpress
3. don't add all fragmnets at once. only add the fragment when it needs to be added.
</details>
# 答案2
**得分**: 0
尝试像这样更改:
```java
private FragmentManager fm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
fm = getSupportFragmentManager();
fm.beginTransaction().add(R.id.main_container, fragment5, "5").hide(fragment5).commit();
fm.beginTransaction().add(R.id.main_container, fragment4, "4").hide(fragment4).commit();
fm.beginTransaction().add(R.id.main_container, fragment3, "3").hide(fragment3).commit();
fm.beginTransaction().add(R.id.main_container, fragment2, "2").hide(fragment2).commit();
fm.beginTransaction().add(R.id.main_container, fragment1, "1").commit();
}
英文:
Try changing it like this
private FragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
fm = getSupportFragmentManager();
fm.beginTransaction().add(R.id.main_container, fragment5, "5").hide(fragment5).commit();
fm.beginTransaction().add(R.id.main_container, fragment4, "4").hide(fragment4).commit();
fm.beginTransaction().add(R.id.main_container, fragment3, "3").hide(fragment3).commit();
fm.beginTransaction().add(R.id.main_container, fragment2, "2").hide(fragment2).commit();
fm.beginTransaction().add(R.id.main_container, fragment1, "1").commit();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论