本文介绍了如何preserve片段娱乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现与碎片和RadioGroup中一个标签栏

I'm trying to implement a tab bar with fragments and RadioGroup

我切换样片段在这样的RadioGroup中签变化(看到SDK中的例子是这样的)

i switch fragments like on checked Change of radiogroup like this (saw something like this in sdk examples)

公共区域

 void onCheckedChanged(RadioGroup radioGroup, int id) {
        TabInfo newTab = mContent.get(id);

        if (newTab != lastTab) {
            FragmentTransaction transaction = mActivity.getSupportFragmentManager().beginTransaction();
            if (lastTab != null && lastTab.fragment != null) {
                transaction.detach(lastTab.fragment);
            }
            if (newTab.fragment == null) {
                newTab.fragment = Fragment.instantiate(mActivity, newTab.getTag());
                transaction.add(mContainerId, newTab.fragment);
            } else {
                transaction.attach(newTab.fragment);
            }
            lastTab = newTab;
            transaction.setCustomAnimations(R.anim.tab_transaction, R.anim.tab_transaction);
            transaction.commit();
        }
    }

但每次这种事情发生连接片段从头开始创建时即所谓的onCreate等..

but every time this happen attached fragment is created from scratch i.e. called onCreate and so on..

有没有什么办法preserve片段从活动中创建了一遍又一遍?
我也不想后退按钮可以切换回片段;

is there any way to preserve fragments from creating over and over again within an activity?also i don't want the back button could switch fragments back;

推荐答案

一href=\"http://developer.android.com/reference/android/app/FragmentTransaction.html#attach%28android.app.Fragment%29\">FragmentTransaction.attach()和FragmentTransaction.detach()你可以使用FragmentTransaction.show()和FragmentTransaction.hide().你可能还需要改变一些周围$ C $的c您在上面的例子给了,但我会离开,作为一个练习你的好自我。

Instead of using the methods FragmentTransaction.attach() and FragmentTransaction.detach() you could use FragmentTransaction.show() and FragmentTransaction.hide(). You would need to also alter some of the surrounding code you gave in the example above but I'll leave that as an exercise for your good-self.

这篇关于如何preserve片段娱乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 05:13