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 17 package com.example.android.supportv4.app; 18 19 import android.os.Bundle; 20 import android.view.LayoutInflater; 21 import android.view.View; 22 import android.view.View.OnClickListener; 23 import android.view.ViewGroup; 24 import android.widget.Button; 25 import android.widget.TextView; 26 27 import androidx.core.content.ContextCompat; 28 import androidx.core.view.ViewCompat; 29 import androidx.fragment.app.Fragment; 30 import androidx.fragment.app.FragmentActivity; 31 import androidx.fragment.app.FragmentTransaction; 32 33 import com.example.android.supportv4.R; 34 35 public class FragmentCustomAnimationSupport extends FragmentActivity { 36 int mStackLevel = 1; 37 38 @Override onCreate(Bundle savedInstanceState)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 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 = 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 */ 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 ViewCompat.setBackground(tv, 127 ContextCompat.getDrawable(getContext(), android.R.drawable.gallery_thumb)); 128 return v; 129 } 130 } 131 //END_INCLUDE(fragment) 132 } 133