アクティビティまたはgetApplicationContext、フラグメント



Activity Getapplicationcontext



アクティビティまたはgetApplicationContext

表示ダイアログはアクティビティを使用する必要があります

アクティビティを使用してアダプタのconvertViewを初期化することをお勧めします



マップを表示するときにApplicationContextを使用する必要があります

他のほとんどの場合、両方を使用できます



ApplicationContextの選択に間違いなく問題はありません

アクティビティを選択すると、アクティビティオブジェクトがリサイクルされず、メモリリークが発生する可能性があります

ビューオブジェクトはアクティビティにロードされ、ライフサイクルはアクティビティのライフサイクルよりも短いか類似していることが多いため、通常、アクティビティをビューオブジェクトのコンテキストとして使用することは問題ありません。



断片

1.理解する

It is a new concept since Android 3.0, which means fragmentation. Born to make better use of the larger and larger screen space, it has become a popular development method. Fragment is a UI fragment that can be embedded in Activity A partial module used to form the Activity interface. It can also be said that an Actiivty interface can be composed of multiple Fragments Its behavior is very similar to Activity, with its own corresponding layout (including specific View), it has its own life cycle, receives its own input events, and can be added or removed from the running activity A fragment must always be embedded in an activity, and the life cycle of the fragment is affected by the activity In essence, a FrameLayout is generated, and the layout it loads is its child layout

2.android-support-v4.jar互換パッケージについて

What is it? Compatible jar package provided by google that is compatible to api4 (1.6) Many new and more powerful APIs have been added to the Android 3.0 (11) SDK for developers to use The v4 package contains some new APIs introduced by android3.0 Why use it? If the latest API is directly used in the project, it cannot be installed in a lower version of the phone And using the V4 compatibility package development can be installed to the lower version (1.6) mobile phone Important API Fragment ViewPager FragmentStatePagerAdatper FragmentPagerAdapter Attention When using Fragment and its related API, it is necessary to guide the definition in V4 How to associate the source code of the jar package under libs

3.関連API

Fragment View onCreateView() onActivityCreated() When the Activity is created getActivity() Host setArguments(), getArgument() ListFragment setListAdapter(adapter) onListItemClick() getFragmentManager() FragmentActivity getSupportFragmentManager() FragmentManager beginTransaction() FragmentTransaction add(), replace(), remove() show() hide() commit() addToBackStack()

4.使用する

Static loading of Fragment 1. Define a subclass of Fragment, and load a layout file in onCreateView() 2. Specify the custom Fragment in the layout file through 3. Our Activity must inherit from FragmentActivity Dynamic loading of Fragment 1. Define a subclass of Fragment, and load a layout file in onCreateView() 2. Our Activity must inherit from FragmentActivity 3. In the code, the Fragment object is dynamically created and added to the specified ViewGroup add replace remove hide show Fragment life cycle Life cycle state Operating status Visible and operable Suspended state Visible, but inoperable Stopped state Exists, but not visible State of death Object does not exist Life cycle approach onAttach() Called when the fragment is associated with the Activity onCreate() onCreateView() Called when creating a view for the fragment (loading the layout) onActivityCreated() Called when the Activity associated with the fragment has been created onStart() onResume() onPause() onStop() onDestroyView() Called when the view associated with the fragment is removed onDestroy() onDetach() Called when the fragment is disassociated from the Activity Test case Add Fragment object display home to desktop Back to application replace for other Fragment Return to its own Fragment Exit the application

5.フラグメントを適用します

Two loading methods of application Fragment Use ListFragment to display the list setListAdapter() Fragment carries data Small technique: how to change the color of the selection android:background='?android:attr/activatedBackgroundIndicator' Set ListView to single selection mode getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE) Set the first one to be selected by default getListView().setItemChecked(0, true)