(Android) Fragment

Fragment


The core series of lifecycle methods that are called to bring a fragment up to resumed state (interacting with the user) are:

  1. onAttach(Activity) called once the fragment is associated with its activity.
  2. onCreate(Bundle) called to do initial creation of the fragment.
  3. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.
  4. onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.onCreate().
  5. onViewStateRestored(Bundle) tells the fragment that all of the saved state of its view hierarchy has been restored.
  6. onStart() makes the fragment visible to the user (based on its containing activity being started).
  7. onResume() makes the fragment interacting with the user (based on its containing activity being resumed).

As a fragment is no longer being used, it goes through a reverse series of callbacks:

  1. onPause() fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.
  2. onStop() fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.
  3. onDestroyView() allows the fragment to clean up resources associated with its View.
  4. onDestroy() called to do final cleanup of the fragment's state.
  5. onDetach() called immediately prior to the fragment no longer being associated with its activity.



1) Create one class extends Fragment

public class FragmentA extends Fragment {

private View parentView;

@Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
     parentView = inflater.inflate(R.layout.activity_group_list, null);

     //Use parentView to get other views.

     View view = (View)parentView.findViewById(...); 
     return parentView;
 }

}

2)  Layout xml for the fragment

layout. xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/fragmentcontainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background" />

</LinearLayout>

3) Replace the fragment in the activity


private FragmentManager fragmentMgr;

private FragmentTransaction ft;

if (fragmentMgr == null)
   fragmentMgr = getSupportFragmentManager();

FragmentA fragment = fragmentMgr.findFragmentByTag("A") != null ? (FragmentA) fragmentMgr
     .findFragmentByTag("A") : new FragmentA();

private void switchToCurrentFragment(Fragment frag, String tag) {
  ft = fragmentMgr.beginTransaction();
  ft.replace(R.id.fragmentcontainer, frag, tag);
  if (!isFinishing()) {
   ft.commitAllowingStateLoss();
  }
 }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值