Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I go back to parent list activity using master / detail fragments when using up navigation?

I am learning android programming, and am having problems with up navigation in master / detail fragments. For practice, I am putting together an application to track sheet music by composer for my wife's choir's library of sheet music. (Pretty esoteric, but if I can get it working, I'll have a better understanding of Android programming.)

The first screen is a listing of Composers. Selecting one from the list displays details about the composer in the detail fragment; this is working fine-and-dandy.

I have the detail fragment being created like this: (Straight from the template provided by eclipse)

//In ComposerListActivity.java
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(ComposerDetailFragment.ARG_ITEM_ID, id);
        ComposerDetailFragment fragment = new ComposerDetailFragment();
        fragment.setArguments(arguments);
        getFragmentManager()
            .beginTransaction()
            .replace(R.id.composer_detail_container, fragment)
            .commit();
    } ...
}

The Detail fragment displays information about the composer (name, birth/death dates, bio, etc.) as well a button to view the pieces composed by that composer. Pressing that button brings up another Master / Detail Activity:

    //In ComposerDetailFragment.java
    protected void viewPieces() {
        Intent intent = new Intent(this.getActivity(), PieceListActivity.class);
        intent.putExtra(COMPOSER_ID, this.dataObject.getId());
        this.getActivity().startActivityForResult(intent, VIEW_PIECES_LIST);
    }

This works fine, and shows the list of pieces written by that composer. You can select one of the pieces and it shows information about that piece (title, number of parts, key, number of pages, etc.) If I press "back" from the pieces, it takes me back to the list of composers with the previously-selected composer selected. However, if I press "up" (on the icon in the action bar), it takes me to an activity showing a blank detail fragment (the id of the composer is lost). I would expect in this case that back and up should function the same -- they both should go up one level (for the list of pieces to the list of composers). On a phone, however, it should do what it's doing (except retaining the composer id, but that is a separate issue I suspect).

//In PieceListActivity.java
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

What do I need to do to accomplish this? Should I abandon Up navigation entirely here since back seems to be doing what I want, or is there a way to get it to do what I want? None of the questions/answered I could find here seemed to address this situation, although a few were close (and I seemed to be led in circled on a few of them).

Thanks in advance.

like image 508
user2920555 Avatar asked Jan 31 '26 12:01

user2920555


1 Answers

In onItemSelected(String id) of ComposerListActivity:

if (mTwoPane) {
    Bundle args = new Bundle();
    args.putString(ComposerDetailFragment.ARG_ITEM_ID, id);
    ComposerDetailFragment fragment = new ComposerDetailFragment();
    fragment.setArguments(args);
    getFragmentManager().beginTransaction().replace(R.id.composer_detail_container, fragment).commit();
}

In viewPieces of ComposerDetailFragment:

Intent i = new Intent(getActivity(), PieceListActivity.class);
intent.putExtra(ARG_ITEM_ID, getArguments().getString(ARG_ITEM_ID));
startActivityForResult(i, VIEW_PIECES_LIST);

In onOptionsItemSelected of PieceListActivity:

case android.R.id.home:
    // Add id of detail fragment as extra to intent on task stack before navigating up
    Intent upIntent = NavUtils.getParentActivityIntent(this);
    String id = getIntent().getStringExtra(ComposerDetailFragment.ARG_ITEM_ID);
    upIntent.putExtra(ComposerDetailFragment.ARG_ITEM_ID, id);
    NavUtils.navigateUpTo(this, upIntent);
    return true;

In onCreate of ComposerListActivity:

String id = getIntent().getStringExtra(ComposerDetailFragment.ARG_ITEM_ID);
if (id != null) onItemSelected(id);
like image 195
Matt Boes Avatar answered Feb 02 '26 00:02

Matt Boes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!