我在Android中玩碎片。
我知道我可以通过使用以下代码更改一个片段:
FragmentManager fragMgr = getSupportFragmentManager();
FragmentTransaction fragTrans = fragMgr.beginTransaction();
MyFragment myFragment = new MyFragment(); //my custom fragment
fragTrans.replace(android.R.id.content, myFragment);
fragTrans.addToBackStack(null);
fragTrans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragTrans.commit();
我的问题是,在Java文件中,如何获得当前显示的片段实例?
你也可以很容易地在logcat中使用URL,它将重定向到当前片段源代码的源代码。首先,你需要在主机活动中添加一个OnBackStackChangedListener,比如-
activity.getChildFragmentManager().addOnBackStackChangedListener(backStackListener);
OnBackStackChangedListener的实现是-
public FragmentManager.OnBackStackChangedListener backStackListener = () -> {
String simpleName = "";
String stackName = getStackTopName().trim();
if (Validator.isValid(stackName) && stackName.length() > 0) {
simpleName = stackName.substring(Objects.requireNonNull(stackName).lastIndexOf('.') + 1).trim();
List<Fragment >
fragmentList = getChildFragmentManager().getFragments();
Fragment myCurrentFragment;
for (int i = 0; i < fragmentList.size(); i++) {
myCurrentFragment= fragmentList.get(i);
if (myCurrentFragment.getClass().getSimpleName().equals(simpleName)) {
//Now you get the current displaying fragment assigned in myCurrentFragment.
break;
}
myFragment = null;
}
}
//The code below is for the source code redirectable logcat which would be optional for you.
StackTraceElement stackTraceElement = new StackTraceElement(simpleName, "", simpleName + ".java", 50);
String fileName = stackTraceElement.getFileName();
if (fileName == null) fileName = "";
final String info = "Current Fragment is:" + "(" + fileName + ":" +
stackTraceElement.getLineNumber() + ")";
Log.d("now", info + "\n\n");
};
getStackTopName()方法是-
public String getStackTopName() {
FragmentManager.BackStackEntry backEntry = null;
FragmentManager fragmentManager = getChildFragmentManager();
if (fragmentManager != null) {
if (getChildFragmentManager().getBackStackEntryCount() > 0)
backEntry = getChildFragmentManager().getBackStackEntryAt(
getChildFragmentManager().getBackStackEntryCount() - 1
);
}
return backEntry != null ? backEntry.getName() : null;
}
我最近不得不这样做,这里没有一个答案真正适合这种情况。
如果你确信只有一个片段是可见的(全屏),那么真的要找到backstack顶部的内容。例如,作为Fragment的Kotlin:
import androidx.fragment.app.Fragment
fun Fragment.setVisibilityChangeListener(clazz: Class<out Fragment>, listener: (Boolean) -> Unit) {
fragmentManager?.run {
addOnBackStackChangedListener {
val topFragmentTag = getBackStackEntryAt(backStackEntryCount - 1).name
val topFragment = findFragmentByTag(topFragmentTag)
listener(topFragment != null && topFragment::class.java == clazz)
}
}
}
像这样使用它:
class MyFragment: Fragment {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setVisibilityChangeListener(this::class.java) { visible ->
// Do stuff
}
}
}