Java tutorial
/** * This file is part of KeithAndTheGirl for Android * * KeithAndTheGirl for Android is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * KeithAndTheGirl for Android is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with KeithAndTheGirl for Android. If not, see <http://www.gnu.org/licenses/>. * * This software can be found at <https://github.com/dmfrey/KeithAndTheGirl/> * */ package com.keithandthegirl.ui.activity; import android.annotation.TargetApi; import android.app.ActionBar; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import com.keithandthegirl.R; import com.keithandthegirl.ui.preferences.SettingsActivity; import com.keithandthegirl.ui.preferences.SettingsActivityHC; /** * @author Daniel Frey * */ public class EpisodesActivity extends FragmentActivity { private static final String TAG = EpisodesActivity.class.getSimpleName(); private static final int SETTINGS_ID = Menu.FIRST + 10; private static final int ABOUT_ID = Menu.FIRST + 20; /* (non-Javadoc) * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle) */ @Override protected void onCreate(Bundle bundle) { Log.v(TAG, "onCreate : enter"); super.onCreate(bundle); setContentView(R.layout.activity_episodes); setupActionBar(); Log.v(TAG, "onCreate : enter"); } @TargetApi(11) @Override public boolean onCreateOptionsMenu(Menu menu) { Log.d(TAG, "onCreateOptionsMenu : enter"); MenuItem settings = menu.add(Menu.NONE, SETTINGS_ID, Menu.NONE, getResources().getString(R.string.pref_settings_title)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { settings.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); } MenuItem about = menu.add(Menu.NONE, ABOUT_ID, Menu.NONE, getResources().getString(R.string.about_header)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { about.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); } Log.d(TAG, "onCreateOptionsMenu : exit"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { Log.d(TAG, "onOptionsItemSelected : enter"); switch (item.getItemId()) { case android.R.id.home: // app icon in action bar clicked; go home Intent intent = new Intent(this, HomeActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); return true; case SETTINGS_ID: Log.d(TAG, "onOptionsItemSelected : settings selected"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { startActivity(new Intent(this, SettingsActivityHC.class)); } else { startActivity(new Intent(this, SettingsActivity.class)); } return true; case ABOUT_ID: Log.d(TAG, "onOptionsItemSelected : about selected"); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); Fragment prev = getSupportFragmentManager().findFragmentByTag("aboutDialog"); if (null != prev) { ft.remove(prev); } ft.addToBackStack(null); DialogFragment newFragment = AboutDialogFragment.newInstance(); newFragment.show(ft, "aboutDialog"); return true; } Log.d(TAG, "onOptionsItemSelected : exit"); return super.onOptionsItemSelected(item); } // internal helpers @TargetApi(11) private void setupActionBar() { Log.v(TAG, "setupActionBar : enter"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); } Log.v(TAG, "setupActionBar : exit"); } }