Skip to content

Instantly share code, notes, and snippets.

@writtmeyer
Last active May 25, 2017 23:22
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 writtmeyer/5052512 to your computer and use it in GitHub Desktop.
Save writtmeyer/5052512 to your computer and use it in GitHub Desktop.
A very quick test case for up navigation in master detail views. I simply enhanced some of the code, the wizard creates. This is more or less the code of my blogpost "Adding ActionBarSherlock to Your Project" (http://www.grokkingandroid.com/adding-actionbarsherlock-to-your-project/). Lines 73 and 74 add an animation and line 76 adds the detail f…
package com.example.test;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.MenuItem;
/**
* An activity representing a list of Items. This activity has different
* presentations for handset and tablet-size devices. On handsets, the activity
* presents a list of items, which when touched, lead to a
* {@link ItemDetailActivity} representing item details. On tablets, the
* activity presents the list of items and item details side-by-side using two
* vertical panes.
* <p>
* The activity makes heavy use of fragments. The list of items is a
* {@link ItemListFragment} and the item details (if present) is a
* {@link ItemDetailFragment}.
* <p>
* This activity also implements the required {@link ItemListFragment.Callbacks}
* interface to listen for item selections.
*/
public class ItemListActivity extends FragmentActivity implements
ItemListFragment.Callbacks {
private static final String BACKSTACK_NAME= "detailFragment";
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
*/
private boolean mTwoPane;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
if (findViewById(R.id.item_detail_container) != null) {
// The detail container view will be present only in the
// large-screen layouts (res/values-large and
// res/values-sw600dp). If this view is present, then the
// activity should be in two-pane mode.
mTwoPane = true;
// In two-pane mode, list items should be given the
// 'activated' state when touched.
((ItemListFragment) getSupportFragmentManager().findFragmentById(
R.id.item_list)).setActivateOnItemClick(true);
}
// TODO: If exposing deep links into your app, handle intents here.
}
/**
* Callback method from {@link ItemListFragment.Callbacks} indicating that
* the item with the given ID was selected.
*/
@Override
public void onItemSelected(String id) {
if (mTwoPane) {
// In two-pane mode, show the detail view in this activity by
// adding or replacing the detail fragment using a
// fragment transaction.
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(R.animator.slide_in_left,
R.animator.slide_out_right, R.animator.slide_in_left, R.animator.slide_out_right)
.replace(R.id.item_detail_container, fragment)
.addToBackStack(BACKSTACK_NAME)
.commit();
getActionBar().setDisplayHomeAsUpEnabled(true);
} else {
// In single-pane mode, simply start the detail activity
// for the selected item ID.
Intent detailIntent = new Intent(this, ItemDetailActivity.class);
detailIntent.putExtra(ItemDetailFragment.ARG_ITEM_ID, id);
startActivity(detailIntent);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Log.v("grokkingandroid", "up selected");
Fragment f = getSupportFragmentManager().findFragmentById(R.id.item_detail_container);
getSupportFragmentManager().popBackStack(BACKSTACK_NAME, FragmentManager.POP_BACK_STACK_INCLUSIVE);
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(R.animator.slide_in_left,
R.animator.slide_out_right)
.remove(f)
.commit();
getActionBar().setDisplayHomeAsUpEnabled(false);
getActionBar().setHomeButtonEnabled(false);
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
moveTaskToBack(true);
}
getSupportFragmentManager().popBackStack(BACKSTACK_NAME, FragmentManager.POP_BACK_STACK_INCLUSIVE);
getActionBar().setDisplayHomeAsUpEnabled(false);
getActionBar().setHomeButtonEnabled(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment