Umgang mit setDisplayHomeAsUpEnabled mit Fragmenten

Habe ich ein fragment-basiertes layout mit zwei ListFragments (A und B), beide enthalten in einer Aktivität (genannt ListingActivity 1). Wenn die app gestartet wird, ListingActivity 1 aufgerufen wird und abhängig davon, ob das Gerät im hoch-oder Querformat, entweder ListFragment A angezeigt wird oder nur beiden ListFragment s angezeigt werden.

Wenn Sie auf ein Element im Fragment A ListView, Fragment B der ListView angezeigt. Wenn Sie auf ein Element in Fragment B ListView-es geht um eine neue Aktivität (Aktivität 1).

Ich bin mit diesem code (genannt ListingActivity 2), um zu bestimmen, ob ListFragment B auf eigenen oder zusammen mit 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 der Aktivität 1, ich bin mit setDisplayHomeAsUpEnabled aktivieren Sie das Actionbar-logo als eine Schaltfläche "zurück", aber ich bin mir nicht sicher, wie Sie Sie zu behandeln, die Zuhause Absicht. Wenn sich das Gerät im portrait-Modus, die Benutzer sollten gehen Sie zurück zu ListingActivity 2, aber, wenn Sie in landscape-Modus, Sie sollten gehen Sie zurück zu ListingActivity 1.

Ich würde so etwas tun, aber es scheint wirklich 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);
    }
}
InformationsquelleAutor Kris B | 2012-04-22
Schreibe einen Kommentar