Java tutorial
/* * Copyright 2015 Udacity, Inc. * * 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. */ package com.jpventura.alexandria; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.res.Configuration; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.content.LocalBroadcastManager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; import com.jpventura.alexandria.api.Callback; public class MainActivity extends ActionBarActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks, Callback { /** * Fragment managing the behaviors, interactions and presentation of the navigation drawer. */ private NavigationDrawerFragment navigationDrawerFragment; /** * Used to store the last screen title. For use in {@link #restoreActionBar()}. */ private CharSequence title; public static boolean IS_TABLET = false; private BroadcastReceiver messageReciever; public static final String MESSAGE_EVENT = "MESSAGE_EVENT"; public static final String MESSAGE_KEY = "MESSAGE_EXTRA"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); IS_TABLET = isTablet(); if (IS_TABLET) { setContentView(R.layout.activity_main_tablet); } else { setContentView(R.layout.activity_main); } messageReciever = new MessageReciever(); IntentFilter filter = new IntentFilter(MESSAGE_EVENT); LocalBroadcastManager.getInstance(this).registerReceiver(messageReciever, filter); navigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager() .findFragmentById(R.id.navigation_drawer); title = getTitle(); // Set up the drawer. navigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout)); } @Override public void onNavigationDrawerItemSelected(int position) { FragmentManager fragmentManager = getSupportFragmentManager(); Fragment nextFragment; switch (position) { default: case 0: nextFragment = new ListOfBooks(); break; case 1: nextFragment = new AddBook(); break; case 2: nextFragment = new About(); break; } fragmentManager.beginTransaction().replace(R.id.container, nextFragment).addToBackStack((String) title) .commit(); } public void setTitle(int titleId) { title = getString(titleId); } public void restoreActionBar() { ActionBar actionBar = getSupportActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); actionBar.setDisplayShowTitleEnabled(true); actionBar.setTitle(title); } @Override public boolean onCreateOptionsMenu(Menu menu) { if (!navigationDrawerFragment.isDrawerOpen()) { // Only show items in the action bar relevant to this screen // if the drawer is not showing. Otherwise, let the drawer // decide what to show in the action bar. getMenuInflater().inflate(R.menu.main, menu); restoreActionBar(); return true; } return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { startActivity(new Intent(this, SettingsActivity.class)); return true; } return super.onOptionsItemSelected(item); } @Override protected void onDestroy() { LocalBroadcastManager.getInstance(this).unregisterReceiver(messageReciever); super.onDestroy(); } @Override public void onItemSelected(String ean) { Bundle args = new Bundle(); args.putString(BookDetail.EAN_KEY, ean); BookDetail fragment = new BookDetail(); fragment.setArguments(args); int id = R.id.container; if (findViewById(R.id.right_container) != null) { id = R.id.right_container; } getSupportFragmentManager().beginTransaction().replace(id, fragment).addToBackStack("Book Detail").commit(); } private class MessageReciever extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getStringExtra(MESSAGE_KEY) != null) { Toast.makeText(MainActivity.this, intent.getStringExtra(MESSAGE_KEY), Toast.LENGTH_LONG).show(); } } } public void goBack(View view) { getSupportFragmentManager().popBackStack(); } private boolean isTablet() { return (getApplicationContext().getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE; } @Override public void onBackPressed() { if (getSupportFragmentManager().getBackStackEntryCount() < 2) { finish(); } super.onBackPressed(); } }