1 /*
2  * Copyright (C) 2012 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 package com.example.android.fragments;
17 
18 import android.support.v4.app.Fragment;
19 import android.os.Bundle;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.TextView;
24 
25 public class ArticleFragment extends Fragment {
26     final static String ARG_POSITION = "position";
27     int mCurrentPosition = -1;
28 
29     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)30     public View onCreateView(LayoutInflater inflater, ViewGroup container,
31         Bundle savedInstanceState) {
32 
33         // If activity recreated (such as from screen rotate), restore
34         // the previous article selection set by onSaveInstanceState().
35         // This is primarily necessary when in the two-pane layout.
36         if (savedInstanceState != null) {
37             mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
38         }
39 
40         // Inflate the layout for this fragment
41         return inflater.inflate(R.layout.article_view, container, false);
42     }
43 
44     @Override
onStart()45     public void onStart() {
46         super.onStart();
47 
48         // During startup, check if there are arguments passed to the fragment.
49         // onStart is a good place to do this because the layout has already been
50         // applied to the fragment at this point so we can safely call the method
51         // below that sets the article text.
52         Bundle args = getArguments();
53         if (args != null) {
54             // Set article based on argument passed in
55             updateArticleView(args.getInt(ARG_POSITION));
56         } else if (mCurrentPosition != -1) {
57             // Set article based on saved instance state defined during onCreateView
58             updateArticleView(mCurrentPosition);
59         }
60     }
61 
updateArticleView(int position)62     public void updateArticleView(int position) {
63         TextView article = (TextView) getActivity().findViewById(R.id.article);
64         article.setText(Ipsum.Articles[position]);
65         mCurrentPosition = position;
66     }
67 
68     @Override
onSaveInstanceState(Bundle outState)69     public void onSaveInstanceState(Bundle outState) {
70         super.onSaveInstanceState(outState);
71 
72         // Save the current article selection in case we need to recreate the fragment
73         outState.putInt(ARG_POSITION, mCurrentPosition);
74     }
75 }