2 つの ListFragments (A と B) を持つフラグメント ベースのレイアウトがあり、どちらもアクティビティ (ListingActivity 1 と呼ばれます) に含まれています。アプリが起動すると、ListingActivity 1 が呼び出され、デバイスが縦向きか横向きかによって、ListFragment A のみが表示されるか、両方の ListFragment が表示されます。
フラグメント A の ListView の項目をクリックすると、フラグメント B の ListView が表示されます。フラグメント B の ListView 内の項目をクリックすると、新しいアクティビティ (アクティビティ 1) に移動します。
このコード (ListingActivity 2 と呼ばれる) を使用して、ListFragment B を単独で表示するか、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();
}
}
}
アクティビティ 1 ではsetDisplayHomeAsUpEnabled
、Actionbar のロゴを戻るボタンとして有効にするために使用していますが、ホーム インテントを処理する方法がわかりません。デバイスが縦向きモードの場合、ユーザーは ListingActivity 2 に戻る必要がありますが、横向きモードの場合は ListingActivity 1 に戻る必要があります。
私はこのようなことをするつもりでしたが、それは本当にハッキーなようです:
@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);
}
}