1 /* 2 * Copyright (C) 2011 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.newsreader; 18 19 import android.content.res.Configuration; 20 import android.os.Bundle; 21 import android.support.v4.app.FragmentActivity; 22 23 /** 24 * Activity that displays a particular news article onscreen. 25 * 26 * This activity is started only when the screen is not large enough for a two-pane layout, in 27 * which case this separate activity is shown in order to display the news article. This activity 28 * kills itself if the display is reconfigured into a shape that allows a two-pane layout, since 29 * in that case the news article will be displayed by the {@link NewsReaderActivity} and this 30 * Activity therefore becomes unnecessary. 31 */ 32 public class ArticleActivity extends FragmentActivity { 33 // The news category index and the article index for the article we are to display 34 int mCatIndex, mArtIndex; 35 36 /** 37 * Sets up the activity. 38 * 39 * Setting up the activity means reading the category/article index from the Intent that 40 * fired this Activity and loading it onto the UI. We also detect if there has been a 41 * screen configuration change (in particular, a rotation) that makes this activity 42 * unnecessary, in which case we do the honorable thing and get out of the way. 43 */ 44 @Override onCreate(Bundle savedInstanceState)45 protected void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 mCatIndex = getIntent().getExtras().getInt("catIndex", 0); 48 mArtIndex = getIntent().getExtras().getInt("artIndex", 0); 49 50 // If we are in two-pane layout mode, this activity is no longer necessary 51 if (getResources().getBoolean(R.bool.has_two_panes)) { 52 finish(); 53 return; 54 } 55 56 // Place an ArticleFragment as our content pane 57 ArticleFragment f = new ArticleFragment(); 58 getSupportFragmentManager().beginTransaction().add(android.R.id.content, f).commit(); 59 60 // Display the correct news article on the fragment 61 NewsArticle article = NewsSource.getInstance().getCategory(mCatIndex).getArticle(mArtIndex); 62 f.displayArticle(article); 63 } 64 } 65