Android 无法在后台添加/恢复碎片sameclass
我使用一个活动来处理多个项目的详细信息Android 无法在后台添加/恢复碎片sameclass,android,android-fragments,Android,Android Fragments,我使用一个活动来处理多个项目的详细信息 ActivityA有一个产品列表,当您点击该列表中的任何项目时,您可以调用ActivityB传递产品ID: 活动a: Intent i = new Intent(this, ProductDetail.class); i.putExtra("pID", product.getId()); i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); i.addFlags(Intent.FLAG_ACTI
ActivityA
有一个产品列表,当您点击该列表中的任何项目时,您可以调用ActivityB
传递产品ID:
活动a
:
Intent i = new Intent(this, ProductDetail.class);
i.putExtra("pID", product.getId());
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
startActivity(i);
在onCreate
和onNewIntent
方法中,我以如下方式处理它:
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Bundle bundle = new Bundle(); //bundle to send data to the fragment
bundle.putInt("pID", intent.getExtras().getInt("pID"));
ProductDetail details = new ProductDetail();
details.setArguments(bundle);
loadFragment(details);
}
以及loadFragment
,如果找到具有相同pID标记的片段,则必须添加或返回到特定片段:
public void loadFragment(ProductDetail fragment) {
fm = getFragmentManager();
Log.d("TagFrag", "TAG: "+ pID); //can get pID from global variable or extra from fragment
if(fm.findFragmentByTag(pID+"") !=null) {
Log.d("TagFrag", "Already Added");
fm.popBackStack(pID+ "", 0);
} else {
Log.d("TagFrag", "New Added");
ft = fm.beginTransaction();
ft.add(R.id.fragments, fragment, pID+ "");
ft.commit();
fm.executePendingTransactions();
}
它显示它是“新添加的”
还是“已添加的”
,但是fm.popbackbackstack(pID+”,0)代码>仅显示最后添加的片段
例:
单击项目1->添加片段->显示项目1详细信息
单击项目2->添加片段->显示项目2详细信息
如果返回到项目列表并再次单击项目1,则行为为:
单击项目1->fragmentAlreadyAdded->显示项目2详细信息
为什么它不再显示项目1
你真的给backbackback加了什么东西吗?选中fm.getbackbackbackentrycount()
。如果0,那么您需要添加事务:ft.addToBackStack(tag)
它添加到后台,entryCount正在计数,问题是当我使用popBack返回到第一个片段时,所有其他片段都会被销毁,我不希望此=/@user2582318从您的源代码中看到addToBackStack调用并不明显。不管怎样,你是否考虑过使用StuttTrink的显示/隐藏方法?对不起,我忘了添加,并且我没有考虑到,无论如何,在不丢失数据的情况下存储片段,然后显示/隐藏吗?