不会相应地调用片段的生命周期回调

时间:2016-02-08 14:30:04

标签: android android-fragments android-viewpager android-lifecycle

我有一个带有视图寻呼机的主要活动和三个动作标签“片段”。我实现了每个主要活动和生命周期回调 三个片段。并且在每个生命周期回调中我放置了一个日志语句,指示调用哪个生命周期回调以了解如何 视图寻呼机的操作栏表现出来。在运行时我发现了一种奇怪的行为,我无法理解或将其归因于任何原因。

第一个行为

NSAttributeDescription *groupId = [entity.propertiesByName objectForKey:@"group_id"];
NSArray *propertiesToFetch = [NSArray arrayWithObjects:groupId, maxReviewED, nil];
NSArray *propertiesToGroupBy = [NSArray arrayWithObjects:groupId, nil];
[fetchRequest setPropertiesToFetch:propertiesToFetch];
[fetchRequest setPropertiesToGroupBy:propertiesToGroupBy];

我找不到任何理由为什么只有mainactivty,frag_1和frag_2的生命周期回调被称为但不是frag_3?任何解释?

第二种行为

when the App fisrt starts I receive:

02-08 15:16:14.771 32243-32243/com.example.com.vpager_00 W/MainActivity: onCreate()
02-08 15:16:14.901 32243-32243/com.example.com.vpager_00 W/MainActivity: onStart()
02-08 15:16:14.901 32243-32243/com.example.com.vpager_00 W/MainActivity: onResume()
02-08 15:16:14.941 32243-32243/com.example.com.vpager_00 W/Frag_1: onAttach()
02-08 15:16:14.941 32243-32243/com.example.com.vpager_00 W/Frag_1: onCreate()
02-08 15:16:14.941 32243-32243/com.example.com.vpager_00 W/Frag_1: onCreateView()
02-08 15:16:14.941 32243-32243/com.example.com.vpager_00 W/Frag_1: onStart()
02-08 15:16:14.941 32243-32243/com.example.com.vpager_00 W/Frag_1: onResume()
02-08 15:16:14.941 32243-32243/com.example.com.vpager_00 W/Frag_2: onAttach()
02-08 15:16:14.941 32243-32243/com.example.com.vpager_00 W/Frag_2: onCreate()
02-08 15:16:14.941 32243-32243/com.example.com.vpager_00 W/Frag_2: onCreateView()
02-08 15:16:14.941 32243-32243/com.example.com.vpager_00 W/Frag_2: onStart()
02-08 15:16:14.941 32243-32243/com.example.com.vpager_00 W/Frag_2: onResume()

我认为生命周期回调被称为这是可以理解的,但为什么只有frag_1的生命周期回调也被称为,我想, 除了frag_3和frag_1的回调之外,必须调用aslo frag_2回调,因为最近frag_2处于onResume()状态。

任何解释?

mainactivity

occured when i touched tab3 "frag_3", i received:

02-08 15:16:36.031 32243-32243/com.example.com.vpager_00 D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
02-08 15:16:36.121 32243-32243/com.example.com.vpager_00 W/Frag_3: onAttach()
02-08 15:16:36.121 32243-32243/com.example.com.vpager_00 W/Frag_3: onCreate()
02-08 15:16:36.121 32243-32243/com.example.com.vpager_00 W/Frag_3: onCreateView()
02-08 15:16:36.121 32243-32243/com.example.com.vpager_00 W/Frag_3: onStart()
02-08 15:16:36.121 32243-32243/com.example.com.vpager_00 W/Frag_3: onResume()
02-08 15:16:36.461 32243-32243/com.example.com.vpager_00 W/Frag_1: onPause()
02-08 15:16:36.461 32243-32243/com.example.com.vpager_00 W/Frag_1: onStop()
02-08 15:16:36.471 32243-32243/com.example.com.vpager_00 W/Frag_1: onDestroy()

}

frag_1

public class MainActivity extends AppCompatActivity {

private final String TAG = this.getClass().getSimpleName();

private Toolbar mTB = null;
private TabLayout mTL = null;
private ViewPager mVP = null;
private VPagerAdapter mVPAdapter = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.w(TAG, "onCreate()");

    this.initViews(R.layout.act_main);
    this.initObjs();
}

private void initObjs() {
    this.mVPAdapter = new VPagerAdapter(getSupportFragmentManager(), this.mTL.getTabCount());
    this.mVP.setAdapter(this.mVPAdapter);

    this.mVP.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(this.mTL));
    this.mTL.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            mVP.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}

private void initViews(int rootView) {
    setContentView(rootView);

    this.mTB = (Toolbar) findViewById(R.id.toolbar);
    this.mTL = (TabLayout) findViewById(R.id.tab_layout);
    this.mVP = (ViewPager) findViewById(R.id.pager);

    setSupportActionBar(this.mTB);
    this.mTL.addTab(this.mTL.newTab().setText("Tab 1"));
    this.mTL.addTab(this.mTL.newTab().setText("Tab 2"));
    this.mTL.addTab(this.mTL.newTab().setText("Tab 3"));
    this.mTL.setTabGravity(TabLayout.GRAVITY_FILL);
}

@Override
protected void onStart() {
    super.onStart();
    Log.w(TAG, "onStart()");
}

@Override
protected void onResume() {
    super.onResume();
    Log.w(TAG, "onResume()");
}

@Override
protected void onPause() {
    super.onPause();
    Log.w(TAG, "onPause()");
}

@Override
protected void onStop() {
    super.onStop();
    Log.w(TAG, "onStop()");
}

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.w(TAG, "onDestroy()");
}

}

frag_2

public class Frag_1 extends Fragment {

private final String TAG = this.getClass().getSimpleName();

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    Log.w(TAG, "onAttach()");
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.w(TAG, "onCreate()");
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.w(TAG, "onCreateView()");

    return inflater.inflate(R.layout.frag_1, container, false);
}

@Override
public void onStart() {
    super.onStart();
    Log.w(TAG, "onStart()");
}

@Override
public void onResume() {
    super.onResume();
    Log.w(TAG, "onResume()");
}

@Override
public void onPause() {
    super.onPause();
    Log.w(TAG, "onPause()");
}

@Override
public void onStop() {
    super.onStop();
    Log.w(TAG, "onStop()");
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.w(TAG, "onDestroy()");
}

}

frag_3

public class Frag_2 extends Fragment {

private final String TAG = this.getClass().getSimpleName();

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    Log.w(TAG, "onAttach()");
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.w(TAG, "onCreate()");
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.w(TAG, "onCreateView()");

    return inflater.inflate(R.layout.frag_2, container, false);
}

@Override
public void onStart() {
    super.onStart();
    Log.w(TAG, "onStart()");
}

@Override
public void onResume() {
    super.onResume();
    Log.w(TAG, "onResume()");
}

@Override
public void onPause() {
    super.onPause();
    Log.w(TAG, "onPause()");
}

@Override
public void onStop() {
    super.onStop();
    Log.w(TAG, "onStop()");
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.w(TAG, "onDestroy()");
}

}

2 个答案:

答案 0 :(得分:3)

在ViewPager中,正在提前创建片段。

这意味着,您的Frag_2尚未显示(因为Frag_1覆盖整个屏幕),但它仍然会预先创建视图,因此滚动到Frag_2会很顺利。

默认情况下,ViewPager会在每一侧预加载一个片段。因此,如果您将ViewPager的起始位置设置为Frag_2,它也会加载Frag_1Frag_2,因为它们是邻居。

您会注意到,当您滑动到Frag_3时会创建Frag_2

您可以通过调用ViewPager.setOffscreenPageLimit(int limit)来增加预加载片段的数量。

注意分钟。 number为1,因此无法禁用。

答案 1 :(得分:0)

生命周期回调没问题。

检查setOffscreenPageLimit方法,您可以使用它来设置在空闲状态下应保留到视图层次结构中当前页面任一侧的页数。默认设置为1,这就是原因,因为你有这种行为..