I have a fragment-based layout with two ListFragments (A and B) that are contained in an activity (called ListActivity 1). When the application starts, ListActivity 1 is called and depending on whether the device is in portrait or landscape view, only ListFragment A is displayed or both ListFragment lists are displayed.
When you click on an item in the Fragment ListView, then the Fragment B ListView is displayed. When you click on an item in the Fragment B ListView, go to the new action (Activity 1).
I use this code (called ListActivity 2) to determine whether to show ListFragment B on it alone or along with ListFragment A:
public class ListingActivity extends SherlockFragmentActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
finish();
return;
}
if (savedInstanceState == null) {
final ListingFragment details = new ListingFragment();
details.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
}
}
}
1 setDisplayHomeAsUpEnabled, Actionbar "", , . , ListActivity 2, , ListActivity 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);
}
}