Niezawodny sposób obsługi fragmentu przy zmianie orientacji
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
}
Mam potrzebę wyświetlania dwóch widoków listy obok siebie ,menu po lewej stronie i jego zawartość po prawej stronie,domyślnie jest zaznaczone pierwsze menu, a jego zawartość jest wyświetlana po prawej stronie.Fragment, który wyświetla zawartość jest jak poniżej
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 i onCreateView nazywa się dwa razy, istnieje wiele przykładów z wykorzystaniem fragmentów, dlatego jestem początkujący w tym segmencie nie jestem w stanie odnieść przykład z moim problemem .I need fool proof way to handle zmiana orientacji w lepszy sposób.Nie zadeklarowałem android: configChanges w pliku manifestu, muszę zniszczyć aktywność i odtworzyć, aby móc korzystać z innego układu w trybie krajobrazowym.Proszę o pomoc w obejściu problemu
2 answers
Tworzysz nowy fragment za każdym razem, gdy obracasz Ekran w swojej aktywności onCreate();
, ale zachowujesz również stare za pomocą super.onCreate(savedInstanceState);
. Więc może ustawić tag i znaleźć fragment, jeśli istnieje, lub przekazać null bundle do super.
To zajęło mi trochę czasu, aby nauczyć się i to naprawdę może być bi****, gdy pracujesz z rzeczy takich jak viewpager.
Polecam przeczytać o fragmentach dodatkowy czas, ponieważ dokładnie ten temat jest poruszany.
Oto przykład jak obsługa fragmentów przy regularnej zmianie orientacji:
Aktywność :
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);
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-10 12:18:54
Dobre wytyczne dotyczące zachowania danych między zmianami orientacji a rekreacją można znaleźć w wytycznych Androida.
Podsumowanie:
-
Spraw, aby twój fragment był możliwy do uzyskania:
setRetainInstance(true);
-
Utwórz nowy fragment tylko w razie potrzeby (lub przynajmniej weź z niego dane)
dataFragment = (DataFragment) fm.findFragmentByTag("data"); // create the fragment and data the first time if (dataFragment == null) {
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-13 15:34:17