如何在现有片段中获取捆绑

时间:2018-07-13 12:39:20

标签: android android-fragments

在不重新创建现有片段的情况下将其传递给我之后如何获取它。我只是显示和隐藏片段。在onResume()之后,方法transaction.commitNow()不调用。我曾在片段中考虑过方法update(),但无法在Android Fragment.class中创建它。我该如何通知我现有的片段,该捆绑包已通过?

我通过捆绑软件的导航器方法

导航器:

  @PublishedApi
internal fun goTo(tag: String,
                  keepState: Boolean,
                  withCustomAnimation: Boolean,
                  args: Bundle,
                  backStrategy: BackStrategy
                  ){
    if(activeTag == tag){
        return
    }

    if(!fragmentMap.containsKey(tag) || !keepState) {
        val fragment = Fragment.instantiate(activity, tag)
        if (!args.isEmpty) {
            fragment.arguments = args
        }

        if (!keepState) {
            val weakFragment = fragmentManager.findFragmentByTag(tag)
            weakFragment?.let {
                fragmentManager.inTransaction {
                    remove(weakFragment)
                }
            }
        }
        fragmentManager.inTransaction {
            addOpenAnimation(this, withCustomAnimation)
            add(containerId, fragment, tag)
        }
        fragmentMap.put(tag, Screen(fragment, backStrategy))

        if (activeTag == null) {
            rootTag == tag
        }
    }else{
        val fragment = fragmentMap[tag]?.fragment
        if(!args.isEmpty){
            fragment?.arguments = args
        }
        fragmentMap.put(tag, Screen(fragment!!, backStrategy))

        if (activeTag == null) {
            rootTag == tag
        }
    }
    fragmentManager.inTransaction {
        addOpenAnimation(this, withCustomAnimation)
        fragmentMap
                .filter {
                    it.key != tag
                }
                .forEach {
                    hide(it.value.fragment)
                }
        show(fragmentMap[tag]?.fragment)
        commit()
    }
    activeTag = tag
    invokeFragmentChangeListener(tag)

    fragmentMap.replaceValue(tag, fragmentMap[tag])


}

1 个答案:

答案 0 :(得分:0)

您可以将一个片段捆绑在一起:

@Override
public void onCreate(Bundle savedInstanceState) {
    Bundle bundle = getArguments();
    ....
}

或只在片段中调用您自己的方法:

public void notifyBundleUpdated() {
    Bundle bundle = getArguments();
    ....
}