2016-05-05 33 views
1

我有兩個片段MainFragment和First_Fragment。第一個片段包含一個視頻視圖,但每當我嘗試旋轉時,導航抽屜總是返回到其主片段。我怎麼能保存或恢復我的片段狀態?所以它會停止回到它的主要片段。導航抽屜改變旋轉

這是我對在MainActivity代碼,當你旋轉設備

public class MainActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 

VideoView videoView; 
NavigationView navigationView = null; 
Toolbar toolbar = null; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 


    setContentView(R.layout.activity_main); 

    MainFragment first = new MainFragment(); 
    FragmentTransaction fragmentTransaction = 
      getSupportFragmentManager().beginTransaction(); 
    fragmentTransaction.replace(R.id.content_frame, first); 
    fragmentTransaction.commit(); 
    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 



    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 



} 
    public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 

    if (id == R.id.nav_camera) { 
     // Handle the camera action 
     MainFragment first = new MainFragment(); 
     FragmentTransaction fragmentTransaction = 
       getSupportFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.content_frame, first); 

     fragmentTransaction.commit(); 

    } else if (id == R.id.nav_gallery) { 
     First_Fragment first = new First_Fragment(); 
     FragmentTransaction fragmentTransaction = 
       getSupportFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.content_frame, first); 
     fragmentTransaction.commit(); 

    } else if (id == R.id.nav_slideshow) { 

    } 

回答

3

當你不處理的活性變化方向一樣,鍵盤等它會自動重新創建活動。因此,您的片段不會保存您以前的實例。爲了解決這個問題,我只遇到兩個解決方案。

第一個已經被Zeeshan通過下面這個步驟處理您的方向變化中提到:

  1. 處理的方向性變化的AndroidManifest

    <activity 
        android:name="com.example.Activity" 
        android:label="Activity" 
        android:configChanges="keyboardHidden|orientation|screenSize" 
        android:windowSoftInputMode="stateHidden|adjustResize" 
        android:theme="@style/Theme.AppCompat.Light" /> 
    
  2. 處理該事件的onConfiguration更改方法

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
        super.onConfigurationChanged(newConfig); 
    
    } 
    

第二個是保存碎片狀態。請參考這裏https://stackoverflow.com/a/17135346/5870896

+0

它的工作原理。謝謝btw .. –

1

活動重現。你需要在你的清單,你會處理您的代碼更改方向提,從而使系統不去做這樣的:在

<activity name="MainActivity" android:configChanges="keyboardHidden|orientation|screenSize"/> 

,然後處理該事件

@Override 
public void onConfigurationChanged(Configuration newConfig) { 

} 

您活動

0

寫這篇文章manifests.xml

android:configChanges="orientation" 

這樣的: -

<activity 
     android:name=".fragmentLayouts.MainActivity" 
     android:configChanges="orientation" /> 

添加到您的Java代碼: -

@Override 
public void onConfigurationChanged(Configuration newConfig) 
{ 
    super.onConfigurationChanged(newConfig); 
    drawerListener.onConfigurationChanged(newConfig); 
} 
2

你可以在你的碎片中做setRetainInstance(true),但BottomNavigationBar將工作不正確。在這種情況下,你可以在活動中使用一些布爾標誌,並保存/帶束這樣的方式進行恢復:

在的onCreate:

if (savedInstanceState != null) { 
     isMapFragmentVisible = savedInstanceState.getBoolean("isMainVisible"); 
    } else { 
     navigator.navigateToFragment(fragmentLocation); 
    } 

在onSavedInstanceState:

outState.putBoolean("isMainVisible", isMapFragmentVisible); 

In onNavigationSelectedListener:

case R.id.bottom_navigation_map: 

      if (isMapFragmentVisible) { 
       break; 
      } else { 
       navigator.navigateToFragment(fragmentMap); 
       isMapFragmentVisible = true; 
      } 
      return true; 

     case R.id.bottom_navigation_location: 

      if (!isMapFragmentVisible) { 
       break; 
      } else { 
       navigator.navigateToFragment(fragmentLocation); 
       isMapFragmentVisible = false; 
      } 
      return true; 

對我來說它是完美的。有沒有更優雅的方式?