• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.bitmapfun.util;
18 
19 import android.os.Bundle;
20 import android.support.v4.app.Fragment;
21 import android.support.v4.app.FragmentManager;
22 
23 /**
24  * A simple non-UI Fragment that stores a single Object and is retained over configuration changes.
25  * In this sample it will be used to retain the ImageCache object.
26  */
27 public class RetainFragment extends Fragment {
28     private static final String TAG = "RetainFragment";
29     private Object mObject;
30 
31     /**
32      * Empty constructor as per the Fragment documentation
33      */
RetainFragment()34     public RetainFragment() {}
35 
36     /**
37      * Locate an existing instance of this Fragment or if not found, create and
38      * add it using FragmentManager.
39      *
40      * @param fm The FragmentManager manager to use.
41      * @return The existing instance of the Fragment or the new instance if just
42      *         created.
43      */
findOrCreateRetainFragment(FragmentManager fm)44     public static RetainFragment findOrCreateRetainFragment(FragmentManager fm) {
45         // Check to see if we have retained the worker fragment.
46         RetainFragment mRetainFragment = (RetainFragment) fm.findFragmentByTag(TAG);
47 
48         // If not retained (or first time running), we need to create and add it.
49         if (mRetainFragment == null) {
50             mRetainFragment = new RetainFragment();
51             fm.beginTransaction().add(mRetainFragment, TAG).commit();
52         }
53 
54         return mRetainFragment;
55     }
56 
57     @Override
onCreate(Bundle savedInstanceState)58     public void onCreate(Bundle savedInstanceState) {
59         super.onCreate(savedInstanceState);
60 
61         // Make sure this Fragment is retained over a configuration change
62         setRetainInstance(true);
63     }
64 
65     /**
66      * Store a single object in this Fragment.
67      *
68      * @param object The object to store
69      */
setObject(Object object)70     public void setObject(Object object) {
71         mObject = object;
72     }
73 
74     /**
75      * Get the stored object.
76      *
77      * @return The stored object
78      */
getObject()79     public Object getObject() {
80         return mObject;
81     }
82 
83 }
84