我用以下(和工作!)代码切换我的片段:
Fragment fragment = new DashboardFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.main_fragment, fragment);
fragmentTransaction.commit();
但是,当我调用此代码时(通常是因为它在导航栏中),会调用onCreateView
函数。
由于new DashboardFragment();
,我可以想象它,但我不知道我能改变什么。
是否可以一次创建片段并在下次访问它?
谢谢!
答案 0 :(得分:1)
您可以使用Fragment
找到之前添加的FragmentManager
当您第一次添加Fragment
时,请为其添加标记,以便日后检索。这是这样做的:
fragmentTransaction.replace(R.id.main_fragment, fragment, "my_fragment_tag");
然后,只要您想获得fragment
,只需使用以下代码:
FragmentManager fragmentManager = getSupportFragmentManager();
DashboardFragment dashboard = fragmentManager.findFragmentByTag("my_fragment_tag");
if(dashboard) {
//Do whatever you want with the fragment
} else {
dashboard = new DashboardFragment();
}
答案 1 :(得分:1)
无论您如何尝试,在替换onCreateView
后始终会调用Fragment
方法,此方法会自动调用,因为它是lifecycle’s Fragment
也就是说,您可以保存Fragment
变量值来保存静态实例并通过静态初始化方法调用它:
private static SameInstanceFragment sameInstanceFragment;
public static SameInstanceFragment getInstance() {
sameInstanceFragment = sameInstanceFragment == null ? new SameInstanceFragment() : sameInstanceFragment;
return sameInstanceFragment;
}
在下一个示例中,您将看到每次实例化的Fragment
如何无法保存其值,而已保存为唯一实例的那个可以执行此操作:
// SameInstanceFragment
public class SameInstanceFragment extends Fragment {
private static SameInstanceFragment sameInstanceFragment;
private int mCounter;
public static SameInstanceFragment getInstance() {
sameInstanceFragment = sameInstanceFragment == null ? new SameInstanceFragment() : sameInstanceFragment;
return sameInstanceFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mCounter++;
return inflater.inflate(R.layout.test_fragment, null);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String feedback = " Member counter: " + mCounter;
Toast.makeText(getActivity(), feedback, Toast.LENGTH_SHORT).show();
//member variables preserve its value after replace
}
}
// NewInstanceFragment
public class NewInstanceFragment extends Fragment {
private int mCounter;
public static NewInstanceFragment getInstance() {
return new NewInstanceFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mCounter++;
return inflater.inflate(R.layout.test_fragment, null);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String feedback = " Member counter: " + mCounter;
Toast.makeText(getActivity(), feedback, Toast.LENGTH_SHORT).show();
//member variables don’t preserve its value after replace
}
}
// TestFragmentActivity
public class TestFragmentActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.test_fragments_activity);
switchFragment();
setListener();
}
private void setListener() {
findViewById(R.id.tb_switch_fragment).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switchFragment();
}
});
}
private void switchFragment() {
Fragment fragmentDisplayed = getCurrentFragmentDisplayed();
Fragment fragmentToSwitch = null;
if (fragmentDisplayed == null)
fragmentToSwitch = SameInstanceFragment.getInstance();
else if(fragmentDisplayed instanceof SameInstanceFragment)
fragmentToSwitch = NewInstanceFragment.getInstance();
else
fragmentToSwitch = SameInstanceFragment.getInstance();
setFragment(fragmentToSwitch);
}
private void setFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fl_container, fragment);
fragmentTransaction.commit();
}
public Fragment getCurrentFragmentDisplayed(){
List<Fragment> fragments = getSupportFragmentManager().getFragments();
if(fragments == null) return null;
for (Fragment fragment : fragments) {
if(fragment.isVisible()) return fragment;
}
return null;
}
}
答案 2 :(得分:0)
当然,创建一个成员变量和一个检索它的方法,如下所示:
private DashboardFragment fragment;
private DashboardFragment getFragment() {
if (fragment == null)
fragment = new DashboardFragment();
return fragment;
}
然后,当您想要访问该片段时,请使用new DashboardFragment()
而不是getFragment()
。