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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:38:25  来源:igfitidea点击:

Get the fragment name or id

javaandroidandroid-fragmentsandroid-framelayoutid

提问by Jéw?m'

I am have a framelayoutcalled one time in my Activityand various time in other Fragment(passing by the same framelayoutof the Activity).

framelayout在我的Activity一次和其他的不同时间Fragment(经过相同framelayoutActivity)被称为一次。

In my MainActivity.class, I need to know in what fragment is my framelayout. For example, I need to know if framelayoutis using MyFragment1.classor MyFragment2.class.

在 my 中MainActivity.class,我需要知道我的framelayout. 例如,我需要知道是否framelayout正在使用MyFragment1.classMyFragment2.class

I need something like this (in this example, the loghave 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 ContentFrameLayoutthis Id Is Fragment_Main

我有一个ContentFrameLayout这个 IDFragment_Main

You Can Use FrameLayout

您可以使用 FrameLayout

To ActivityWrite This code

为了Activity写这个代码

final Fragment fragmentInFrame = getSupportFragmentManager().findFragmentById(R.id.Fragment_Main);

if(fragmentInFrame instanceof YourFragment){

}

To FragmentWrite 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 fragmentInFramewill still refer to old fragment.

您正在此处替换片段。所以fragmentInFrame仍然会引用旧片段。

So after replacing you need to update fragmentInFramevariable by finding it again.

因此,替换后您需要fragmentInFrame通过再次查找来更新变量。

fragmentInFrame = getSupportFragmentManager().findFragmentById(R.id.framelayout);

Now checking the fragmentInFramewill work.

现在检查fragmentInFrame将工作。

Don't forget to remove the finalkeyword.

不要忘记删除final关键字。

回答by Sneh Pandya

In your each Fragment class, just define Log inside onCreateViewmethod.

在您的每个 Fragment 类中,只需定义 Log insideonCreateView方法。