• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright 2013 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 
18 
19 package com.example.android.batchstepsensor;
20 
21 import android.os.Bundle;
22 import android.support.v4.app.FragmentManager;
23 import android.support.v4.app.FragmentTransaction;
24 import android.view.Menu;
25 
26 import com.example.android.common.activities.SampleActivityBase;
27 import com.example.android.common.logger.Log;
28 
29 import com.example.android.batchstepsensor.cardstream.CardStream;
30 import com.example.android.batchstepsensor.cardstream.CardStreamFragment;
31 import com.example.android.batchstepsensor.cardstream.CardStreamState;
32 import com.example.android.batchstepsensor.cardstream.OnCardClickListener;
33 import com.example.android.batchstepsensor.cardstream.StreamRetentionFragment;
34 
35 public class MainActivity extends SampleActivityBase implements CardStream {
36     public static final String TAG = "MainActivity";
37     public static final String FRAGTAG = "BatchStepSensorFragment";
38 
39     private CardStreamFragment mCardStreamFragment;
40 
41     private StreamRetentionFragment mRetentionFragment;
42     private static final String RETENTION_TAG = "retention";
43 
44     @Override
onCreate(Bundle savedInstanceState)45     protected void onCreate(Bundle savedInstanceState) {
46         super.onCreate(savedInstanceState);
47         setContentView(R.layout.activity_main);
48 
49         FragmentManager fm = getSupportFragmentManager();
50         BatchStepSensorFragment fragment =
51                 (BatchStepSensorFragment) fm.findFragmentByTag(FRAGTAG);
52 
53         if (fragment == null) {
54             FragmentTransaction transaction = fm.beginTransaction();
55             fragment = new BatchStepSensorFragment();
56             transaction.add(fragment, FRAGTAG);
57             transaction.commit();
58         }
59 
60         // Use fragment as click listener for cards, but must implement correct interface
61         if(!(fragment instanceof OnCardClickListener)){
62             throw new ClassCastException("BatchStepSensorFragment must " +
63                     "implement OnCardClickListener interface.");
64         }
65         OnCardClickListener clickListener = (OnCardClickListener) fm.findFragmentByTag(FRAGTAG);
66 
67         mRetentionFragment = (StreamRetentionFragment) fm.findFragmentByTag(RETENTION_TAG);
68         if (mRetentionFragment == null) {
69             mRetentionFragment = new StreamRetentionFragment();
70             fm.beginTransaction().add(mRetentionFragment, RETENTION_TAG).commit();
71         } else {
72             // If the retention fragment already existed, we need to pull some state.
73             // pull state out
74             CardStreamState state = mRetentionFragment.getCardStream();
75 
76             // dump it in CardStreamFragment.
77             mCardStreamFragment =
78                     (CardStreamFragment) fm.findFragmentById(R.id.fragment_cardstream);
79             mCardStreamFragment.restoreState(state, clickListener);
80         }
81     }
82 
getCardStream()83     public CardStreamFragment getCardStream() {
84         if (mCardStreamFragment == null) {
85             mCardStreamFragment = (CardStreamFragment)
86                     getSupportFragmentManager().findFragmentById(R.id.fragment_cardstream);
87         }
88         return mCardStreamFragment;
89     }
90 
91     @Override
onSaveInstanceState(Bundle outState)92     protected void onSaveInstanceState(Bundle outState) {
93         super.onSaveInstanceState(outState);
94         CardStreamState state = getCardStream().dumpState();
95         mRetentionFragment.storeCardStream(state);
96     }
97 }
98