我有兩個片段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) {
}
它的工作原理。謝謝btw .. –