I'm very new to Androind, trying to figure how fragment and activity should be work together. I have a very ugly layout. 1 activity and 1 "root" fragment. When user click on left menu fragment are replaced by fragment manager.
expandableList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
LeftMenuItem group = groups.get(i);
String fragmentTag = group.getFragmentTag();
if (fragmentTag.equals(Fragment1.TAG)) {
Fragment1 fragment = (Fragment1) currentFragmentManager.findFragmentByTag(Fragment1.TAG);
if (fragment == null) {
fragment = new Fragment1();
}
FragmentTransaction ft = currentFragmentManager.beginTransaction();
ft.replace(R.id.root_frame, fragment, Fragment1.TAG);
ft.commitAllowingStateLoss();
} else if (fragmentTag.equals(Fragment2.TAG)) {
I assume code above is supposed to replace current fragment with new one. Fragments are always null actually. I do not know why.
In onCreateView of RootFragment Fragment1 is created by default.
if (savedInstanceState == null) {
Log.d(TAG, "savedInstanceState is null, creating Framgent1");
Fragment1 fragment = new Fragment1();
FragmentTransaction ft = currentFragmentManager.beginTransaction();
ft.replace(R.id.root_frame, fragment, Fragment1.TAG);
ft.commitAllowingStateLoss();
}
In onCreateView
of rootFragment, rootFragment replaces itself with another Fragment1, which looks very ugly for me. Is it well known Androind pattern or just bad design ?
Let's assume that'm sending httpRequest from onCreateView of MyActivity using Volley. Once I received response I need to update Fragment1 UI from callback. How can I do it ?
Should I try to find fragment using findFragmentByTag in my activity and update UI directly ? Is http volley response is in the same thread ? If no, it is ok to update UI from different thread ?
Should I use a Handler class to send message from Activity to Fragment ?