1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.contactslist.ui; 18 19 import android.annotation.TargetApi; 20 import android.net.Uri; 21 import android.os.Build; 22 import android.os.Bundle; 23 import android.support.v4.app.FragmentActivity; 24 import android.support.v4.app.FragmentTransaction; 25 import android.support.v4.app.NavUtils; 26 import android.view.MenuItem; 27 28 import com.example.android.contactslist.BuildConfig; 29 import com.example.android.contactslist.util.Utils; 30 31 /** 32 * This class defines a simple FragmentActivity as the parent of {@link ContactDetailFragment}. 33 */ 34 public class ContactDetailActivity extends FragmentActivity { 35 // Defines a tag for identifying the single fragment that this activity holds 36 private static final String TAG = "ContactDetailActivity"; 37 38 @TargetApi(Build.VERSION_CODES.HONEYCOMB) 39 @Override onCreate(Bundle savedInstanceState)40 protected void onCreate(Bundle savedInstanceState) { 41 if (BuildConfig.DEBUG) { 42 // Enable strict mode checks when in debug modes 43 Utils.enableStrictMode(); 44 } 45 super.onCreate(savedInstanceState); 46 47 // This activity expects to receive an intent that contains the uri of a contact 48 if (getIntent() != null) { 49 50 // For OS versions honeycomb and higher use action bar 51 if (Utils.hasHoneycomb()) { 52 // Enables action bar "up" navigation 53 getActionBar().setDisplayHomeAsUpEnabled(true); 54 } 55 56 // Fetch the data Uri from the intent provided to this activity 57 final Uri uri = getIntent().getData(); 58 59 // Checks to see if fragment has already been added, otherwise adds a new 60 // ContactDetailFragment with the Uri provided in the intent 61 if (getSupportFragmentManager().findFragmentByTag(TAG) == null) { 62 final FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 63 64 // Adds a newly created ContactDetailFragment that is instantiated with the 65 // data Uri 66 ft.add(android.R.id.content, ContactDetailFragment.newInstance(uri), TAG); 67 ft.commit(); 68 } 69 } else { 70 // No intent provided, nothing to do so finish() 71 finish(); 72 } 73 } 74 75 @Override onOptionsItemSelected(MenuItem item)76 public boolean onOptionsItemSelected(MenuItem item) { 77 switch (item.getItemId()) { 78 case android.R.id.home: 79 // Tapping on top left ActionBar icon navigates "up" to hierarchical parent screen. 80 // The parent is defined in the AndroidManifest entry for this activity via the 81 // parentActivityName attribute (and via meta-data tag for OS versions before API 82 // Level 16). See the "Tasks and Back Stack" guide for more information: 83 // http://developer.android.com/guide/components/tasks-and-back-stack.html 84 NavUtils.navigateUpFromSameTask(this); 85 return true; 86 } 87 // Otherwise, pass the item to the super implementation for handling, as described in the 88 // documentation. 89 return super.onOptionsItemSelected(item); 90 } 91 } 92