diff options
| author | Dianne Hackborn <hackbod@google.com> | 2010-04-15 14:45:25 -0700 |
|---|---|---|
| committer | Dianne Hackborn <hackbod@google.com> | 2010-04-19 15:09:31 -0700 |
| commit | 2dedce6e84679ead961a485c7fe4b0f77c713b6a (patch) | |
| tree | 356954d8f2c4b3580c65f693811d3fae6f536a1e /core/java/android/app/FragmentManager.java | |
| parent | 2df4c76b96173ddff079f610ba74274e1cc9ae79 (diff) | |
| download | frameworks_base-2dedce6e84679ead961a485c7fe4b0f77c713b6a.zip frameworks_base-2dedce6e84679ead961a485c7fe4b0f77c713b6a.tar.gz frameworks_base-2dedce6e84679ead961a485c7fe4b0f77c713b6a.tar.bz2 | |
Introducing Fragment.
Basic implementation of an API for organizing a single activity into separate,
discrete pieces. Currently supports adding and removing fragments, and
performing basic lifecycle callbacks on them.
Change-Id: I6ea8e6bdb04d93f8105c2e983fe9b6532422de34
Diffstat (limited to 'core/java/android/app/FragmentManager.java')
| -rw-r--r-- | core/java/android/app/FragmentManager.java | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/core/java/android/app/FragmentManager.java b/core/java/android/app/FragmentManager.java new file mode 100644 index 0000000..d5e49cf --- /dev/null +++ b/core/java/android/app/FragmentManager.java @@ -0,0 +1,183 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.app; + +import android.os.Bundle; +import android.view.ViewGroup; + +import java.util.ArrayList; + +/** + * Container for fragments associated with an activity. + */ +class FragmentManager { + final ArrayList<Fragment> mFragments = new ArrayList<Fragment>(); + + int mCurState = Fragment.INITIALIZING; + Activity mActivity; + + void moveToState(Fragment f, int newState) { + if (f.mState < newState) { + switch (f.mState) { + case Fragment.INITIALIZING: + f.mActivity = mActivity; + f.mCalled = false; + f.onAttach(mActivity); + if (!f.mCalled) { + throw new SuperNotCalledException("Fragment " + f + + " did not call through to super.onAttach()"); + } + f.mCalled = false; + f.onCreate(null); + if (!f.mCalled) { + throw new SuperNotCalledException("Fragment " + f + + " did not call through to super.onCreate()"); + } + + ViewGroup container = null; + if (f.mContainerId != 0) { + container = (ViewGroup)mActivity.findViewById(f.mContainerId); + if (container == null) { + throw new IllegalArgumentException("New view found for id 0x" + + Integer.toHexString(f.mContainerId) + + " for fragment " + f); + } + } + f.mContainer = container; + f.mView = f.onCreateView(mActivity.getLayoutInflater(), container); + if (container != null && f.mView != null) { + container.addView(f.mView); + } + + case Fragment.CREATED: + if (newState > Fragment.CREATED) { + f.mCalled = false; + f.onStart(); + if (!f.mCalled) { + throw new SuperNotCalledException("Fragment " + f + + " did not call through to super.onStart()"); + } + } + case Fragment.STARTED: + if (newState > Fragment.STARTED) { + f.mCalled = false; + f.onResume(); + if (!f.mCalled) { + throw new SuperNotCalledException("Fragment " + f + + " did not call through to super.onResume()"); + } + } + } + } else if (f.mState > newState) { + switch (f.mState) { + case Fragment.RESUMED: + if (newState < Fragment.RESUMED) { + f.mCalled = false; + f.onPause(); + if (!f.mCalled) { + throw new SuperNotCalledException("Fragment " + f + + " did not call through to super.onPause()"); + } + } + case Fragment.STARTED: + if (newState < Fragment.STARTED) { + f.mCalled = false; + f.onStop(); + if (!f.mCalled) { + throw new SuperNotCalledException("Fragment " + f + + " did not call through to super.onStop()"); + } + } + case Fragment.CREATED: + if (newState < Fragment.CREATED) { + if (f.mContainer != null && f.mView != null) { + f.mContainer.removeView(f.mView); + } + f.mContainer = null; + f.mView = null; + + f.mCalled = false; + f.onDestroy(); + if (!f.mCalled) { + throw new SuperNotCalledException("Fragment " + f + + " did not call through to super.onDestroy()"); + } + f.mCalled = false; + f.onDetach(); + if (!f.mCalled) { + throw new SuperNotCalledException("Fragment " + f + + " did not call through to super.onDetach()"); + } + f.mActivity = null; + } + } + } + + f.mState = newState; + } + + void moveToState(int newState) { + if (mActivity == null && newState != Fragment.INITIALIZING) { + throw new IllegalStateException("No activity"); + } + + mCurState = newState; + for (int i=0; i<mFragments.size(); i++) { + Fragment f = mFragments.get(i); + moveToState(f, newState); + } + } + + public void addFragment(Fragment fragment) { + mFragments.add(fragment); + } + + public void removeFragment(Fragment fragment) { + mFragments.remove(fragment); + moveToState(fragment, Fragment.INITIALIZING); + } + + public void attachActivity(Activity activity) { + if (mActivity != null) throw new IllegalStateException(); + mActivity = activity; + } + + public void dispatchCreate(Bundle state) { + moveToState(Fragment.CREATED); + } + + public void dispatchStart() { + moveToState(Fragment.STARTED); + } + + public void dispatchResume() { + moveToState(Fragment.RESUMED); + } + + public void dispatchPause() { + moveToState(Fragment.STARTED); + } + + public void dispatchStop() { + moveToState(Fragment.CREATED); + } + + public void dispatchDestroy() { + moveToState(Fragment.INITIALIZING); + mActivity = null; + } +} |
