每次重新启动后都会添加一个片段(Android)。

huangapple go评论75阅读模式
英文:

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 &#39;Apply changes and restart activity&#39;, 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.&lt;br/&gt; 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&#39;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, &quot;5&quot;).hide(fragment5).commit();
fm.beginTransaction().add(R.id.main_container, fragment4, &quot;4&quot;).hide(fragment4).commit();
fm.beginTransaction().add(R.id.main_container, fragment3, &quot;3&quot;).hide(fragment3).commit();
fm.beginTransaction().add(R.id.main_container, fragment2, &quot;2&quot;).hide(fragment2).commit();
fm.beginTransaction().add(R.id.main_container, fragment1, &quot;1&quot;).commit();
}

huangapple
  • 本文由 发表于 2020年10月2日 18:45:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64170196.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定