Java tutorial
package edu.uwp.alga; /** * Copyright 2015 UW-Parkside, Harleen Kaur, Hanh Le, Francisco Mateo * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.. */ import android.content.Intent; import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.NavUtils; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import edu.uwp.alga.settings.PreferencesActivity; import edu.uwp.alga.adapter.SectionsPagerAdapter; import edu.uwp.alga.utils.SnackbarFactory; public class MainActivity extends AppCompatActivity { /** * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the * sections. We use a {@link FragmentPagerAdapter} derivative, which will keep every loaded * fragment in memory. If this becomes too memory intensive, it may be best to switch to a * {@link android.support.v4.app.FragmentStatePagerAdapter}. */ private SectionsPagerAdapter mSectionsPagerAdapter; private int REQUEST_CODE = 101; /** * The {@link ViewPager} that will host the section contents. */ private ViewPager mViewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // setToolbar(); setFragmentPagers(); setTabLayout(); } private void setFragmentPagers() { // Create the adapter that will return a fragment for each of the three // primary sections of the activity. mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.container); mViewPager.setAdapter(mSectionsPagerAdapter); Intent intent = getIntent(); if (intent.getBooleanExtra("toData", false)) { mViewPager.setCurrentItem(1); } } /** * {@inheritDoc} */ @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } /** * Handle action bar item click events. The action bar will automatically handle clicks on the * Home/Up button, so long as the parent activity is specified in the AndroidManifest.xml. * @param item The {@link MenuItem} clicked * @return boolean True on success, false on fail. */ @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_about: SnackbarFactory.create(this, findViewById(R.id.main_content), getString(R.string.action_about_pushed)) .show(); return true; case R.id.action_settings: startActivity(new Intent(this, PreferencesActivity.class)); return true; case R.id.home: NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); } /** * The {@link Toolbar} is a generalization of {@link ActionBar} for use within application layouts. */ /* private void setToolbar() { final Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mToolbar); }*/ private void setTabLayout() { final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(mViewPager); // Make it swipeable, otherwise they will be squished together. //tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); } @Override public void onBackPressed() { super.onBackPressed(); if (mViewPager.getCurrentItem() == 0) { Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//***Change Here*** startActivity(intent); finish(); System.exit(0); } } }