【发布时间】:2015-05-04 07:25:25
【问题描述】:
我正在玩 Android 的标准有效导航示例,它展示了如何创建可滑动的视图
代码可以从以下页面下载: http://developer.android.com/training/implementing-navigation/lateral.html
在 getView() 方法中,创建了一个新片段。这不是一个不好的做法,不会导致内存泄漏。
public Fragment getItem(int i) {
switch (i) {
case 0:
// The first section of the app is the most interesting -- it offers
// a launchpad into the other demonstrations in this example application.
return new LaunchpadSectionFragment();
default:
// The other sections of the app are dummy placeholders.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
return fragment;
}
}
【问题讨论】:
-
这里的内存泄漏是什么意思?
-
每次都会在 getView() 中创建一个新的 Fragment 实例。我可以在调试器中看到 DummySectionFragments 正在获取新的 id。
标签: android android-fragments android-actionbar android-viewpager