Skip to content

Instantly share code, notes, and snippets.

@jalvarado91
Last active August 29, 2015 14:24
Show Gist options
  • Save jalvarado91/ec000fa81b3001fc4407 to your computer and use it in GitHub Desktop.
Save jalvarado91/ec000fa81b3001fc4407 to your computer and use it in GitHub Desktop.
...imports...
public class ResultsActivity extends AppCompatActivity implements
ResultListFragment.OnListFragmentInteractionListener,
ResultsMapFragment.OnResultsMapFragmentInteractionListener{
public static final String TAG = ResultsActivity.class.getSimpleName();
public static final int LIST_FRAGMENT = 1;
public static final int MAP_FRAGMENT = 2;
private int CURRENT_FRAGMENT = LIST_FRAGMENT;
... fields and such ...
private ResultListFragment listFragment;
private ResultsMapFragment mapFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_property_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.home_list_toolbar);
setSupportActionBar(toolbar);
... other onCreatecode...
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.results_fragment_container) != null) {
// Get Fragment from savedInstanceState
if (savedInstanceState != null) {
CURRENT_FRAGMENT = savedInstanceState.getInt("current_fragment");
listFragment = (ResultListFragment) getSupportFragmentManager().getFragment(savedInstanceState, "listFragment");
mapFragment = (ResultsMapFragment) getSupportFragmentManager().getFragment(savedInstanceState, "mapFragment");
}
// Create a new Fragment to be placed in the activity layout
listFragment = new ResultListFragment();
mapFragment = new ResultsMapFragment();
displayFragment(LIST_FRAGMENT);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("current_fragment", CURRENT_FRAGMENT);
if(listFragment.isAdded()) {
getSupportFragmentManager().putFragment(outState, "listFragment", listFragment);
}
if(mapFragment.isAdded()) {
getSupportFragmentManager().putFragment(outState, "mapFragment", mapFragment);
}
}
protected void displayFragment(int fragmentFlag) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if(fragmentFlag == MAP_FRAGMENT) {
CURRENT_FRAGMENT = MAP_FRAGMENT;
ft.add(R.id.results_fragment_container, mapFragment);
}
else {
CURRENT_FRAGMENT = LIST_FRAGMENT;
ft.add(R.id.results_fragment_container, listFragment);
}
ft.commit();
}
protected void replaceFragment(int newFragmentFlag) {
if(CURRENT_FRAGMENT == newFragmentFlag) {
return;
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if(newFragmentFlag == MAP_FRAGMENT) {
CURRENT_FRAGMENT = MAP_FRAGMENT;
ft.replace(R.id.results_fragment_container, mapFragment);
}
else if (newFragmentFlag == LIST_FRAGMENT) {
CURRENT_FRAGMENT = LIST_FRAGMENT;
ft.replace(R.id.results_fragment_container, listFragment);
}
ft.commit();
}
... other code that doesnt pertain to the fragment stuff ...
// callbacks from when menu options to switch to other fragment is called from
// within a fragment
@Override
public void onMenuMapItemSelected() {
replaceFragment(MAP_FRAGMENT);
}
@Override
public void onMenuListItemSelected() {
replaceFragment(LIST_FRAGMENT);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment