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
17package com.example.android.supportv4.app;
18
19import android.os.Bundle;
20import android.support.v4.app.Fragment;
21import android.support.v4.app.FragmentActivity;
22import android.support.v4.app.FragmentManager;
23import android.support.v4.app.FragmentTransaction;
24import android.support.v4.content.ContextCompat;
25import android.support.v4.view.ViewCompat;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.view.View.OnClickListener;
29import android.view.ViewGroup;
30import android.widget.Button;
31import android.widget.TextView;
32
33import com.example.android.supportv4.R;
34
35public class FragmentStackSupport extends FragmentActivity {
36    int mStackLevel = 1;
37
38    @Override
39    protected void onCreate(Bundle savedInstanceState) {
40        super.onCreate(savedInstanceState);
41        setContentView(R.layout.fragment_stack);
42
43        // Watch for button clicks.
44        Button button = findViewById(R.id.new_fragment);
45        button.setOnClickListener(new OnClickListener() {
46            @Override
47            public void onClick(View v) {
48                addFragmentToStack();
49            }
50        });
51        button = findViewById(R.id.home);
52        button.setOnClickListener(new OnClickListener() {
53            @Override
54            public void onClick(View v) {
55                // If there is a back stack, pop it all.
56                FragmentManager fm = getSupportFragmentManager();
57                if (fm.getBackStackEntryCount() > 0) {
58                    fm.popBackStack(fm.getBackStackEntryAt(0).getId(),
59                            FragmentManager.POP_BACK_STACK_INCLUSIVE);
60                }
61            }
62        });
63
64        if (savedInstanceState == null) {
65            // Do first time initialization -- add initial fragment.
66            Fragment newFragment = CountingFragment.newInstance(mStackLevel);
67            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
68            ft.add(R.id.simple_fragment, newFragment).commit();
69        } else {
70            mStackLevel = savedInstanceState.getInt("level");
71        }
72    }
73
74    @Override
75    public void onSaveInstanceState(Bundle outState) {
76        super.onSaveInstanceState(outState);
77        outState.putInt("level", mStackLevel);
78    }
79
80//BEGIN_INCLUDE(add_stack)
81    void addFragmentToStack() {
82        mStackLevel++;
83
84        // Instantiate a new fragment.
85        Fragment newFragment = CountingFragment.newInstance(mStackLevel);
86
87        // Add the fragment to the activity, pushing this transaction
88        // on to the back stack.
89        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
90        ft.replace(R.id.simple_fragment, newFragment);
91        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
92        ft.addToBackStack(null);
93        ft.commit();
94    }
95//END_INCLUDE(add_stack)
96
97//BEGIN_INCLUDE(fragment)
98    public static class CountingFragment extends Fragment {
99        int mNum;
100
101        /**
102         * Create a new instance of CountingFragment, providing "num"
103         * as an argument.
104         */
105        static CountingFragment newInstance(int num) {
106            CountingFragment f = new CountingFragment();
107
108            // Supply num input as an argument.
109            Bundle args = new Bundle();
110            args.putInt("num", num);
111            f.setArguments(args);
112
113            return f;
114        }
115
116        /**
117         * When creating, retrieve this instance's number from its arguments.
118         */
119        @Override
120        public void onCreate(Bundle savedInstanceState) {
121            super.onCreate(savedInstanceState);
122            mNum = getArguments() != null ? getArguments().getInt("num") : 1;
123        }
124
125        /**
126         * The Fragment's UI is just a simple text view showing its
127         * instance number.
128         */
129        @Override
130        public View onCreateView(LayoutInflater inflater, ViewGroup container,
131                Bundle savedInstanceState) {
132            View v = inflater.inflate(R.layout.hello_world, container, false);
133            View tv = v.findViewById(R.id.text);
134            ((TextView) tv).setText("Fragment #" + mNum);
135            ViewCompat.setBackground(tv,
136                    ContextCompat.getDrawable(getContext(), android.R.drawable.gallery_thumb));
137            return v;
138        }
139    }
140//END_INCLUDE(fragment)
141}
142