[英]What is the best way to remove fragment after calling another Activity?
I believe I am running into a race condition. 我相信我遇到了比赛状况。 From a Fragment, I am starting an Activity, and following that I am removing the Fragment.
我从一个片段开始一个活动,然后删除该片段。 While the app is kept in the foreground there is no issue with this implementation.
虽然应用程序处于前台状态,但此实现没有问题。 However, if right before the Activity starts I put the app into the background, the Activity I am calling will start, however, the fragment is not removed.
但是,如果恰好在活动开始之前,我将应用程序置于后台,则我正在调用的活动将开始,但是不会删除该片段。 Here is my code,
这是我的代码,
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.putExtras(extras);
startActivity(intent);
if (getActivity() != null) {
getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
try {
getFragmentManager().popBackStack();
} catch (Exception e) {
e.printStackTrace();
}
}
Is there a better way to implement this? 有没有更好的方法来实现这一目标? I thought about adding the remove to another thread, but then I receive an exception saying that I cannot perform the remove fragment action before onSavedState.
我曾考虑过将删除添加到另一个线程,但是随后我收到一个异常消息,说我不能在onSavedState之前执行删除片段操作。 Thanks in advance!
提前致谢!
getsupportfragmentmanager()。弹出()
You can mark the fragment as removable (through a state instance variable) before starting the new activity. 您可以在开始新活动之前将片段标记为可移动(通过状态实例变量)。 When control gets back to the fragment, just pop the fragment off the stack in your fragment's onResume code, or in your activity's onResume code depending on the state of the fragment.
当控制权回到片段时,只需根据片段的状态将片段从片段的onResume代码或活动的onResume代码中弹出即可。 If you are not clear of the idea, let me know.
如果您不清楚这个主意,请告诉我。 I will write the code sample.
我将编写代码示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.