1 /* 2 * Copyright (C) 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 package com.android.incallui.baseui; 18 19 import android.os.Bundle; 20 import android.support.v4.app.Fragment; 21 22 /** Parent for all fragments that use Presenters and Ui design. */ 23 public abstract class BaseFragment<T extends Presenter<U>, U extends Ui> extends Fragment { 24 25 private static final String KEY_FRAGMENT_HIDDEN = "key_fragment_hidden"; 26 27 private T presenter; 28 BaseFragment()29 protected BaseFragment() { 30 presenter = createPresenter(); 31 } 32 createPresenter()33 public abstract T createPresenter(); 34 getUi()35 public abstract U getUi(); 36 37 /** 38 * Presenter will be available after onActivityCreated(). 39 * 40 * @return The presenter associated with this fragment. 41 */ getPresenter()42 public T getPresenter() { 43 return presenter; 44 } 45 46 @Override onActivityCreated(Bundle savedInstanceState)47 public void onActivityCreated(Bundle savedInstanceState) { 48 super.onActivityCreated(savedInstanceState); 49 presenter.onUiReady(getUi()); 50 } 51 52 @Override onCreate(Bundle savedInstanceState)53 public void onCreate(Bundle savedInstanceState) { 54 super.onCreate(savedInstanceState); 55 if (savedInstanceState != null) { 56 presenter.onRestoreInstanceState(savedInstanceState); 57 if (savedInstanceState.getBoolean(KEY_FRAGMENT_HIDDEN)) { 58 getFragmentManager().beginTransaction().hide(this).commit(); 59 } 60 } 61 } 62 63 @Override onDestroyView()64 public void onDestroyView() { 65 super.onDestroyView(); 66 presenter.onUiDestroy(getUi()); 67 } 68 69 @Override onSaveInstanceState(Bundle outState)70 public void onSaveInstanceState(Bundle outState) { 71 super.onSaveInstanceState(outState); 72 presenter.onSaveInstanceState(outState); 73 outState.putBoolean(KEY_FRAGMENT_HIDDEN, isHidden()); 74 } 75 } 76