3 조각 (조각 홈, 조각 A, 조각 B 및 단편 C)이 있습니다. 앱을 처음 실행하면 Fragment Home (Mainactivity에 설정 됨)이 표시됩니다. From Drawing Draw Item은 모든 조각을 선택할 수 있습니다. 선택한 모든 항목에 세부 조각이 표시됩니다.Fragment에서 화면을 회전하고 데이터를 저장하는 방법은 무엇입니까?
I는 데이터를 처리하고 단편을 유지하는 문제를 가지고
(1). 조각을 선택하면 (예 : 조각 A) 조각 A의 페이지가 표시됩니다. 그러나 장치를 회전 할 때 조각이 원래 조각으로 돌아가고 현재 조각에 머물러 있지 않은 이유는 무엇입니까?
(2). 단편 B에서는 GridView에 이미지를 표시합니다. 그러나 장치를 회전 할 때 왜 내 조각이 조각 홈으로 돌아가고 현재 조각에는 머물러 있지 않은지 ?? 조각을 처리하고 여전히이 조각을 기존 데이터로 표시합니까? 당신은 당신의 조각 A와 B에 onConfigurationChanged(Configuration newConfig)
메소드를 오버라이드 (override) 할 필요가
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);
}
시도 설정 'setRetainInstance (참)' [정확히 retainStateTrue()에서 발생 (http://stackoverflow.com/questions/12640316/further-understanding-setretaininstancetrue) –
@EmdadHossain 이미 시도했지만 여전히 수 없습니다. 몇 가지 예를들 수 있습니까? –