Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2012 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.Fragment;
     22 import android.app.FragmentTransaction;
     23 import android.os.Bundle;
     24 import android.view.LayoutInflater;
     25 import android.view.View;
     26 import android.view.ViewGroup;
     27 import android.view.View.OnClickListener;
     28 import android.widget.Button;
     29 
     30 public class FragmentStackFragment extends Fragment {
     31     int mStackLevel = 1;
     32 
     33     @Override
     34     public void onCreate(Bundle savedInstanceState) {
     35         super.onCreate(savedInstanceState);
     36 
     37         if (savedInstanceState == null) {
     38             // Do first time initialization -- add initial fragment.
     39             Fragment newFragment = FragmentStack.CountingFragment.newInstance(mStackLevel);
     40             FragmentTransaction ft = getChildFragmentManager().beginTransaction();
     41             ft.add(R.id.simple_fragment, newFragment).commit();
     42         } else {
     43             mStackLevel = savedInstanceState.getInt("level");
     44         }
     45     }
     46 
     47     @Override
     48     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     49             Bundle savedInstanceState) {
     50         View v = inflater.inflate(R.layout.fragment_stack, container, false);
     51 
     52         // Watch for button clicks.
     53         Button button = (Button)v.findViewById(R.id.new_fragment);
     54         button.setOnClickListener(new OnClickListener() {
     55             public void onClick(View v) {
     56                 addFragmentToStack();
     57             }
     58         });
     59         button = (Button)v.findViewById(R.id.delete_fragment);
     60         button.setOnClickListener(new OnClickListener() {
     61             public void onClick(View v) {
     62                 getChildFragmentManager().popBackStack();
     63             }
     64         });
     65 
     66         return v;
     67     }
     68 
     69     @Override
     70     public void onSaveInstanceState(Bundle outState) {
     71         super.onSaveInstanceState(outState);
     72         outState.putInt("level", mStackLevel);
     73     }
     74 
     75     void addFragmentToStack() {
     76         mStackLevel++;
     77 
     78         // Instantiate a new fragment.
     79         Fragment newFragment = FragmentStack.CountingFragment.newInstance(mStackLevel);
     80 
     81         // Add the fragment to the activity, pushing this transaction
     82         // on to the back stack.
     83         FragmentTransaction ft = getChildFragmentManager().beginTransaction();
     84         ft.replace(R.id.simple_fragment, newFragment);
     85         ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
     86         ft.addToBackStack(null);
     87         ft.commit();
     88     }
     89 }
     90