gpt4 book ai didi

android - 旋转时未调用 fragment onCreateView 和 onActivityCreated

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:15:07 28 4
gpt4 key购买 nike

我正在处理 fragment 并将新 fragment 推送到后台堆栈,但是当我将设备旋转两次时, fragment 的 onCreateViewonActivityCreated 等等在 fragment 生命周期中永远不会调用方法而留下​​空白屏幕。这仅在 fragment 已添加到后台堆栈或返回到后台堆栈中的第一个 fragment 时发生。

这是我的 Activity 的 fragment 处理方法:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Injection
MormonChannel.injectActivity(this);

setDrawerIndicatorEnabled(true);

// Do not set currentNavigationItem here.
NavigationItemSelectedEvent.NavigationItem navItem = null;
Intent intent = getIntent();
if (intent != null) {
navItem = (NavigationItemSelectedEvent.NavigationItem)
intent.getSerializableExtra(EXTRA_NAV_ITEM);
}

if (savedInstanceState == null) {
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.container, new FeatureListFragment()).commit();
if (navItem != null) {
onNavigationItemSelected(new NavigationItemSelectedEvent(navItem));
} else {
currentNavigationItem = NavigationItemSelectedEvent.NavigationItem.FEATURES;
}
}

}

@Subscribe
public void onNavigationItemSelected(NavigationItemSelectedEvent event) {

if (currentNavigationItem == event.getNavigationItem()) {
return;
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.popBackStack();
NavigationItemSelectedEvent.NavigationItem navigationItem = event.getNavigationItem();
String name = navigationItem.getName();
switch (navigationItem) {
default:
case FEATURES:
// Nothing needs to be done it is already there.
break;
case AUDIO:
fragmentManager.beginTransaction().replace(R.id.container,
CollectionListFragment.newInstance(prefs.getLanguageId(), prefs.getAudioCollectionId()))
.addToBackStack
(name).commit();
break;
case VIDEO:
fragmentManager.beginTransaction().replace(R.id.container,
CollectionListFragment.newInstance(prefs.getLanguageId(), prefs.getVideoCollectionId()))
.addToBackStack(name).commit();
break;
case RADIO:
fragmentManager.beginTransaction().replace(R.id.container,
CollectionListFragment.newInstance(prefs.getLanguageId(), prefs.getRadioCollectionId()))
.addToBackStack(name).commit();
break;
case HISTORY:
fragmentManager.beginTransaction().replace(R.id.container, new HistoryFragment()).addToBackStack(name).commit();
break;
case DOWNLOADS:
fragmentManager.beginTransaction().replace(R.id.container, new DownloadsFragment()).addToBackStack(name).commit();
break;
case PLAYLISTS:
fragmentManager.beginTransaction().replace(R.id.container, new PlaylistFragment()).addToBackStack(name).commit();
break;
}
currentNavigationItem = navigationItem;
}

这是我的 CollectionListFragment 代码:

 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MormonChannel.injectFragment(this);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.collection_list_fragment, container, false);
ButterKnife.inject(this, v);
return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

listView.setDividerHeight(0);
listView.setFastScrollEnabled(true);


Bundle args = getArguments();
if (args != null) {
languageId = args.getLong(ARG_LANGUAGE_ID, -1L);
collectionId = args.getLong(ARG_COLLECTION_ID, -1L);
if (args.containsKey(ARG_SORT)) {
sort = (Sort) args.getSerializable(ARG_SORT);
}
}

if (collectionId == -1L || languageId == -1L) {
// TODO Implement Empty Collection Text
} else {
collection = collectionManager.findByCollectionId(languageId, collectionId);
getLoaderManager().initLoader(LOADER_ID, null, this);
}

titleEvent = new TitleChangeEvent(collection != null ? collection.getTitle() : getString(R.string.app_name));
bus.post(titleEvent);
}

Activity list :

<activity
android:name=".activity.MainActivity"
android:launchMode="singleTask">
<meta-data
android:name="android.app.default_searchable"
android:value=".activity.SearchActivity"/>
</activity>

最佳答案

在您的 CollectionListFragment 代码中,在 onCreate() 方法中添加对 setRetainInstance() 方法的调用,并将 true 作为其参数:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
MormonChannel.injectFragment(this);

}

使用 setRetainInstance(true) 确保当配置更改发生时,activity 将被终止,但 android 不会破坏它托管的 fragment 。

相反,android 将保存 fragment 状态并将 fragment 与 Activity 分离。此外,它不会破坏 fragment ,因此稍后在创建托管 Activity 时不会创建它。因此 fragment 将不会收到对其 onDestroy() 和 onCreate() 方法的调用。

关于android - 旋转时未调用 fragment onCreateView 和 onActivityCreated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23391837/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com