为什么getFragmentManager()。findFragmentByTag(将String.valueOf(某个整数))不工作? [英] Why is getFragmentManager().findFragmentByTag(String.valueOf(SOME INTEGER)) NOT working?
问题描述
我有碎片的堆栈和使用getFragmentManager试图返回在栈()。findFragmentByTag()方法。
I have a stack of Fragments and trying go back in the stack using getFragmentManager().findFragmentByTag() method.
这方法确实返回对象,如果称这种方式:
This method does return the object if called this way:
getFragmentManager().findFragmentByTag("3")
CountingFragment {40d68d00#2 ID = 0x7f090000 3}
CountingFragment{40d68d00 #2 id=0x7f090000 3}
但是,如果使用的变量作为自变量,它会返回NULL:
But it will return NULL if I use the variable as an argument:
int nextLevel = currentPositionInTheStack+1;
getFragmentManager().findFragmentByTag(String.valueOf(nextLevel));
或
int nextLevel = currentPositionInTheStack+1;
String nextLevelTag = "a" + nextLevel;
getFragmentManager().findFragmentByTag(nextLevelTag);
任何建议,为什么发生这种情况,
Any suggestions why this is happening,
感谢您提前
推荐答案
想通了如何浏览前进后退堆栈中的:
Have figured out how to navigate forward and back in the stack:
对于前进导航,如果一个碎片已经存在,使用隐藏()和show()方法:
for the forward navigation , if next fragment already exists , used hide() and show() methods:
Button button = (Button)findViewById(R.id.new_fragment);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
CountingFragment currentFragment = (CountingFragment)getFragmentManager().findFragmentByTag(String.valueOf(currentPositionInTheStack));
int nextLevel = currentPositionInTheStack+1;
currentPositionInTheStack++;
CountingFragment nextFragment = (CountingFragment)getFragmentManager().findFragmentByTag(String.valueOf(currentPositionInTheStack));
if (nextFragment == null) {
addFragmentToStack();
}
else
{
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.hide(currentFragment);
fragmentTransaction.show(nextFragment);
fragmentTransaction.commit();
}
}
});
有关的后退导航,不得不重写onBack pressed方式:
For the back navigation , had to override onBackPressed method:
public void onBackPressed() {
super.onBackPressed();
currentPositionInTheStack--;
Log.d("Current Position In The Stack", String.valueOf(currentPositionInTheStack));
if (currentPositionInTheStack ==0) {
//do smth
} }
该片段在原来的问题是NULL,应该为null,但是,为什么它返回的对象,如果我这样做getFragmentManager()。findFragmentByTag(3),例如,目前尚不清楚。
The Fragment in the original question was NULL and should be null, though, why it returned the Object , if I did getFragmentManager().findFragmentByTag("3"), for example, is not clear.
这篇关于为什么getFragmentManager()。findFragmentByTag(将String.valueOf(某个整数))不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!