fragment|fragment 生命周期的不同表现

Viewpager +fragment

08-11 11:33:36.1567162-7162/com.example.yinsgo.myui V/Fragment1﹕ setUserVisibleHint false 08-11 11:33:36.1567162-7162/com.example.yinsgo.myui V/Fragment2﹕ setUserVisibleHint false 08-11 11:33:36.1577162-7162/com.example.yinsgo.myui V/Fragment1﹕ setUserVisibleHint true 08-11 11:33:36.1587162-7162/com.example.yinsgo.myui V/Fragment1﹕ onAttach 08-11 11:33:36.1587162-7162/com.example.yinsgo.myui V/Fragment1﹕ onCreate 08-11 11:33:36.1597162-7162/com.example.yinsgo.myui V/Fragment1﹕ onCreateView 08-11 11:33:36.1607162-7162/com.example.yinsgo.myui V/Fragment1﹕ onActivityCreated 08-11 11:33:36.1607162-7162/com.example.yinsgo.myui V/Fragment1﹕ onResume() 08-11 11:33:36.1607162-7162/com.example.yinsgo.myui V/Fragment2﹕ onAttach 08-11 11:33:36.1607162-7162/com.example.yinsgo.myui V/Fragment2﹕ onCreate 08-11 11:33:36.1607162-7162/com.example.yinsgo.myui V/Fragment2﹕ onCreateView 08-11 11:33:36.1617162-7162/com.example.yinsgo.myui V/Fragment2﹕ onActivityCreated 08-11 11:33:36.1617162-7162/com.example.yinsgo.myui V/Fragment2﹕ onResume()当切换到第二个fragment时打印日志:08-11 11:33:54.0847162-7162/com.example.yinsgo.myui V/Fragment3﹕ setUserVisibleHint false 08-11 11:33:54.0847162-7162/com.example.yinsgo.myui V/Fragment1﹕ setUserVisibleHint false 08-11 11:33:54.0847162-7162/com.example.yinsgo.myui V/Fragment2﹕ setUserVisibleHint true 08-11 11:33:54.0847162-7162/com.example.yinsgo.myui V/Fragment3﹕ onAttach 08-11 11:33:54.0857162-7162/com.example.yinsgo.myui V/Fragment3﹕ onCreate 08-11 11:33:54.0857162-7162/com.example.yinsgo.myui V/Fragment3﹕ onCreateView 08-11 11:33:54.0857162-7162/com.example.yinsgo.myui V/Fragment3﹕ onActivityCreated 08-11 11:33:54.0857162-7162/com.example.yinsgo.myui V/Fragment3﹕ onResume()

  • 首次setUserVisibleHint true时,fragment还未初始化
  • activity进入时,两个fragment都会resume

    推荐阅读