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