Jéw*_*ôm' 0 java android android-fragments android-framelayout
我有framelayout一次在我Activity和其他时间的不同时间Fragment(经过相同framelayout的Activity)。
在 my 中MainActivity.class,我需要知道我的framelayout. 例如,我需要知道是否framelayout正在使用MyFragment1.class或MyFragment2.class。
我需要这样的东西(在这个例子中,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");
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
You need to use below code to get runtime fragment from FrameLayout
Fragment fragmentInFrame = (Fragemnt)getSupportFragmentManager()
.findFragmentById(R.id.frag2_view);
Run Code Online (Sandbox Code Playgroud)
then you have check fragment type using instanceof
if (fragmentInFrame instanceof MyFragment1){
Log.d("debug", "you are in MyFragment1");
}else if (fragmentInFrame instanceof MyFragment2){
Log.d("debug", "you are in MyFragment2");
}
Run Code Online (Sandbox Code Playgroud)