获取当前片段和保存的onSaveInstanceState上()方法屏幕方向(Get Current

2019-07-31 17:41发布

我有多个片段一个活动。 我也使用actionbarSherlock我的标签也连接到碎片。

我的问题是,当我将旋转屏幕(即纵向到横向/反之亦然),我的活动将被再次调用,所以它重新启动我的活动。

我想不重新启动我的活动,但只是恢复被证明是旋转前的当前片段。 请不要回答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

    }
}

Answer 1:

碎片将默认的设备旋转后可以恢复,如果你不重新添加他们。 如果你想在片段看起来是一样的,那么你应该执行你onSaveInstanceStateFragment本身。 在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. 
        */
    }
}

即使你不重写onSaveInstanceState在活动中, savedInstanceState恢复的参数时,仍然会非空Activity 。 这将只是一个空Bundle

另一种选择是存储了什么选择的选项卡索引和恢复您的活动时,重新选择该选项卡。

@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正被显示。 唯一的缺点,这是你的片段的状态不保存。 要保存片段的状态,你必须像做第一个解决方案。



文章来源: Get Current Fragment and Save on onSaveInstanceState() Method For Screen Orientation