Android: recover fragment after changing screen orientation

I start with fragments and I have a problem. I will not recover my fragment after rotating the screen. My application looks like this: on the terrain, I have a button in the left pane that updates the label in the right pane. If in portrait mode, I move on to a new action. However, I will not maintain the state of the fragment after rotation. The code is as follows:

Left area fragment:

public class ListFragment extends Fragment implements OnClickListener { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_list, container, false); Button button = (Button) view.findViewById(R.id.button1); button.setOnClickListener(this); return view; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: updateDetail(); break; } } public void updateDetail() { String newTime = String.valueOf(System.currentTimeMillis()); DetailFragment fragment = (DetailFragment) getFragmentManager() .findFragmentById(R.id.detailFragment); if (fragment != null && fragment.isInLayout()) { fragment.setText(newTime); } else { Intent intent = new Intent(getActivity().getApplicationContext(), DetailActivity.class); intent.putExtra("value", newTime); startActivity(intent); } } 

}

Actions in the right pane:

 public class DetailActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { finish(); return; } if (savedInstanceState == null) { setContentView(R.layout.activity_detail); Bundle extras = getIntent().getExtras(); if (extras != null) { String s = extras.getString("value"); TextView view = (TextView) findViewById(R.id.detailsText); view.setText(s); } } } 

}

Fragment of the right area:

 public class DetailFragment extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater .inflate(R.layout.fragment_detail, container, false); return view; } public void setText(String item) { TextView view = (TextView) getView().findViewById(R.id.detailsText); view.setText(item); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); FragmentManager fm = getFragmentManager(); DetailFragment fragment = (DetailFragment)fm.findFragmentById(R.id.detailFragment); if (fragment == null) { fragment = new DetailFragment(); fragment.setTargetFragment(this, 0); fm.beginTransaction().add(R.id.detailFragment, fragment).commit(); } } 

}

What can i do wrong?

+4
source share
2 answers

You have several options that you can use the Bundle to save and restore the state of fragments.

Or you can use the setRetainInstance method:

 onCreate(Bundle save) { super.onCreate(save); setRetainInstance(true); } 

Keep in mind that the setRetainInstance method does not work for fragments in the backstack.

-1
source
 @Override public void onConfigurationChanged(Configuration newConfig) { int orientation = getResources().getConfiguration().orientation; switch (orientation) { case Configuration.ORIENTATION_LANDSCAPE: getSupportFragmentManager().beginTransaction().replace(R.id.detailFragment, new DetailFragment()).commitAllowingStateLoss(); getSupportFragmentManager().beginTransaction().remove(new DetailFragment()).commitAllowingStateLoss(); break; case Configuration.ORIENTATION_PORTRAIT: getSupportFragmentManager().beginTransaction().replace(R.id.detailFragment, new DetailFragment()).commitAllowingStateLoss(); getSupportFragmentManager().beginTransaction().remove(new DetailFragment()).commitAllowingStateLoss(); break; } super.onConfigurationChanged(newConfig); } 

You can check onConfigurationChange and make sure your fragment state is not lost by running commitAllowingStateLoss

-2
source

Source: https://habr.com/ru/post/1439057/


All Articles