Home | History | Annotate | Download | only in activity
      1 /*
      2  * Copyright (C) 2014 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.dialer.activity;
     18 
     19 import android.app.Activity;
     20 import android.os.Bundle;
     21 
     22 import com.android.dialerbind.analytics.AnalyticsActivity;
     23 
     24 /**
     25  * A common superclass that keeps track of whether an {@link Activity} has saved its state yet or
     26  * not.
     27  */
     28 public abstract class TransactionSafeActivity extends AnalyticsActivity {
     29 
     30     private boolean mIsSafeToCommitTransactions;
     31 
     32     @Override
     33     protected void onCreate(Bundle savedInstanceState) {
     34         super.onCreate(savedInstanceState);
     35         mIsSafeToCommitTransactions = true;
     36     }
     37 
     38     @Override
     39     protected void onStart() {
     40         super.onStart();
     41         mIsSafeToCommitTransactions = true;
     42     }
     43 
     44     @Override
     45     protected void onResume() {
     46         super.onResume();
     47         mIsSafeToCommitTransactions = true;
     48     }
     49 
     50     @Override
     51     protected void onSaveInstanceState(Bundle outState) {
     52         super.onSaveInstanceState(outState);
     53         mIsSafeToCommitTransactions = false;
     54     }
     55 
     56     /**
     57      * Returns true if it is safe to commit {@link FragmentTransaction}s at this time, based on
     58      * whether {@link Activity#onSaveInstanceState} has been called or not.
     59      *
     60      * Make sure that the current activity calls into
     61      * {@link super.onSaveInstanceState(Bundle outState)} (if that method is overridden),
     62      * so the flag is properly set.
     63      */
     64     public boolean isSafeToCommitTransactions() {
     65         return mIsSafeToCommitTransactions;
     66     }
     67 }
     68