Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StevenByle/5729753 to your computer and use it in GitHub Desktop.
Save StevenByle/5729753 to your computer and use it in GitHub Desktop.
Android: Example showing layout dependent event handling. Taking different actions for phones and tablets.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d(TAG, "onOptionsItemSelected");
switch (item.getItemId()) {
case R.id.menu_rotate:
Log.i(TAG, "onOptionsItemSelected: rotate menu item selected");
// This menu option is only provided to phone layouts, since
// tablet layouts show the image rotator at all times
// Get the parent activity's fragment manager
FragmentManager fragmentManager = getFragmentManager();
// Create the image rotator fragment and pass in arguments
ImageRotatorFragment imageRotatorFragment = ImageRotatorFragment.newInstance(mCurImageResourceId);
// Add the new fragment on top of this one, and add it to
// the back stack
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(mContainer.getId(), imageRotatorFragment, ImageRotatorFragment.class.getName());
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.addToBackStack(null);
// Commit the transaction
fragmentTransaction.commit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onImageSelected(ImageItem imageItem, int position) {
Log.d(TAG, "onImageSelected: title = " + imageItem.getTitle() + " position = " + position);
FragmentManager fragmentManager = getSupportFragmentManager();
ImageRotatorFragment imageRotatorFragment = (ImageRotatorFragment) fragmentManager.findFragmentByTag(
ImageRotatorFragment.class.getName());
// If the rotating fragment is in the current layout, update its
// selected image
if (imageRotatorFragment != null) {
imageRotatorFragment.setImageSelected(imageItem, position);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment