Kris B
Kris B

Reputation: 3578

Handling setDisplayHomeAsUpEnabled with fragments

I have a fragment based layout with two ListFragments (A and B), both contained in an activity (called ListingActivity 1). When the app starts, ListingActivity 1 is called and depending on if the device is in portrait or landscape, either ListFragment A is displayed only or both ListFragment's are displayed.

When you click on an item in Fragment A's ListView, Fragment B's ListView is then displayed. When you click on an item in Fragment B's ListView it go to a new activity (Activity 1).

I'm using this code (called ListingActivity 2) to determine whether to show ListFragment B on it's own or together with ListFragment A:

public class ListingActivity extends SherlockFragmentActivity  
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            // If the screen is now in landscape mode, we can show the
            // dialog in-line so we don't need this activity.
            finish();
            return;
        }

        if (savedInstanceState == null) {
            // During initial setup, plug in the details fragment.
            final ListingFragment details = new ListingFragment();
            details.setArguments(getIntent().getExtras());

            getSupportFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
        }
    }
}

In Activity 1, I'm using setDisplayHomeAsUpEnabled to enable the Actionbar logo as a back button, but I'm not sure how to handle the home intent. When the device is in portrait mode, the user should go back to ListingActivity 2, but if they are in landscape mode, they should go back to ListingActivity 1.

I was going to do something like this, but it seems really hacky:

@Override
public boolean onOptionsItemSelected(final MenuItem item) 
{
    if (item.getItemId() == android.R.id.home) 
    {
        final Intent intent;

        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
            intent = new Intent(this, ListingActivity1.class);
        else
            intent = new Intent(this, ListingActivity2.class);

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        return true;
    } else {
        return super.onOptionsItemSelected(item);
    }
}

Upvotes: 3

Views: 6688

Answers (1)

Alex Lockwood
Alex Lockwood

Reputation: 83313

Honestly, I think your solution is the correct way in implementing the described behavior. It abides by all of the Android standards with regards to up-navigation (in this case, it should act as would the "back" button, I believe). The only thing I would re-consider is your use of the Intent.FLAG_ACTIVITY_NEW_TASK flag. From the Android documentation:

A task is the sequence of activities a user follows to accomplish a goal.

and it doesn't seem like you are starting a new task, in this case.

Upvotes: 1

Related Questions