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