1/*
2 * Copyright 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
17package com.example.android.effectivenavigation;
18
19import android.app.ActionBar;
20import android.content.Intent;
21import android.os.Bundle;
22import android.support.v4.app.Fragment;
23import android.support.v4.app.FragmentActivity;
24import android.support.v4.app.FragmentManager;
25import android.support.v4.app.FragmentStatePagerAdapter;
26import android.support.v4.app.NavUtils;
27import android.support.v4.app.TaskStackBuilder;
28import android.support.v4.view.ViewPager;
29import android.view.LayoutInflater;
30import android.view.MenuItem;
31import android.view.View;
32import android.view.ViewGroup;
33import android.widget.TextView;
34
35public class CollectionDemoActivity extends FragmentActivity {
36
37    /**
38     * The {@link android.support.v4.view.PagerAdapter} that will provide fragments representing
39     * each object in a collection. We use a {@link android.support.v4.app.FragmentStatePagerAdapter}
40     * derivative, which will destroy and re-create fragments as needed, saving and restoring their
41     * state in the process. This is important to conserve memory and is a best practice when
42     * allowing navigation between objects in a potentially large collection.
43     */
44    DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
45
46    /**
47     * The {@link android.support.v4.view.ViewPager} that will display the object collection.
48     */
49    ViewPager mViewPager;
50
51    public void onCreate(Bundle savedInstanceState) {
52        super.onCreate(savedInstanceState);
53        setContentView(R.layout.activity_collection_demo);
54
55        // Create an adapter that when requested, will return a fragment representing an object in
56        // the collection.
57        //
58        // ViewPager and its adapters use support library fragments, so we must use
59        // getSupportFragmentManager.
60        mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getSupportFragmentManager());
61
62        // Set up action bar.
63        final ActionBar actionBar = getActionBar();
64
65        // Specify that the Home button should show an "Up" caret, indicating that touching the
66        // button will take the user one step up in the application's hierarchy.
67        actionBar.setDisplayHomeAsUpEnabled(true);
68
69        // Set up the ViewPager, attaching the adapter.
70        mViewPager = (ViewPager) findViewById(R.id.pager);
71        mViewPager.setAdapter(mDemoCollectionPagerAdapter);
72    }
73
74    @Override
75    public boolean onOptionsItemSelected(MenuItem item) {
76        switch (item.getItemId()) {
77            case android.R.id.home:
78                // This is called when the Home (Up) button is pressed in the action bar.
79                // Create a simple intent that starts the hierarchical parent activity and
80                // use NavUtils in the Support Package to ensure proper handling of Up.
81                Intent upIntent = new Intent(this, MainActivity.class);
82                if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
83                    // This activity is not part of the application's task, so create a new task
84                    // with a synthesized back stack.
85                    TaskStackBuilder.from(this)
86                            // If there are ancestor activities, they should be added here.
87                            .addNextIntent(upIntent)
88                            .startActivities();
89                    finish();
90                } else {
91                    // This activity is part of the application's task, so simply
92                    // navigate up to the hierarchical parent activity.
93                    NavUtils.navigateUpTo(this, upIntent);
94                }
95                return true;
96        }
97        return super.onOptionsItemSelected(item);
98    }
99
100    /**
101     * A {@link android.support.v4.app.FragmentStatePagerAdapter} that returns a fragment
102     * representing an object in the collection.
103     */
104    public static class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
105
106        public DemoCollectionPagerAdapter(FragmentManager fm) {
107            super(fm);
108        }
109
110        @Override
111        public Fragment getItem(int i) {
112            Fragment fragment = new DemoObjectFragment();
113            Bundle args = new Bundle();
114            args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1); // Our object is just an integer :-P
115            fragment.setArguments(args);
116            return fragment;
117        }
118
119        @Override
120        public int getCount() {
121            // For this contrived example, we have a 100-object collection.
122            return 100;
123        }
124
125        @Override
126        public CharSequence getPageTitle(int position) {
127            return "OBJECT " + (position + 1);
128        }
129    }
130
131    /**
132     * A dummy fragment representing a section of the app, but that simply displays dummy text.
133     */
134    public static class DemoObjectFragment extends Fragment {
135
136        public static final String ARG_OBJECT = "object";
137
138        @Override
139        public View onCreateView(LayoutInflater inflater, ViewGroup container,
140                Bundle savedInstanceState) {
141            View rootView = inflater.inflate(R.layout.fragment_collection_object, container, false);
142            Bundle args = getArguments();
143            ((TextView) rootView.findViewById(android.R.id.text1)).setText(
144                    Integer.toString(args.getInt(ARG_OBJECT)));
145            return rootView;
146        }
147    }
148}
149