为什么我的片段没有在步骤4中显示?我已经将片段添加到后台堆栈,所以为什么(当Back按钮看起来已经知道它存在时)它是否显示片段?
这是我在Activity 2中用来打开Fragment A的代码.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_profile_edit);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
transaction.addToBackStack(null);
transaction.add(android.R.id.content, new MyFragment());
transaction.commit();
}
Run Code Online (Sandbox Code Playgroud)
这是打开Fragment B的代码
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
transaction.add(android.R.id.content, new MyOtherFragment());
transaction.commit();
Run Code Online (Sandbox Code Playgroud)
Ale*_*ran 10
你尝试过transaction.replace(...)而不是transaction.add(...)吗?这应该工作.我猜是因为如果你只是在另一个上面添加一个片段,它就不会看到事务需要从片段A返回.
编辑问题 的实际答案如下所示:addToBackStack()应该用于替换的片段,而不是被替换的片段.
| 归档时间: |
|
| 查看次数: |
10151 次 |
| 最近记录: |