방향 변경시 Fragment를 처리하는 확실한 방법
public class MainActivity extends Activity implements MainMenuFragment.OnMainMenuItemSelectedListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
// add menu fragment
MainMenuFragment myFragment = new MainMenuFragment();
fragmentTransaction.add(R.id.menu_fragment, myFragment);
//add content
DetailPart1 content1= new DetailPart1 ();
fragmentTransaction.add(R.id.content_fragment, content1);
fragmentTransaction.commit();
}
public void onMainMenuSelected(String tag) {
//next menu is selected replace existing fragment
}
두 개의 목록보기를 나란히 표시해야합니다. 메뉴는 왼쪽에, 내용은 오른쪽에 있습니다. 기본적으로 첫 번째 메뉴가 선택되고 해당 내용이 오른쪽에 표시됩니다. 콘텐츠를 표시하는 Fragment는 다음과 같습니다.
public class DetailPart1 extends Fragment {
ArrayList<HashMap<String, String>> myList = new ArrayList<HashMap<String, String>>();
ListAdapter adap;
ListView listview;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(savedInstanceState!=null){
myList = (ArrayList)savedInstanceState.getSerializable("MYLIST_obj");
adap = new LoadImageFromArrayListAdapter(getActivity(),myList );
listview.setAdapter(adap);
}else{
//get list and load in list view
getlistTask = new GetALLListTasks().execute();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.skyview_fragment, container,false);
return v;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("MYLIST_obj", myList );
}
}
onActivityCreated 및 onCreateView는 두 번 호출 됩니다 . 조각을 사용하는 많은 예제가 있습니다. 나는이 주제의 초보자이기 때문에 예제와 내 문제를 관련시킬 수 없습니다. 방향 변경을 처리 할 수있는 어리석은 방법이 필요합니다. android:configChanges매니페스트 파일에 선언하지 않았습니다 . 가로 모드에서 다른 레이아웃을 사용할 수 있도록 활동을 파괴하고 다시 만들어야합니다.
You are creating a new fragment every time you turn the screen in your activity onCreate(); But you are also maintaining the old ones with super.onCreate(savedInstanceState);. So maybe set tag and find the fragment if it exist, or pass null bundle to super.
This took me a while to learn and it can really be a bi**** when you are working with stuff like viewpager.
I'd recommend you to read about fragments an extra time as this exact topic is covered.
Here is an example of how to handle fragments on a regular orientation change:
Activity:
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
TestFragment test = new TestFragment();
test.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().replace(android.R.id.content, test, "your_fragment_tag").commit();
} else {
TestFragment test = (TestFragment) getSupportFragmentManager().findFragmentByTag("your_fragment_tag");
}
}
}
Fragment:
public class TestFragment extends Fragment {
public static final String KEY_ITEM = "unique_key";
public static final String KEY_INDEX = "index_key";
private String mTime;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
if (savedInstanceState != null) {
// Restore last state
mTime = savedInstanceState.getString("time_key");
} else {
mTime = "" + Calendar.getInstance().getTimeInMillis();
}
TextView title = (TextView) view.findViewById(R.id.fragment_test);
title.setText(mTime);
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("time_key", mTime);
}
}
A good guideline about how to retain data between orientation changes and activity recreation can be found in android guidelines.
Summary:
make your fragment retainable:
setRetainInstance(true);Create a new fragment only if necessary (or at least take data from it)
dataFragment = (DataFragment) fm.findFragmentByTag("data"); // create the fragment and data the first time if (dataFragment == null) {
참고URL : https://stackoverflow.com/questions/13305861/fool-proof-way-to-handle-fragment-on-orientation-change
'developer tip' 카테고리의 다른 글
| Console.WriteLine ()과 Debug.WriteLine ()의 차이점은 무엇입니까? (0) | 2020.10.05 |
|---|---|
| 조각의 MapView (Honeycomb) (0) | 2020.10.05 |
| Python Pandas는 특정 열만 병합합니다. (0) | 2020.10.05 |
| angularjs e2e 각도기 테스트에서 파일을 업로드하는 방법 (0) | 2020.10.05 |
| git push 명령의 사용자 이름 및 비밀번호 (0) | 2020.10.05 |