Last active
August 29, 2015 14:11
-
-
Save koalahamlet/89343b9ae008c9f2beaf to your computer and use it in GitHub Desktop.
Fragments, how do they work?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) { | |
FragmentManager manager = getFragmentManager(); | |
FragmentTransaction fts = manager | |
.beginTransaction(); | |
// lazy instantiation for the win | |
if (tab.getTag() == "PoiListFragment") { | |
if (lFrag == null) { | |
lFrag = new PoiListFragment(); | |
} | |
fts.replace(R.id.frameContainer, lFrag, "HTL"); | |
fts.commit(); | |
} else { | |
fts.replace(R.id.frameContainer, mMapFragment, "MF"); | |
fts.commit(); | |
manager.executePendingTransactions(); | |
if (mMapFragment != null) { | |
myMap = mMapFragment.getMap(); | |
if (myMap != null) { | |
myMap.setMyLocationEnabled(true); | |
} else { | |
Toast.makeText(this, "Error - Map was null!!", | |
Toast.LENGTH_SHORT).show(); | |
} | |
} else { | |
Toast.makeText(this, "Error - Map Fragment was null!!", | |
Toast.LENGTH_SHORT).show(); | |
} | |
if (isGooglePlayServicesAvailable()) { | |
mLocationClient.connect(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//in onCreate, I call this | |
private void makeFragments() { | |
mMapFragment = new MapFragment(); | |
if (lFrag == null) { | |
lFrag = new PoiListFragment(); | |
lFrag.setRetainInstance(true); | |
} | |
} | |
///////////////////////////////////////////// | |
@Override | |
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) { | |
FragmentManager manager = getFragmentManager(); | |
FragmentTransaction fts = manager | |
.beginTransaction(); | |
// lazy instantiation for the win | |
if (tab.getTag() == "PoiListFragment") { | |
if (mMapFragment.isAdded()) { | |
fts.hide(mMapFragment); | |
fts.show(lFrag); | |
} else { | |
lFrag = new PoiListFragment(); | |
fts.add(R.id.frameContainer, lFrag, "HTL"); | |
} | |
fts.commit(); | |
} else { | |
mMapFragment.setRetainInstance(true); | |
fts.hide(lFrag); | |
if (mMapFragment.isAdded()) { | |
fts.show(mMapFragment); | |
} else { | |
fts.add(R.id.frameContainer, mMapFragment, "MF"); | |
} | |
fts.commit(); | |
manager.executePendingTransactions(); | |
// mMapFragment = ((SupportMapFragment) | |
// getSupportFragmentManager().findFragmentById(R.id.map)); | |
if (mMapFragment != null) { | |
myMap = mMapFragment.getMap(); | |
if (myMap != null) { | |
myMap.setMyLocationEnabled(true); | |
} else { | |
Toast.makeText(this, "Error - Map was null!!", | |
Toast.LENGTH_SHORT).show(); | |
} | |
} else { | |
Toast.makeText(this, "Error - Map Fragment was null!!", | |
Toast.LENGTH_SHORT).show(); | |
} | |
if (isGooglePlayServicesAvailable()) { | |
// Toast.makeText(this, "Connected to location client", | |
// Toast.LENGTH_SHORT).show(); | |
mLocationClient.connect(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment