Android Fragment 事务在后台

标签 android android-fragments

我正在开发一个带有 fragment 的应用程序。它有一个 JavaScript 接口(interface),在主 Activity 中调用,并具有 fragment 替换逻辑。当应用程序在前台时一切正常,但当应用程序在后台时, fragment 事务替换不起作用。当我返回到我的应用程序时,我仍然看到旧 fragment 而看不到新 fragment 。

@JavascriptInterface
public void beginCall(String toast) {
    FragmentTransaction fTrans;
    taskFragment = TaskFragment.newInstance(toast,"");
    fTrans =    getSupportFragmentManager().beginTransaction();
    fTrans.replace(R.id.frgmCont, taskFragment);
    fTrans.commit();
}

怎么了?为什么 FragmentTransaction 在后台不起作用?

最佳答案

一段时间后我找到了答案:在onStop之后是不可能执行 fragment 事务的,它会导致java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState。我没有得到那个异常,因为 JavascriptInterface 是在一个单独的线程中执行的。当我强制我的代码在主线程中运行时,我得到了那个错误。所以我需要实现不同的逻辑,也使用一些Activity Life-cycle方法,或者切换到多个 Activity 逻辑。希望我的回答对大家有帮助。

关于Android Fragment 事务在后台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38199165/

相关文章:

android - 支持库 : FragmentTransaction animations not working

android - Fragment交易到底在哪里实现?

java - super() 前后代码的区别

java - 设置文本重心垂直在 android 编辑文本中不起作用

android - Qt/Android/CMake : Custom AndroidManifest. xml 未考虑在内

android - fragment : what is the best place to measure Views?

android - 共享元素过渡适用于 FragmentTransaction.replace() 但不适用于 FragmentTransaction.add()

Android - 如何创建类似 Google Inbox 的个人资料 View ?

Android 翻页标签故事

java - 如何在 Android 手机唤醒时可靠地检测互联网连接