简体   繁体   English

调用另一个Activity后删除片段的最佳方法是什么?

[英]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.

相关问题 在活动方法中知道哪个片段正在调用该方法的最佳方法是什么 - What is the best way to know in activity method which fragment is calling that method 将JSON数据从一个片段传递到另一个片段的最佳方法是什么 - What is best way to pass JSON data one Fragment to another Fragment Android:从片段中更新“活动”视图的最佳方法是什么? - Android: what is the best way to update the Activity view from within a fragment? 从活动更新片段视图的最佳方法是什么 - what is the best way to update fragment view from activity Android 中的 Fragment 和 Activity 之间通信的最佳方式是什么? - What is the best way of communicating between a Fragment and an Activity in Android? 片段-onDestroy活动后保存/恢复模型的最佳方法? - fragment - best way to save/restore model after onDestroy of activity? 销毁并重新创建活动后处理重新创建的对话框片段的最佳方法 - Best way of handling recreated dialog fragment after activity is destroyed and recreated 调用.remove(fragment)后未删除片段 - Fragment not being deleted after calling .remove(fragment) onAttach(Activity activity)这个活动在另一个片段内的一个片段中是什么 - onAttach(Activity activity) what is this activity in a fragment inside another fragment 获取数据和传递给另一个Activity的最佳方法是什么? - What is the best way for fetching data and for passing to another Activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM