如何处理旋转屏幕并在Fragment中保存数据?

时间:2016-10-25 05:42:01

标签: android android-fragments fragment android-fragmentactivity screen-orientation

我有3个片段(片段主页,片段A,片段B和片段C)。首次运行应用程序将显示Fragment Home(设置为Mainactivity)。从导航绘图项可以选择每个片段。每个选定的项目都将显示细节片段。

我在处理数据和保留片段时遇到问题:

(1)。当我选择一个片段(例如Fragment A)时会显示片段A的页面。但是当我旋转设备时,为什么我的片段会回到片段主页并且不会停留在当前片段?如何处理它?<​​br/>
(2)。在片段B中,我在GridView中显示图像。但是当我旋转设备时,为什么我的片段会回到Fragment Home并且不会停留在当前片段?如何处理它并仍然用现有数据显示这个片段?

这是我的代码:

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);
}

3 个答案:

答案 0 :(得分:1)

解决了这个问题。在MainActivity中添加以下内容:

id

在每个片段中添加条件以检查savedInstanceState是否为空。

谢谢大家

答案 1 :(得分:0)

您需要覆盖片段A和B中的onConfigurationChanged(Configuration newConfig)方法,告诉他们在应用配置更改时留在那里。例如:

      @Override
        public void onConfigurationChanged(Configuration newConfig) {
                super.onConfigurationChanged(newConfig);
                // get current fragment
                Fragment fragment = mAdapter.getItem(currentItem);
                if(fragment!=null){
                     fragment.onResume();
                }
        }

答案 2 :(得分:0)

因为每次旋转屏幕都会调用onCreate()方法。

解决

onCreate()方法中的

检查 savedInstanceState()是否为空

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(null == savedInstanceState){
             //set you initial fragment object 
        }
    else
        {
        //Load the fragment by tag
        }
  }

并在所有片段中的onPause()之前保存实例状态

@Override 
protected void onSaveInstanceState(Bundle bundle1)
{ 
super.onSaveInstanceState(bundle1); Log.i(tag,"inside onSaveInstanceState");      
bundle1.putInt("tag",n); 

}