java 获取片段名称或 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42439367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the fragment name or id
提问by Jéw?m'
I am have a framelayout
called one time in my Activity
and various time in other Fragment
(passing by the same framelayout
of the Activity
).
我framelayout
在我的Activity
一次和其他的不同时间Fragment
(经过相同framelayout
的Activity
)被称为一次。
In my MainActivity.class
, I need to know in what fragment is my framelayout
.
For example, I need to know if framelayout
is using MyFragment1.class
or MyFragment2.class
.
在 my 中MainActivity.class
,我需要知道我的framelayout
. 例如,我需要知道是否framelayout
正在使用MyFragment1.class
或MyFragment2.class
。
I need something like this (in this example, the log
have to say me "you are in MyFragment1") :
我需要这样的东西(在这个例子中,log
必须说我“你在 MyFragment1”):
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.framelayout);
getSupportFragmentManager().beginTransaction()
.replace(R.id.framelayout,new MyFragment1())
.commit();
if (framelayout.getname.equals("package.MyFragment1.class"))
Log.d("debug", "you are in MyFragment1");
else if (framelayout.getname.equals("package.MyFragment2.class"))
Log.d("debug", "you are in MyFragment2");
But I don't know how I can do that.
但我不知道我怎么能做到这一点。
回答by N J
You need to use below code to get runtime fragmentfrom FrameLayout
您需要使用下面的代码来获得运行时 的片段从FrameLayout
Fragment fragmentInFrame = (Fragemnt)getSupportFragmentManager()
.findFragmentById(R.id.frag2_view);
then you have check fragment type using instanceof
然后你有检查片段类型使用 instanceof
if (fragmentInFrame instanceof MyFragment1){
Log.d("debug", "you are in MyFragment1");
}else if (fragmentInFrame instanceof MyFragment2){
Log.d("debug", "you are in MyFragment2");
}
回答by Jéw?m'
I found the solution
我找到了解决方案
getSupportFragmentManager().beginTransaction()
.replace(R.id.framelayout,new MyFragment1())
.commit();
final Fragment fragmentInFrame = getSupportFragmentManager().findFragmentById(R.id.framelayout);
if (fragmentInFrame instanceof MyFragment1){
Log.d("debug", "you are in MyFragment1");
} else if (fragmentInFrame instanceof MyFragment2) {
Log.d("debug", "you are in MyFragment2");
}
回答by Diako Hasani
I Have One ContentFrameLayout
this Id Is Fragment_Main
我有一个ContentFrameLayout
这个 IDFragment_Main
You Can Use FrameLayout
您可以使用 FrameLayout
To Activity
Write This code
为了Activity
写这个代码
final Fragment fragmentInFrame = getSupportFragmentManager().findFragmentById(R.id.Fragment_Main);
if(fragmentInFrame instanceof YourFragment){
}
To Fragment
Write This Code
为了Fragment
编写该代码
final Fragment fragmentInFrame =getActivity().getSupportFragmentManager().findFragmentById(R.id.Fragment_Main);
if(fragmentInFrame instanceof YourFragment){
}
回答by Jd Prajapati
You need to check using instance like
您需要使用类似的实例进行检查
you need to check with your fragment object:
你需要检查你的片段对象:
public Fragment getVisibleFragment(){
FragmentManager fragmentManager = MainActivity.this.getSupportFragmentManager();
List<Fragment> fragments = fragmentManager.getFragments();
if(fragments != null){
for(Fragment fragment : fragments){
if(fragment != null && fragment.isVisible())
return fragment;
}
}
return null;
}
fragment = getVisibleFragment();
if (fragment instance of MyFragment1){
}
回答by Shreyash S Sarnayak
final Fragment fragmentInFrame = getSupportFragmentManager().findFragmentById(R.id.framelayout);
getSupportFragmentManager().beginTransaction()
.replace(R.id.framelayout, new MyFragment1())
.commit();
You are replacing the fragment here. So the fragmentInFrame
will still refer to old fragment.
您正在此处替换片段。所以fragmentInFrame
仍然会引用旧片段。
So after replacing you need to update fragmentInFrame
variable by finding it again.
因此,替换后您需要fragmentInFrame
通过再次查找来更新变量。
fragmentInFrame = getSupportFragmentManager().findFragmentById(R.id.framelayout);
Now checking the fragmentInFrame
will work.
现在检查fragmentInFrame
将工作。
Don't forget to remove the final
keyword.
不要忘记删除final
关键字。
回答by Sneh Pandya
In your each Fragment class, just define Log inside onCreateView
method.
在您的每个 Fragment 类中,只需定义 Log insideonCreateView
方法。