English 中文(简体)
从容器中移除碎片后, 碎片返回为真 。 isAdded 返回为真 。
原标题:Fragment.isAdded returns true after fragment removed from a container

我有一个活动与下面的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <FrameLayout
        android:id="@+id/frameLayoutA"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1" >
    </FrameLayout>
    <FrameLayout
        android:id="@+id/frameLayoutB"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1" >
    </FrameLayout>
</LinearLayout>

在活动类别上,我在LayoutA框架和B框架LayoutB框架上装载碎片A。

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    fmA=(FrameLayout) findViewById(R.id.frameLayoutA);
    fmB=(FrameLayout) findViewById(R.id.frameLayoutB);

    fragA=new FragmentA();
    fragB=new FragmentB();
    fragC=new FragmentC();
    addFragmentsInActivity(R.id.frameLayoutA,fragA);
    addFragmentsInActivity(R.id.frameLayoutB,fragB);
}

public void addFragmentsInActivity(int id, Fragment fragment)
{
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(id, fragment);
    fragmentTransaction.commit();
}

使用菜单操作, 我要在框架 LayoutA 和框架 LayoutB 中装入碎片_ B 和碎片_ C 。 菜单操作是 :

    removeFragmentsInActivity(R.id.frameLayoutB,fragB);
    addFragmentsInActivity(R.id.frameLayoutB,fragC);
    if(!fragB.isAdded()){
            Log.e("check", "fragB already removed from frameLayoutB");
        removeFragmentsInActivity(R.id.frameLayoutB,fragB);
        addFragmentsInActivity(R.id.frameLayoutA,fragB);
    }
    else{
        Log.e("check", "fragB already added");
    }    

    public void removeFragmentsInActivity(int id, Fragment fragment)
    {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.remove(fragment);
        fragmentTransaction.commit();
    }

LayoutA 框架显示为fragment_A。 当再次单击菜单操作时, 碎片_ B 会被装入 。

调试我发现, 在 frafB. isAdded () 之后, 在 frafB. remove () 操作完成后返回真 。 在第二个菜单操作 frafB. isAdded () 返回假和 frafB. add () 被执行, 碎片B 在框架LayoutA 中显示 。

据我所知, 承诺是一个 Async 操作。 添加返回是真实的, 因为承诺是 Async 且移除操作承诺直到时间被调用 rafB. isAdded () 才会完成 。 这是真的吗?

请提出解决这一问题的可能解决办法或办法。

Regards, Vibhor

最佳回答

是的,交易是非同步进行的。如果您想要确保执行 < code> isAdded 之前的所有 trasaction 都已完成, 请运行 :

getFragmentManager().executePendingTransactions();

http:// developmenter.android.com/ reference/android/app/fragmentManager.html#executePendingTransActions%28%29"\\code>executePendingTransActions () : http://developmenter.android.com/ reference/android/app/fragmentManager.html#executPendingTransActions% 28%29"\\code>executPendingTransActions () :

After a FragmentTransaction is committed with FragmentTransaction.commit(), it is scheduled to be executed asynchronously on the process s main thread. If you want to immediately executing any such pending operations, you can call this function (only from the main thread) to do so. Note that all callbacks and other related behavior will be done from within this call, so be careful about where this is called from.

所以,你的代码应该看起来像:

removeFragmentsInActivity(R.id.frameLayoutB,fragB);
addFragmentsInActivity(R.id.frameLayoutB,fragC);
getFragmentManager().executePendingTransactions();
if(!fragB.isAdded()){
        Log.e("check", "fragB already removed from frameLayoutB");
    removeFragmentsInActivity(R.id.frameLayoutA,fragA);
    addFragmentsInActivity(R.id.frameLayoutA,fragB);
}
else{
    Log.e("check", "fragB already added");
}

注 也固定清除碎片A。

问题回答

也许你可以把这种零碎的 交易 化成这样

private void commitFragmentTransaction(final FragmentTransaction ft, boolean allowStateLoss, boolean now) {
    if (ft == null || ft.isEmpty()) {
        return;
    }

    if (allowStateLoss) {
        if (now) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                ft.commitNowAllowingStateLoss();
            } else {
                ft.commitAllowingStateLoss();
                mFragmentManager.executePendingTransactions();
            }
        } else {
            ft.commitAllowingStateLoss();
        }
    } else {
        if (now) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                ft.commitNow();
            } else {
                ft.commit();
                mFragmentManager.executePendingTransactions();
            }
        } else {
            ft.commit();
        }
    }
}

承诺现在() 并承诺现在( ) 并承诺现在( ) 允许国家损失( ) 添加到 API 级别 24 中

要求承诺现在比要求承诺()然后执行交易()更可取,因为后者的副作用是试图进行所有目前尚未结账的交易,无论这是否为预期行为。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签