Wenyi Wang | e4e359f | 2016-04-02 14:30:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 17 | package com.android.contacts.activities; |
Wenyi Wang | e4e359f | 2016-04-02 14:30:02 -0700 | [diff] [blame] | 18 | |
| 19 | import android.os.Bundle; |
| 20 | import android.support.v7.app.AppCompatActivity; |
| 21 | |
| 22 | /** |
| 23 | * A common superclass that keeps track of whether an {@link AppCompatActivity} has saved its state |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 24 | * yet or not, copied from {@link TransactionSafeActivity}, |
Wenyi Wang | e4e359f | 2016-04-02 14:30:02 -0700 | [diff] [blame] | 25 | * which will be deprecated after Kitkat backporting is done. |
| 26 | */ |
| 27 | public abstract class AppCompatTransactionSafeActivity extends AppCompatActivity { |
| 28 | |
| 29 | private boolean mIsSafeToCommitTransactions; |
| 30 | |
| 31 | @Override |
| 32 | protected void onCreate(Bundle savedInstanceState) { |
| 33 | super.onCreate(savedInstanceState); |
| 34 | mIsSafeToCommitTransactions = true; |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | protected void onStart() { |
| 39 | super.onStart(); |
| 40 | mIsSafeToCommitTransactions = true; |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | protected void onResume() { |
| 45 | super.onResume(); |
| 46 | mIsSafeToCommitTransactions = true; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | protected void onSaveInstanceState(Bundle outState) { |
| 51 | super.onSaveInstanceState(outState); |
| 52 | mIsSafeToCommitTransactions = false; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns true if it is safe to commit {@link FragmentTransaction}s at this time, based on |
| 57 | * whether {@link FragmentActivity#onSaveInstanceState} has been called or not. |
| 58 | * |
| 59 | * Make sure that the current activity calls into |
| 60 | * {@link super.onSaveInstanceState(Bundle outState)} (if that method is overridden), |
| 61 | * so the flag is properly set. |
| 62 | */ |
| 63 | public boolean isSafeToCommitTransactions() { |
| 64 | return mIsSafeToCommitTransactions; |
| 65 | } |
| 66 | } |