Android Fragment - move from one View to another?

后端 未结 6 511
有刺的猬
有刺的猬 2020-12-16 18:04

Can i first add a Fragment to a View, then \"detach\" it, and then \"re-attach\" it to another View?

In code, i want to:

fragOne one = new fragOne();         


        
6条回答
  •  南方客
    南方客 (楼主)
    2020-12-16 18:35

    after trying all the answers from similar questions, looks like i've found a way to do the trick.

    First issue - you really have to commit and execute remove transaction before trying to add fragment to another container. Thanks for that goes to nave's answer

    But this doesn't work every time. The second issue is a back stack. It somehow blocks the transaction.

    So the complete code, that works for me looks like:

    manager.popBackStackImmediate(null, manager.POP_BACK_STACK_INCLUSIVE);
    manager.beginTransaction().remove(detailFragment).commit();
    manager.executePendingTransactions();
    manager.beginTransaction()
        .replace(R.id.content, masterFragment, masterTag)
        .add(R.id.detail, detailFragment, activeTag)
        .commit();              
    

提交回复
热议问题