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.os.Bundle;
19 import android.support.v4.app.FragmentActivity;
20 import android.support.v4.app.FragmentTransaction;
21 
22 public class MainActivity extends FragmentActivity
23         implements HeadlinesFragment.OnHeadlineSelectedListener {
24 
25     /** Called when the activity is first created. */
26     @Override
onCreate(Bundle savedInstanceState)27     public void onCreate(Bundle savedInstanceState) {
28         super.onCreate(savedInstanceState);
29         setContentView(R.layout.news_articles);
30 
31         // Check whether the activity is using the layout version with
32         // the fragment_container FrameLayout. If so, we must add the first fragment
33         if (findViewById(R.id.fragment_container) != null) {
34 
35             // However, if we're being restored from a previous state,
36             // then we don't need to do anything and should return or else
37             // we could end up with overlapping fragments.
38             if (savedInstanceState != null) {
39                 return;
40             }
41 
42             // Create an instance of ExampleFragment
43             HeadlinesFragment firstFragment = new HeadlinesFragment();
44 
45             // In case this activity was started with special instructions from an Intent,
46             // pass the Intent's extras to the fragment as arguments
47             firstFragment.setArguments(getIntent().getExtras());
48 
49             // Add the fragment to the 'fragment_container' FrameLayout
50             getSupportFragmentManager().beginTransaction()
51                     .add(R.id.fragment_container, firstFragment).commit();
52         }
53     }
54 
onArticleSelected(int position)55     public void onArticleSelected(int position) {
56         // The user selected the headline of an article from the HeadlinesFragment
57 
58         // Capture the article fragment from the activity layout
59         ArticleFragment articleFrag = (ArticleFragment)
60                 getSupportFragmentManager().findFragmentById(R.id.article_fragment);
61 
62         if (articleFrag != null) {
63             // If article frag is available, we're in two-pane layout...
64 
65             // Call a method in the ArticleFragment to update its content
66             articleFrag.updateArticleView(position);
67 
68         } else {
69             // If the frag is not available, we're in the one-pane layout and must swap frags...
70 
71             // Create fragment and give it an argument for the selected article
72             ArticleFragment newFragment = new ArticleFragment();
73             Bundle args = new Bundle();
74             args.putInt(ArticleFragment.ARG_POSITION, position);
75             newFragment.setArguments(args);
76             FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
77 
78             // Replace whatever is in the fragment_container view with this fragment,
79             // and add the transaction to the back stack so the user can navigate back
80             transaction.replace(R.id.fragment_container, newFragment);
81             transaction.addToBackStack(null);
82 
83             // Commit the transaction
84             transaction.commit();
85         }
86     }
87 }