139a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell/*
2ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas * Copyright 2018 The Android Open Source Project
339a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell *
439a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell * Licensed under the Apache License, Version 2.0 (the "License");
539a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell * you may not use this file except in compliance with the License.
639a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell * You may obtain a copy of the License at
739a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell *
839a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell *      http://www.apache.org/licenses/LICENSE-2.0
939a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell *
1039a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell * Unless required by applicable law or agreed to in writing, software
1139a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell * distributed under the License is distributed on an "AS IS" BASIS,
1239a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1339a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell * See the License for the specific language governing permissions and
1439a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell * limitations under the License.
1539a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell */
1639a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell
1739a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell
18ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikaspackage androidx.fragment.app;
1939a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell
2039a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powellimport android.os.Bundle;
2139a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powellimport android.view.LayoutInflater;
2239a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powellimport android.view.View;
2339a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powellimport android.view.ViewGroup;
2439a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell
25320113721c2e14bbc2403809046fa2959a665c11Aurimas Liutikasimport androidx.fragment.test.R;
26320113721c2e14bbc2403809046fa2959a665c11Aurimas Liutikas
2739a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powellpublic class StrictViewFragment extends StrictFragment {
2839a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell    boolean mOnCreateViewCalled, mOnViewCreatedCalled, mOnDestroyViewCalled;
29990e6fc0326f5948736650c0cb71b6002d443c9cGeorge Mount    int mLayoutId = R.layout.strict_view_fragment;
30990e6fc0326f5948736650c0cb71b6002d443c9cGeorge Mount
31990e6fc0326f5948736650c0cb71b6002d443c9cGeorge Mount    public void setLayoutId(int layoutId) {
32990e6fc0326f5948736650c0cb71b6002d443c9cGeorge Mount        mLayoutId = layoutId;
33990e6fc0326f5948736650c0cb71b6002d443c9cGeorge Mount    }
3439a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell
3540e1750a5ed6cca1efb47e10af4db19abecd9fd5George Mount    public static StrictViewFragment create(int layoutId) {
3640e1750a5ed6cca1efb47e10af4db19abecd9fd5George Mount        StrictViewFragment fragment = new StrictViewFragment();
3740e1750a5ed6cca1efb47e10af4db19abecd9fd5George Mount        fragment.mLayoutId = layoutId;
3840e1750a5ed6cca1efb47e10af4db19abecd9fd5George Mount        return fragment;
3940e1750a5ed6cca1efb47e10af4db19abecd9fd5George Mount    }
4040e1750a5ed6cca1efb47e10af4db19abecd9fd5George Mount
4139a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell    @Override
4239a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell    public View onCreateView(LayoutInflater inflater, ViewGroup container,
4339a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell            Bundle savedInstanceState) {
4439a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell        checkGetActivity();
4539a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell        checkState("onCreateView", CREATED);
46990e6fc0326f5948736650c0cb71b6002d443c9cGeorge Mount        final View result = inflater.inflate(mLayoutId, container, false);
4739a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell        mOnCreateViewCalled = true;
4839a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell        return result;
4939a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell    }
5039a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell
5139a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell    @Override
5239a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell    public void onViewCreated(View view, Bundle savedInstanceState) {
5339a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell        if (view == null) {
5439a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell            throw new IllegalArgumentException("onViewCreated view argument should not be null");
5539a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell        }
5639a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell        checkGetActivity();
5739a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell        checkState("onViewCreated", CREATED);
5839a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell        mOnViewCreatedCalled = true;
5939a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell    }
6039a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell
6139a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell    @Override
6239a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell    public void onDestroyView() {
6339a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell        super.onDestroyView();
6439a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell        if (getView() == null) {
6539a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell            throw new IllegalStateException("getView returned null in onDestroyView");
6639a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell        }
6739a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell        checkGetActivity();
6839a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell        checkState("onDestroyView", CREATED);
6939a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell        mOnDestroyViewCalled = true;
7039a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell    }
7139a21dcc947992baaa700181c08fbe8bb8f077d6Adam Powell}
72