Fragment 基本使用
程序员文章站
2024-02-26 17:53:16
...
Fragment 表示 Activity 中一种行为或用户界面的一部分,它有自己的生命周期接收自己的输入事件,也可以在 Activity 运行时添加或删除事件。
Fragment 生命周期
- 管理 Fragment 的生命周期非常类似管理 Activity 的生命周期
- Activity 布局文件中添加 fragment,启动 Activity 生命周期执行顺序。
- Activity 布局文件中添加 fragment,按 back 键生命周期执行顺序。
- 测试代码
- MainActivity
public class MainActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate: ");
setContentView(R.layout.activity_main);
if (findViewById(R.id.ll_content) != null) {
if (savedInstanceState != null) {
return;
}
HeadlinesFragment headlinesFragment = new HeadlinesFragment();
headlinesFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(R.id.ll_content,headlinesFragment).commit();
}
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart: ");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume: ");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause: ");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop: ");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
}
- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</FrameLayout>
- 需要添加的 HeadlinesFragment
public class HeadlinesFragment extends ListFragment {
private static final String TAG = "HeadlinesFragment";
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate: ");
setListAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_activated_1,
titles));
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
Log.d(TAG, "onCreateView: ");
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onListItemClick(@NonNull ListView l, @NonNull View v, int position, long id) {
super.onListItemClick(l, v, position, id);
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
Log.d(TAG, "onAttach: ");
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart: ");
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume: ");
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG, "onPause: ");
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG, "onStop: ");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG, "onDestroyView: ");
}
@Override
public void onDetach() {
super.onDetach();
Log.d(TAG, "onDetach: ");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
}