片段交易和 activity 转换
Fragment transactions and activity transitions
我的应用程序中有这个场景:
一个 activity(A) 有多个片段在某个时候调用另一个 activity(B)。
流程是这样的:F1 => F2 => F3 => B 或 F1 => F2 => B 其中 F(n) 代表 fragment.After 我完成 activity B 它returns 我到 F3 或 F2 但我的目标是向用户显示 F1 所以我尝试通过事件总线发送事件并用 F1 替换任何其他片段,请注意我将每个片段添加到 backstack.So 我成功了有了它,但如果我调用片段 F2 或 F3 应用程序崩溃,有时我也会得到 "IllegalStateException: Can not perform this action after onSaveInstanceState"
。
所以在尝试了很多方法之后,我只是这样做了:
public void onClick(View v){
//started activity B
//replaced current fragment with F1
}
最终结果是在 activity B 之前看到 F1,其他一切正常,没有 crashing.So 解决这个故障我在 activity B 启动后 100 毫秒更换了片段.
public void onClick(View v){
//started activity B
new Handler().(new Runable(){
@Override
public void run()
{
//replaced current fragment with F1
}
},100);
}
但是我觉得这样解决这个问题很丑,想请教各位有没有更好的解决办法?
编辑:
我受到特殊答案的启发,所以我对状态做了类似的事情。
在activityA中我有两个变量。
boolean wasAnotherActivityCalled=false;
String showFragment=null;
在我的片段中我有这个:
public void onClick(View v){
//started activity B
getActivity().wasAnotherActivityCalled=true;
getActivity().showFragment=FragmentOne.class.getSimpleName();
}
在 activity A 我有这个 :
@Override
protected void onResumeFragments() {
super.onResumeFragments();
if(wasAnotherActivityCalled)
{
if(showFragment.equals(F1.class.getSimpleName()))
{ //do your logic here}
wasAnotherActivityCalled=false;
showFragment=null;
}
}
提交片段时不要将片段添加到后台堆栈中
我的应用程序中有类似的东西。我所做的是使用一个简单的 "state-machine" ,其中我有一个代表当前状态的 int
属性(0 = Fragment1,1 = Fragment2 ...等)和一个 ArrayList
与所有fragments
我需要的。如果我必须切换 fragment
,我也会增加状态并从 ArrayList
加载片段。
在我的 onPause()
方法中,我将状态保存在 sharedPreferences()
中,在 onResume()
方法中,我从 sharedPreferences()
加载状态并执行 initFragment(state)
我只是用 fragmentArray[state]
中的片段替换 fragmentLayout
:-)
通过这种行为,我可以自己处理后台堆栈,可以返回并进一步保存当前所需片段的状态,每次用户更改 activity 并返回。此外,我不会将 fragments
提交到 backstack 中,因为它已经由我自己处理了。
希望能帮到你。
我的应用程序中有这个场景:
一个 activity(A) 有多个片段在某个时候调用另一个 activity(B)。
流程是这样的:F1 => F2 => F3 => B 或 F1 => F2 => B 其中 F(n) 代表 fragment.After 我完成 activity B 它returns 我到 F3 或 F2 但我的目标是向用户显示 F1 所以我尝试通过事件总线发送事件并用 F1 替换任何其他片段,请注意我将每个片段添加到 backstack.So 我成功了有了它,但如果我调用片段 F2 或 F3 应用程序崩溃,有时我也会得到 "IllegalStateException: Can not perform this action after onSaveInstanceState"
。
所以在尝试了很多方法之后,我只是这样做了:
public void onClick(View v){
//started activity B
//replaced current fragment with F1
}
最终结果是在 activity B 之前看到 F1,其他一切正常,没有 crashing.So 解决这个故障我在 activity B 启动后 100 毫秒更换了片段.
public void onClick(View v){
//started activity B
new Handler().(new Runable(){
@Override
public void run()
{
//replaced current fragment with F1
}
},100);
}
但是我觉得这样解决这个问题很丑,想请教各位有没有更好的解决办法?
编辑:
我受到特殊答案的启发,所以我对状态做了类似的事情。
在activityA中我有两个变量。
boolean wasAnotherActivityCalled=false;
String showFragment=null;
在我的片段中我有这个:
public void onClick(View v){
//started activity B
getActivity().wasAnotherActivityCalled=true;
getActivity().showFragment=FragmentOne.class.getSimpleName();
}
在 activity A 我有这个 :
@Override
protected void onResumeFragments() {
super.onResumeFragments();
if(wasAnotherActivityCalled)
{
if(showFragment.equals(F1.class.getSimpleName()))
{ //do your logic here}
wasAnotherActivityCalled=false;
showFragment=null;
}
}
提交片段时不要将片段添加到后台堆栈中
我的应用程序中有类似的东西。我所做的是使用一个简单的 "state-machine" ,其中我有一个代表当前状态的 int
属性(0 = Fragment1,1 = Fragment2 ...等)和一个 ArrayList
与所有fragments
我需要的。如果我必须切换 fragment
,我也会增加状态并从 ArrayList
加载片段。
在我的 onPause()
方法中,我将状态保存在 sharedPreferences()
中,在 onResume()
方法中,我从 sharedPreferences()
加载状态并执行 initFragment(state)
我只是用 fragmentArray[state]
中的片段替换 fragmentLayout
:-)
通过这种行为,我可以自己处理后台堆栈,可以返回并进一步保存当前所需片段的状态,每次用户更改 activity 并返回。此外,我不会将 fragments
提交到 backstack 中,因为它已经由我自己处理了。
希望能帮到你。