Android 在ActionBar上使用onNavigationItemSelected的活动循环
我一直在尝试编写一个活动,它有一个ActionBar,您可以在其中选择所需的视图。我已经创建了一个基本活动类,其中包含大多数ActionBar特定的代码,可以扩展到导航下拉微调器中的类。下面是基类的代码Android 在ActionBar上使用onNavigationItemSelected的活动循环,android,android-fragments,android-activity,android-actionbar,android-listfragment,Android,Android Fragments,Android Activity,Android Actionbar,Android Listfragment,我一直在尝试编写一个活动,它有一个ActionBar,您可以在其中选择所需的视图。我已经创建了一个基本活动类,其中包含大多数ActionBar特定的代码,可以扩展到导航下拉微调器中的类。下面是基类的代码 public class MainFlowActivity extends FragmentActivity implements ActionBar.OnNavigationListener { private static final String STATE_SELECTED_
public class MainFlowActivity extends FragmentActivity implements ActionBar.OnNavigationListener {
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
// Set up the action bar to show a dropdown list.
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
// Set up the dropdown list navigation in the action bar.
actionBar.setListNavigationCallbacks(
// Specify a SpinnerAdapter to populate the dropdown list.
new ArrayAdapter(getActionBarThemedContextCompat(),
android.R.layout.simple_spinner_dropdown_item,
android.R.id.text1, new String[] {
getString(R.string.title_section1),
getString(R.string.title_section2),
getString(R.string.title_section3), }), this);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private Context getActionBarThemedContextCompat() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
return getActionBar().getThemedContext();
} else {
return this;
}
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Restore the previously serialized current dropdown position.
if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
// Serialize the current dropdown position.
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar().getSelectedNavigationIndex());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onNavigationItemSelected(int position, long id) {
// When the given dropdown item is selected, show its contents in the
// container view.
switch (position){
case 0:
startActivity(new Intent("com.example.portarius.mainflow.ITEMLISTACTIVITY"));
return true;
case 1:
startActivity(new Intent("com.example.portarius.mainflow.TESTDUMMYLOGG"));
return true;
case 2:
startActivity(new Intent("com.example.portarius.mainflow.TESTDUMMYKARTVIEW"));
return true;
default:
return onNavigationItemSelected(position, id);
}
}
}
下面是继承此类的活动之一:
/**
* An activity representing a list of Items. This activity has different
* presentations for handset and tablet-size devices. On handsets, the activity
* presents a list of items, which when touched, lead to a
* {@link ItemDetailActivity} representing item details. On tablets, the
* activity presents the list of items and item details side-by-side using two
* vertical panes.
*
* The activity makes heavy use of fragments. The list of items is a
* {@link ItemListFragment} and the item details (if present) is a
* {@link ItemDetailFragment}.
*
* This activity also implements the required {@link ItemListFragment.Callbacks}
* interface to listen for item selections.
*/
public class ItemListActivity extends MainFlowActivity implements
ItemListFragment.Callbacks {
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
*/
private boolean mTwoPane;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
if (findViewById(R.id.item_detail_container) != null) {
// The detail container view will be present only in the
// large-screen layouts (res/values-large and
// res/values-sw600dp). If this view is present, then the
// activity should be in two-pane mode.
mTwoPane = true;
// In two-pane mode, list items should be given the
// 'activated' state when touched.
((ItemListFragment) getSupportFragmentManager().findFragmentById(
R.id.item_list)).setActivateOnItemClick(true);
}
// TODO: If exposing deep links into your app, handle intents here.
}
/**
* Callback method from {@link ItemListFragment.Callbacks} indicating that
* the item with the given ID was selected.
*/
@Override
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(ItemDetailFragment.ARG_ITEM_ID, id);
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.item_detail_container, fragment).commit();
} else {
// In single-pane mode, simply start the detail activity
// for the selected item ID.
Intent detailIntent = new Intent(this, ItemDetailActivity.class);
detailIntent.putExtra(ItemDetailFragment.ARG_ITEM_ID, id);
startActivity(detailIntent);
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我怎样才能更有效地做到这一点?现在,当我启动其中一个活动时,它会循环初始化位。它一直在循环中创建相同的活动,直到我按下菜单按钮。类似这样的内容:
@Override
public boolean onNavigationItemSelected(int position, long id) {
// When the given dropdown item is selected, show its contents in the
// container view.
Intent currentIntent = this.getIntent();
switch (position){
case 0:
if (!currentIntent.getAction().equals("com.example.portarius.mainflow.ITEMLISTACTIVITY"))
{
startActivity(new Intent("com.example.portarius.mainflow.ITEMLISTACTIVITY"));
}
return true;
case 1:
if (!currentIntent.getAction().equals("com.example.portarius.mainflow.TESTDUMMYLOGG"))
{
startActivity(new Intent("com.example.portarius.mainflow.TESTDUMMYLOGG"));
}
return true;
case 2:
if (!currentIntent.getAction().equals("com.example.portarius.mainflow.TESTDUMMYKARTVIEW"))
{
startActivity(new Intent("com.example.portarius.mainflow.TESTDUMMYKARTVIEW"));
}
return true;
default:
return onNavigationItemSelected(position, id);
}
}