• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 
17 package com.example.android.apis.app;
18 
19 import com.example.android.apis.R;
20 
21 import android.app.Activity;
22 import android.app.Fragment;
23 import android.app.FragmentTransaction;
24 import android.os.Bundle;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.view.View.OnClickListener;
29 import android.widget.Button;
30 import android.widget.TextView;
31 
32 /**
33  * Demonstrates the use of custom animations in a FragmentTransaction when
34  * pushing and popping a stack.
35  */
36 public class FragmentCustomAnimations extends Activity {
37     int mStackLevel = 1;
38 
39     @Override
onCreate(Bundle savedInstanceState)40     protected void onCreate(Bundle savedInstanceState) {
41         super.onCreate(savedInstanceState);
42         setContentView(R.layout.fragment_stack);
43 
44         // Watch for button clicks.
45         Button button = (Button)findViewById(R.id.new_fragment);
46         button.setOnClickListener(new OnClickListener() {
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 = getFragmentManager().beginTransaction();
56             ft.add(R.id.simple_fragment, newFragment).commit();
57         } else {
58             mStackLevel = savedInstanceState.getInt("level");
59         }
60     }
61 
62     @Override
onSaveInstanceState(Bundle outState)63     public void onSaveInstanceState(Bundle outState) {
64         super.onSaveInstanceState(outState);
65         outState.putInt("level", mStackLevel);
66     }
67 
68 //BEGIN_INCLUDE(add_stack)
addFragmentToStack()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 = getFragmentManager().beginTransaction();
78         ft.setCustomAnimations(R.animator.fragment_slide_left_enter,
79                 R.animator.fragment_slide_left_exit,
80                 R.animator.fragment_slide_right_enter,
81                 R.animator.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          */
newInstance(int num)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
onCreate(Bundle savedInstanceState)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
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)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             tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
127             return v;
128         }
129     }
130 //END_INCLUDE(fragment)
131 }
132