2

必要なビューを選択できる ActionBar を持つアクティビティを作成しようとしています。ナビゲーション ドロップダウン スピナーのクラスに拡張するために、ほとんどの ActionBar 固有のコードを含む基本アクティビティ クラスを作成しました。これが基本クラスのコードです。


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);
            }

        }

}

このクラスを継承するアクティビティの 1 つを次に示します。


/**
 * 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; } }

どうすればこれをより効率的に行うことができますか? 現在、Activites の 1 つを開始すると、初期化ビットがループします。メニューボタンを押すまで、同じアクティビティをループで作成し続けます。

4

1 に答える 1

0

このようなもの:

    @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);
        }

    }
于 2014-06-26T14:16:54.707 に答える