1 <#ftl> 2 <#-- 3 Copyright 2013 The Android Open Source Project 4 5 Licensed under the Apache License, Version 2.0 (the "License"); 6 you may not use this file except in compliance with the License. 7 You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11 Unless required by applicable law or agreed to in writing, software 12 distributed under the License is distributed on an "AS IS" BASIS, 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 See the License for the specific language governing permissions and 15 limitations under the License. 16 --> 17 package ${sample.package}; 18 19 import android.os.Bundle; 20 import android.support.v4.app.FragmentManager; 21 import android.support.v4.app.FragmentTransaction; 22 import android.view.Menu; 23 24 import com.example.android.common.activities.SampleActivityBase; 25 import com.example.android.common.logger.Log; 26 27 import ${sample.package}.cardstream.CardStream; 28 import ${sample.package}.cardstream.CardStreamFragment; 29 import ${sample.package}.cardstream.CardStreamState; 30 import ${sample.package}.cardstream.OnCardClickListener; 31 import ${sample.package}.cardstream.StreamRetentionFragment; 32 33 public class MainActivity extends SampleActivityBase implements CardStream { 34 public static final String TAG = "MainActivity"; 35 public static final String FRAGTAG = "${sample.name?cap_first}Fragment"; 36 37 private CardStreamFragment mCardStreamFragment; 38 39 private StreamRetentionFragment mRetentionFragment; 40 private static final String RETENTION_TAG = "retention"; 41 42 @Override 43 protected void onCreate(Bundle savedInstanceState) { 44 super.onCreate(savedInstanceState); 45 setContentView(R.layout.activity_main); 46 47 FragmentManager fm = getSupportFragmentManager(); 48 ${sample.name?cap_first}Fragment fragment = 49 (${sample.name?cap_first}Fragment) fm.findFragmentByTag(FRAGTAG); 50 51 if (fragment == null) { 52 FragmentTransaction transaction = fm.beginTransaction(); 53 fragment = new ${sample.name?cap_first}Fragment(); 54 transaction.add(fragment, FRAGTAG); 55 transaction.commit(); 56 } 57 58 // Use fragment as click listener for cards, but must implement correct interface 59 if (!(fragment instanceof OnCardClickListener)){ 60 throw new ClassCastException("${sample.name?cap_first}Fragment must " + 61 "implement OnCardClickListener interface."); 62 } 63 OnCardClickListener clickListener = (OnCardClickListener) fm.findFragmentByTag(FRAGTAG); 64 65 mRetentionFragment = (StreamRetentionFragment) fm.findFragmentByTag(RETENTION_TAG); 66 if (mRetentionFragment == null) { 67 mRetentionFragment = new StreamRetentionFragment(); 68 fm.beginTransaction().add(mRetentionFragment, RETENTION_TAG).commit(); 69 } else { 70 // If the retention fragment already existed, we need to pull some state. 71 // pull state out 72 CardStreamState state = mRetentionFragment.getCardStream(); 73 74 // dump it in CardStreamFragment. 75 mCardStreamFragment = 76 (CardStreamFragment) fm.findFragmentById(R.id.fragment_cardstream); 77 mCardStreamFragment.restoreState(state, clickListener); 78 } 79 } 80 81 public CardStreamFragment getCardStream() { 82 if (mCardStreamFragment == null) { 83 mCardStreamFragment = (CardStreamFragment) 84 getSupportFragmentManager().findFragmentById(R.id.fragment_cardstream); 85 } 86 return mCardStreamFragment; 87 } 88 89 @Override 90 protected void onSaveInstanceState(Bundle outState) { 91 super.onSaveInstanceState(outState); 92 CardStreamState state = getCardStream().dumpState(); 93 mRetentionFragment.storeCardStream(state); 94 } 95 } 96