在加载ANDROID后不要重新加载片段
在我的在加载ANDROID后不要重新加载片段,android,android-fragments,Android,Android Fragments,在我的Android应用程序中,我有6个片段,单击导航抽屉中的项目时,这些片段会被一一替换。我的问题是,当单击导航抽屉项目时,片段会一次又一次地被加载。我不希望我的碎片一次又一次地重新加载。相反,我希望它们在从一个片段切换到另一个片段时保持以前的状态Addbacktostack()不起作用。请告诉我其他方法: 我的代码是: @Override public void onDrawerItemSelected(View view, int position) { row_view = v
Android
应用程序中,我有6个片段,单击导航抽屉中的项目时,这些片段会被一一替换。我的问题是,当单击导航抽屉项目时,片段会一次又一次地被加载。我不希望我的碎片一次又一次地重新加载。相反,我希望它们在从一个片段切换到另一个片段时保持以前的状态Addbacktostack()
不起作用。请告诉我其他方法:
我的代码是:
@Override
public void onDrawerItemSelected(View view, int position) {
row_view = view;
displayView(position);
}
private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment = new Fragmenta();
title = "aaaa";
toolbar_title.setText(title);
break;
case 1:
fragment = new Fragmentb();
title = "Introduction";
toolbar_title.setText(title);
break;
case 2:
fragment = new Glossary();
title = "Glossary";
toolbar_title.setText(title);
break;
case 3:
fragment = new SettingsFragment();
title = "Settings";
toolbar_title.setText(title);
break;
case 4:
fragment = new FeedbackFragment();
title = "Feedback";
toolbar_title.setText(title);
break;
case 5:
fragment = new Fragmentc();
title = "cc";
toolbar_title.setText(title);
break;
default:
break;
}
if (myfragmentstate != null) { // This is always null
//Restore the fragment's instance
fragment = getSupportFragmentManager().getFragment(myfragmentstate, "mContent");
Log.i("sadfas","fragment"+fragment);
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.container_body,fragment); //always reloads a fragment and i donot want this
fragmentTransaction.commit();
toolbar_title.setText(title);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i("MicroRAE","instanmce save"+fragment);
getSupportFragmentManager().putFragment(outState, "mContent", fragment);
}
我找到的最佳解决方案如下: onSavedInstanceState():当活动要关闭时(将活动从一个移动到另一个或配置更改),始终在片段内调用。因此,如果我们在同一活动上调用多个片段,那么我们必须使用以下方法: 使用片段的OnDestroyView()并将整个对象保存在该方法中。然后OnActivityCreated():检查对象是否为null(因为此方法每次都调用)。现在在这里恢复一个对象的状态 它总是有用的 要正确保存片段的实例状态,应执行以下操作:
希望这有帮助。我找到了一个非常好的解决方案:
String backStateName = fragment.getClass().getName();
FragmentManager fragmentManager = getSupportFragmentManager();
boolean fragmentPopped = fragmentManager.popBackStackImmediate (backStateName, 0);
if (!fragmentPopped){ //fragment not in back stack, create it.
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.container_body,fragment);
fragmentTransaction.addToBackStack(backStateName);
fragmentTransaction.commit();
toolbar_title.setText(title);
}
非常简单:只需声明一个全局字符串,保存最后加载的片段。在加载片段的方法中,只要比较是否相同即可。如果不匹配,则加载新片段,如果匹配,则不执行任何操作
String lastFragmentLoaded = null; // THIS VARIABLE IS GLOBAL
//YOUR METHOD THAT LOAD FRAGMENTS
LoadMyFragment()
{
String currentFragmentName = yourFragment.getClass().getName();
if(lastFragmentLoaded != currentFragmentName) {
//LOAD YOUR FRAGMENTS
//FINALLY, SAVE YOUR LAST FRAGMENT LOADED TO COMPARE NEXT TIME
lastFragmentLoaded = currentFragmentName;
}
else
{
// LET IT EMPTY IF YOU WANT
}
}
我必须在片段和活动中保存状态吗?SaveInstanceState在活动中始终为空??请编辑您的问题以显示您所做的更改@Shivanigupta请建议我下一步要做什么
String backStateName = fragment.getClass().getName();
FragmentManager fragmentManager = getSupportFragmentManager();
boolean fragmentPopped = fragmentManager.popBackStackImmediate (backStateName, 0);
if (!fragmentPopped){ //fragment not in back stack, create it.
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.container_body,fragment);
fragmentTransaction.addToBackStack(backStateName);
fragmentTransaction.commit();
toolbar_title.setText(title);
}
String lastFragmentLoaded = null; // THIS VARIABLE IS GLOBAL
//YOUR METHOD THAT LOAD FRAGMENTS
LoadMyFragment()
{
String currentFragmentName = yourFragment.getClass().getName();
if(lastFragmentLoaded != currentFragmentName) {
//LOAD YOUR FRAGMENTS
//FINALLY, SAVE YOUR LAST FRAGMENT LOADED TO COMPARE NEXT TIME
lastFragmentLoaded = currentFragmentName;
}
else
{
// LET IT EMPTY IF YOU WANT
}
}