避免在屏幕旋转时被替换的片段调用前一个片段
avoid a replaced fragment from calling the previous when screen is rotated
大家好,我正在尝试制作一个 activity,当抽屉中的项目被 selected 时,该框架布局会发生变化,但我的问题是当我旋转屏幕时被替换回到前一个片段。前任。我打开了应用程序,它显示了片段 A,然后我尝试从抽屉中 select 另一个片段,即片段 B,但是当我在片段 B 上尝试旋转时,它又回到片段 A。这里是我的代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
passedFragment = this.getIntent().getExtras().getString("fragmentClass");
if(savedInstanceState != null){
getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG);
}
switch (passedFragment){
case "com.serverus.oom.fragments.FragmentAgency":
fragmentClass = FragmentAgency.class;
menuItemReserve = mMenu.findItem(R.id.agency_menu_item);
break;
case "com.serverus.oom.fragments.FragmentServices2":
fragmentClass = FragmentServices2.class;
menuItemReserve = mMenu.findItem(R.id.services_menu_item);
break;
case "com.serverus.oom.fragments.FragmentContactUs":
fragmentClass = FragmentContactUs.class;
menuItemReserve = mMenu.findItem(R.id.contact_menu_item);
break;
default:
fragmentClass = FragmentAgency.class;
break;
}
fragmentReplace(fragmentClass);
}
public void fragmentReplace(Class fragmentClass) {
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = this.getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.flContent, fragment, FRAGMENT_TAG).addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE).commit();
}
提前致谢
在创建片段时使用 setRetainInstance(true)。有关详细信息,请参阅此问题 -
Further understanding setRetainInstance(true)
setRetainInstance(true) - 应在片段 activity 的 onCreate() 中提及。
另请注意
Note: Each fragment requires a unique identifier that the system can use to restore the fragment if the activity is restarted (and which you can use to capture the fragment to perform transactions, such as remove it). There are three ways to provide an ID for a fragment:
•Supply the android:id attribute with a unique ID.
•Supply the android:tag attribute with a unique string.
•If you provide neither of the previous two, the system uses the ID of the container view.
这强烈暗示如果您在 Activity.onCreated() 中执行 setContentView(R.layout.whatever) 并且该布局包含带有 setRetainInstance(true) 的片段,那么当 activity 被重新创建时将使用其 id 或标签再次搜索它。
你能展示你所有的代码吗?我的帮助可能会更好..
可能是因为您没有将 passedFragment 存储在 saveInstance 中,当您旋转屏幕时 activity 重新创建自身,passedFragment 包含 null。所以 "FragmentAgency.class" (默认值)将在旋转时始终设置。
此代码:
passedFragment = this.getIntent().getExtras().getString("fragmentClass");
。
检查:
http://developer.android.com/training/basics/activity-lifecycle/recreating.html 查看如何使用 activity 状态来保存和加载您的 activity 数据。
private static String RUN_ONCE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
passedFragment = this.getIntent().getExtras().getString("fragmentClass");
switch (passedFragment){
case "com.serverus.oom.fragments.FragmentAgency":
fragmentClass = FragmentAgency.class;
menuItemReserve = mMenu.findItem(R.id.agency_menu_item);
break;
case "com.serverus.oom.fragments.FragmentServices2":
fragmentClass = FragmentServices2.class;
menuItemReserve = mMenu.findItem(R.id.services_menu_item);
break;
case "com.serverus.oom.fragments.FragmentContactUs":
fragmentClass = FragmentContactUs.class;
menuItemReserve = mMenu.findItem(R.id.contact_menu_item);
break;
default:
fragmentClass = FragmentAgency.class;
break;
}
if(savedInstanceState == null){
fragmentReplace(fragmentClass);
}else{
getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(RUN_ONCE, true);
}
大家好,我正在尝试制作一个 activity,当抽屉中的项目被 selected 时,该框架布局会发生变化,但我的问题是当我旋转屏幕时被替换回到前一个片段。前任。我打开了应用程序,它显示了片段 A,然后我尝试从抽屉中 select 另一个片段,即片段 B,但是当我在片段 B 上尝试旋转时,它又回到片段 A。这里是我的代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
passedFragment = this.getIntent().getExtras().getString("fragmentClass");
if(savedInstanceState != null){
getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG);
}
switch (passedFragment){
case "com.serverus.oom.fragments.FragmentAgency":
fragmentClass = FragmentAgency.class;
menuItemReserve = mMenu.findItem(R.id.agency_menu_item);
break;
case "com.serverus.oom.fragments.FragmentServices2":
fragmentClass = FragmentServices2.class;
menuItemReserve = mMenu.findItem(R.id.services_menu_item);
break;
case "com.serverus.oom.fragments.FragmentContactUs":
fragmentClass = FragmentContactUs.class;
menuItemReserve = mMenu.findItem(R.id.contact_menu_item);
break;
default:
fragmentClass = FragmentAgency.class;
break;
}
fragmentReplace(fragmentClass);
}
public void fragmentReplace(Class fragmentClass) {
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = this.getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.flContent, fragment, FRAGMENT_TAG).addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE).commit();
}
提前致谢
在创建片段时使用 setRetainInstance(true)。有关详细信息,请参阅此问题 - Further understanding setRetainInstance(true)
setRetainInstance(true) - 应在片段 activity 的 onCreate() 中提及。
另请注意
Note: Each fragment requires a unique identifier that the system can use to restore the fragment if the activity is restarted (and which you can use to capture the fragment to perform transactions, such as remove it). There are three ways to provide an ID for a fragment:
•Supply the android:id attribute with a unique ID.
•Supply the android:tag attribute with a unique string.
•If you provide neither of the previous two, the system uses the ID of the container view.
这强烈暗示如果您在 Activity.onCreated() 中执行 setContentView(R.layout.whatever) 并且该布局包含带有 setRetainInstance(true) 的片段,那么当 activity 被重新创建时将使用其 id 或标签再次搜索它。
你能展示你所有的代码吗?我的帮助可能会更好..
可能是因为您没有将 passedFragment 存储在 saveInstance 中,当您旋转屏幕时 activity 重新创建自身,passedFragment 包含 null。所以 "FragmentAgency.class" (默认值)将在旋转时始终设置。
此代码:
passedFragment = this.getIntent().getExtras().getString("fragmentClass");
。
检查: http://developer.android.com/training/basics/activity-lifecycle/recreating.html 查看如何使用 activity 状态来保存和加载您的 activity 数据。
private static String RUN_ONCE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
passedFragment = this.getIntent().getExtras().getString("fragmentClass");
switch (passedFragment){
case "com.serverus.oom.fragments.FragmentAgency":
fragmentClass = FragmentAgency.class;
menuItemReserve = mMenu.findItem(R.id.agency_menu_item);
break;
case "com.serverus.oom.fragments.FragmentServices2":
fragmentClass = FragmentServices2.class;
menuItemReserve = mMenu.findItem(R.id.services_menu_item);
break;
case "com.serverus.oom.fragments.FragmentContactUs":
fragmentClass = FragmentContactUs.class;
menuItemReserve = mMenu.findItem(R.id.contact_menu_item);
break;
default:
fragmentClass = FragmentAgency.class;
break;
}
if(savedInstanceState == null){
fragmentReplace(fragmentClass);
}else{
getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(RUN_ONCE, true);
}