コード例 #1
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "setting selected id from intent");
    setSelectedItemIDFromIntent();
    ActiveUser activeUser = ((GiveOrTakeApplication) getApplication()).getActiveUser();
    if (activeUser == null || activeUser.getUserID() == null) {
      Intent logoutIntent = new Intent(this, LoginActivity.class);
      logoutIntent.putExtra(LoginFragment.EXTRA_LOGIN_ACTION, LoginFragment.LOGOUT);
      logoutIntent.putExtra(MainActivity.EXTRA_ITEM_ID, getSelectedItemID());
      startActivity(logoutIntent);
      finish();
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Set up the action bar.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager.setOnPageChangeListener(
        new ViewPager.SimpleOnPageChangeListener() {
          @Override
          public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
          }
        });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
      // Create a tab with text corresponding to the page title defined by
      // the adapter. Also specify this Activity object, which implements
      // the TabListener interface, as the callback (listener) for when
      // this tab is selected.
      actionBar.addTab(
          actionBar
              .newTab()
              .setText(mSectionsPagerAdapter.getPageTitle(i))
              .setIcon(mSectionsPagerAdapter.getPageIcon(i))
              .setTabListener(this));
    }

    actionBar.selectTab(actionBar.getTabAt(DEFAULT_SELECTED_TAB));

    Long itemID = getSelectedItemID();
    if (itemID != null) {
      actionBar.selectTab(actionBar.getTabAt(FREE_ITEMS_TAB));
    }
  }