更改片段而无需重新创建 [英] Change fragments without re-creation
问题描述
我正在研究MVP.
我在nav菜单上有活动,并且我从菜单中更改片段如下:
I have and activity with nav menu and I change fragments from menu like this:
switch (id) {
case R.id.nav_status:
fragmentManager.beginTransaction().replace(R.id.fr_main, mAppProductFragment).commit();
Log.d(TAG, "Выбрано меню статус");
break;
case R.id.nav_schemas:
AppRedirectFragment schemasFragment = new AppRedirectFragment();
fragmentManager.beginTransaction().replace(R.id.fr_main, schemasFragment).commit();
Log.d(TAG, "Выбрано меню переадресация");
break;
}
问题是,当我更改片段时,我之前的片段被破坏了.它调用以下回调:
The problem is that when I change the fragment, my previous fragment is destroyed. It calls the following callbacks:
D/AppProductPresenter: onStopDetouchView()
D/AppProductFragment: onDestroyView
D/AppProductFragment: onDestroy()
它失去了对演示者的引用,并且放弃了所有的改装请求. mPresenter.onDestroyView();
,所以我所有的网络操作都被破坏了.但我想在后台运行它们.但其处置.
It loses its reference to the presenter and also dsipose all retrofit requests.
mPresenter.onDestroyView();
, so all my network operations are destroyd. But Id like to run them in backgroud. But its disposed.
public void onDestroy() {
super.onDestroy();
mPresenter.onDestroyView();
}
那么如何在不调用onDestroy的情况下更改片段?我读到我必须使用 add
而不是 fragmentManager.beginTransaction().replace
那怎么正确呢?
So how can change fragments without calling onDestroy being called? I read that I have to use add
instead of fragmentManager.beginTransaction().replace
So how do it correctly?
推荐答案
我解决了一个问题.答案是使用以下方法的组合: .add
.attach
和 .detach
方法.我创建了一个函数来替换这样的碎片:
I solve a problem.
The answer is use combo of: .add
.attach
and .detach
methods. I create a function that replace a fragmetns like this:
switch (id) {
case R.id.nav_product:
replaceFragment(PRODUCT_FRAGMENT);
break;
case R.id.nav_redirection:
replaceFragment(REDIRECTION_FRAGMENT);
break;
}
private void replaceFragment(String tag) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
Fragment currentFragment = fragmentManager.findFragmentById(R.id.fr_container);
Fragment nextFragment = fragmentManager.findFragmentByTag(tag);
Log.d(TAG, "f detached: " + currentFragment.toString());
transaction.detach(currentFragment);
if (nextFragment == null) {
nextFragment = createFragment(tag);
transaction.add(R.id.fr_container, nextFragment, tag);
} else {
Log.d(TAG, "f attach: " + nextFragment.toString());
transaction.attach(nextFragment);
}
transaction.commit();
}
private Fragment createFragment(String tag) {
Fragment result = null;
switch (tag) {
case CALLHISTORY_FRAGMENT:
result = new AppCallHistoryFragment();
break;
case CALLTRACKING_FRAGMENT:
result = new AppCallTrackingFragment();
break;
case REDIRECTION_FRAGMENT:
result = new AppRedirectFragment();
break;
}
Log.d(TAG, "create: " + result.toString());
return result;
}
现在可以了.碎片只有在Activity销毁时才被销毁.
Now its ok. Fragments are Destroyed only when Activity is destroying.
这篇关于更改片段而无需重新创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!