如何处理屏幕旋转并在Fragment中保存数据? [英] How to handle rotate screen and save data in Fragment?
问题描述
我有3个片段(片段首页,片段A,片段B和片段C).首次运行应用程序时将显示"Fragment Home"(在主活动"中设置).从导航绘图项目可以选择每个片段.每个选定的项目都会显示详细的片段.
I have 3 Fragment (Fragment Home, Fragment A, Fragment B and Fragment C). First time app run will display Fragment Home (Set in Mainactivity). From Navigation Draw Item can choose every fragment. Every selected item will display detail Fragment.
我在处理数据和保留片段时遇到问题:
(1).当我选择一个片段(例如Fragment A)时,将显示Fragment A的页面.但是,当我旋转设备时,为什么我的片段回到Fragment Home而不停留在当前的Fragment上?如何处理?
(2).在片段B中,我在GridView中显示图像.但是,当我旋转设备时,为什么我的片段返回到Fragment Home而不停留在当前的Fragment上?如何处理它并仍然使用现有数据显示该片段?
I have problems to handle data and retain fragment :
(1). When I select a fragment (for example Fragment A) will show the page of Fragment A. But when I rotate the device, why my fragment back to Fragment Home and not stay at current Fragment ??How to handle it ?
(2). In Fragment B, I show image in GridView. But when I rotate the device, why my fragment back to Fragment Home and not stay at current Fragment ??How to handle it and still display this fragment with existing Data?
这是我的代码:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private static final String LOG_TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar 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 = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
displaySelectedItem(R.id.nav_home);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
displaySelectedItem(item.getItemId());
return true;
}
private void displaySelectedItem (int itemId) {
Fragment fragment = null;
switch (itemId){
case R.id.nav_home:
fragment = new FragmentHome();
break;
case R.id.nav_a:
fragment = new FragmentA();
break;
case R.id.nav_b:
fragment = new FragmentB();
break;
case R.id.nav_c:
fragment = new FragmentC();
break;
case R.id.nav_d:
fragment = new FragmentD();
break;
}
FragmentManager fragmentManager = MainActivity.this.getSupportFragmentManager();
List<Fragment> fragments = fragmentManager.getFragments();
if (fragments != null) {
for(Fragment f : fragments){
fragmentManager.popBackStack();
}
}
//replace the fragment
if (fragment != null) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment, "TAG_FRAGMENT");
fragmentTransaction.commit();
}
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.closeDrawer(GravityCompat.START);
}
片段A:
public class FragmentA extends Fragment {
public FragmentA() {
super();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_a, container, false);
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("Fragment A");
}
}
片段B:
public class FragmentB extends Fragment {
private static final String LOG_TAG = FragmentB.class.getSimpleName();
private ImageAdapter imageAdapter;
private ArrayList<Movie> movieList;
public FragmentNowPlaying() {
super();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_b, container, false);
GridView gridView = (GridView) rootView.findViewById(R.id.gridviewNowPlaying);
imageAdapter = new ImageAdapter(getContext(), R.layout.fragment_b, movieList);
if (savedInstanceState == null) {
movieList = new ArrayList<Movie>();
}else{
movieList = (ArrayList<Movie>) savedInstanceState.get("MovieList");
}
gridView.setAdapter(imageAdapter);
return rootView;
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putParcelableArrayList("MovieList",movieList);
super.onSaveInstanceState(outState);
}
推荐答案
解决了此问题.在MainActivity中添加以下内容:
Solved this issue. In MainActivity add this :
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION, 0);
Menu menu = navigationView.getMenu();
menu.getItem(mCurrentSelectedPosition).setChecked(true);
}
在每个片段中添加条件以检查savedInstanceState是否为空.
In every fragment add condition to check savedInstanceState null or not.
谢谢大家
这篇关于如何处理屏幕旋转并在Fragment中保存数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!