gpt4 book ai didi

android - 获取当前 fragment 并保存在屏幕方向的 onSaveInstanceState() 方法上

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:00:44 26 4
gpt4 key购买 nike

我有一个包含多个 fragment 的 Activity 。我还在我的选项卡上使用 actionbarSherlock,它也连接到 fragment 。

我的问题是当我要旋转屏幕时(即纵向到横向/反之亦然),我的 Activity 将再次被调用,因此它会重新启动我的 Activity 。

我不想重新启动我的 Activity ,而只是恢复在旋转之前显示的当前 fragment 。请不要回答 android:configChanges="orientation|keyboardHidden" 因为它没有解决问题,只是简单地破解了它。我的解决方案是 OnsaveInstanceState 和 onRestoreInstanceState 方法,但我只是不知道如何使用它来解决我的问题。请帮我解决这个问题。非常感谢任何回复。

代码:

 public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tabA = actionBar.newTab().setIcon(R.drawable.about);
ActionBar.Tab tabE = actionBar.newTab().setIcon(R.drawable.faq);
ActionBar.Tab tabB = actionBar.newTab().setIcon(R.drawable.sponsors);
ActionBar.Tab tabC = actionBar.newTab().setIcon(R.drawable.map);
ActionBar.Tab tabD = actionBar.newTab().setIcon(R.drawable.destination);
Fragment aboutTab = new PhotosActivity();
Fragment sponsorTab = new SongsActivity();
Fragment mapTab = new HCCMapActivity();
Fragment questTab = new FaqActivity();
Fragment DestinationTab = new TourActivity();
tabA.setTabListener(new MyTabsListener(aboutTab));
tabB.setTabListener(new MyTabsListener(sponsorTab));
tabC.setTabListener(new MyTabsListener(mapTab));
tabD.setTabListener(new MyTabsListener(DestinationTab));
tabE.setTabListener(new MyTabsListener(questTab));
actionBar.addTab(tabD, 0, true);
actionBar.addTab(tabC, 1, false);
actionBar.addTab(tabA, 2, false);
actionBar.addTab(tabB, 3, false);
actionBar.addTab(tabE, 4, false);
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putString("MyString", "Welcome back to Android");
//savedInstanceState.putString("id",)
// etc.
//getSupportFragmentManager().putFragment(savedInstanceState, "fragment", getSupportFragmentManager().findFragmentById(R.id.fragment_place));
}


@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
FragmentManager fragmentManager ;
FragmentTransaction ft ;
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
String myString = savedInstanceState.getString("MyString");
Log.i("Hello", myString);

fragmentManager = getSupportFragmentManager();
ft = fragmentManager.beginTransaction();
ft.setCustomAnimations(R.anim.slide_out_left, R.anim.slide_out_right);
ft.replace(R.id.fragment_place, getSupportFragmentManager().getFragment(savedInstanceState, "fragment"), null);
}

@Override
public void onConfigurationChanged (Configuration newConfig){
Log.i("hello", "Config");
super.onConfigurationChanged(newConfig);
}

@Override
public boolean onPrepareOptionsMenu (Menu menu) {
//MenuItem menuitem1 = menu.findItem(R.id.menuitem1);
//menuitem1.setVisible(false);

menu.getItem(1).setVisible(false);
menu.getItem(0).setVisible(false);
return true;
}


protected class MyTabsListener implements TabListener{

Fragment fragment;

public MyTabsListener(Fragment fragment){

this.fragment = fragment;
}

public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (myTabPosition < 0){
//ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
}else{
if (myTabPosition > tab.getPosition()){
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
}else{
ft.setCustomAnimations(R.anim.slide_out_left, R.anim.slide_out_right);
}
}
myTabPosition = tab.getPosition();
ft.replace(R.id.fragment_place, fragment, null);
//ft.commit();
}

public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.remove(fragment);
getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}

public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub

}
}

最佳答案

如果您不再次添加 fragment ,默认情况下, fragment 将在设备轮换后恢复。如果您希望 fragment 看起来相同,那么您应该在 Fragment 本身中执行您的 onSaveInstanceState。在 Activity 中,您可以执行以下操作:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState == null) {
/* First launch, add fragments */
} else {
/*
Activity restored, don't add new fragments or in your case,
don't make the first tab selected.
*/
}
}

即使您没有在 Activity 中覆盖 onSaveInstanceStatesavedInstanceState 参数在恢复 Activity 时仍将是非空的。它只是一个空的 Bundle

另一种选择是存储所选标签索引的内容,并在您的 Activity 恢复时重新选择该标签。

@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt("CurrentTab", currentTabIndex);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Your existing code */

if(savedInstanceState != null) {
int currentTab = savedInstanceState.getInt("CurrentTab", 0);
/* Set currently selected tab */
}
}

这将重新选择当前选项卡并显示正在显示的 Fragment。唯一的缺点是您的 fragment 状态未保存。要保存 fragment 的状态,您必须执行类似于第一个解决方案的操作。

关于android - 获取当前 fragment 并保存在屏幕方向的 onSaveInstanceState() 方法上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12513827/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com