在加载片段之前检查视图是否存在
Checking if a view exists before loading a fragment
我想在加载片段之前查看视图是否存在。在我加载片段的方法中,我有以下内容:
public void loadFragment(Fragment fragment){
FragmentManager fragmentManager = Objects.requireNonNull(getActivity()).getSupportFragmentManager();
// Begin Fragment transaction.
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();// Replace the layout holder with the required Fragment object.
fragmentTransaction.replace(R.id.outbound_dashboard_hourly_queued, fragment);
// Commit the Fragment replace action.
fragmentTransaction.commit();
}
我想要的是在加载
之前检查R.id.outbound_dashboard_hourly_queued
是否存在
我该如何进行?
试试这个
检查是否添加了Fragment
if (getCurrentFragment() instanceof YourFragment)
奇数片段
replaceFragment(new HomeFragment(), "YourFragment");
方法
private void replaceFragment(Fragment fragment, String tag)
{
Fragment currentFragment = getCurrentFragment();
if (currentFragment != null)
getSupportFragmentManager().beginTransaction().remove(currentFragment).commit();
getSupportFragmentManager().beginTransaction().add(R.id.container_main, fragment, tag).commit();
}
public Fragment getCurrentFragment()
{
FragmentManager manager = getSupportFragmentManager();
return manager.findFragmentById(R.id.container_main);
}
private void popBackStack()
{
FragmentManager fragmentManager = getSupportFragmentManager();
int total = fragmentManager.getBackStackEntryCount();
for (int i = 0; i < total; i++)
{
fragmentManager.popBackStack();
}
}
只需执行 findViewById(R.id.outbound_dashboard_hourly_queued)
然后加载片段,如果视图不是 Null 保持 if 检查。
public void loadFragment(Fragment fragment){
FragmentManager fragmentManager = Objects.requireNonNull(getActivity()).getSupportFragmentManager();
// Begin Fragment transaction.
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();// Replace the layout holder with the required Fragment object.
View yourView= findViewById(R.id.outbound_dashboard_hourly_queued);
if(yourView!=null){
fragmentTransaction.replace(R.id.outbound_dashboard_hourly_queued, fragment);
// Commit the Fragment replace action.
fragmentTransaction.commit();
}
}
您可以使用FragmentManager提供的findFragmentByTag(String tag)方法来检查片段是否已经存在。
Fragment fragment = getSupportFragmentManager().findFragmentByTag(TAG);
为此,您需要使用 -
提交带有该 TAG 的片段
fragmentTransaction.replace(R.id.outbound_dashboard_hourly_queued, fragment, TAG);
如果计数 == 0 添加,请在替换之前遵循计数检查,否则替换您的代码必须像
public void loadFragment(Fragment fragment){
FragmentManager fragmentManager = Objects.requireNonNull(getActivity()).getSupportFragmentManager();
// Begin Fragment transaction.
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();// Replace the layout holder with the required Fragment object.
if(fragmentManager.getFragments().size() == 0){
fragmentTransaction.add(R.id.outbound_dashboard_hourly_queued, fragment).commit();
}else {
fragmentTransaction.replace(R.id.outbound_dashboard_hourly_queued, fragment).commit();
}
// Commit the Fragment replace action.
}
请试试这个并告诉我。
我想在加载片段之前查看视图是否存在。在我加载片段的方法中,我有以下内容:
public void loadFragment(Fragment fragment){
FragmentManager fragmentManager = Objects.requireNonNull(getActivity()).getSupportFragmentManager();
// Begin Fragment transaction.
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();// Replace the layout holder with the required Fragment object.
fragmentTransaction.replace(R.id.outbound_dashboard_hourly_queued, fragment);
// Commit the Fragment replace action.
fragmentTransaction.commit();
}
我想要的是在加载
之前检查R.id.outbound_dashboard_hourly_queued
是否存在
我该如何进行?
试试这个
检查是否添加了Fragment
if (getCurrentFragment() instanceof YourFragment)
奇数片段
replaceFragment(new HomeFragment(), "YourFragment");
方法
private void replaceFragment(Fragment fragment, String tag)
{
Fragment currentFragment = getCurrentFragment();
if (currentFragment != null)
getSupportFragmentManager().beginTransaction().remove(currentFragment).commit();
getSupportFragmentManager().beginTransaction().add(R.id.container_main, fragment, tag).commit();
}
public Fragment getCurrentFragment()
{
FragmentManager manager = getSupportFragmentManager();
return manager.findFragmentById(R.id.container_main);
}
private void popBackStack()
{
FragmentManager fragmentManager = getSupportFragmentManager();
int total = fragmentManager.getBackStackEntryCount();
for (int i = 0; i < total; i++)
{
fragmentManager.popBackStack();
}
}
只需执行 findViewById(R.id.outbound_dashboard_hourly_queued)
然后加载片段,如果视图不是 Null 保持 if 检查。
public void loadFragment(Fragment fragment){
FragmentManager fragmentManager = Objects.requireNonNull(getActivity()).getSupportFragmentManager();
// Begin Fragment transaction.
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();// Replace the layout holder with the required Fragment object.
View yourView= findViewById(R.id.outbound_dashboard_hourly_queued);
if(yourView!=null){
fragmentTransaction.replace(R.id.outbound_dashboard_hourly_queued, fragment);
// Commit the Fragment replace action.
fragmentTransaction.commit();
}
}
您可以使用FragmentManager提供的findFragmentByTag(String tag)方法来检查片段是否已经存在。
Fragment fragment = getSupportFragmentManager().findFragmentByTag(TAG);
为此,您需要使用 -
提交带有该 TAG 的片段fragmentTransaction.replace(R.id.outbound_dashboard_hourly_queued, fragment, TAG);
如果计数 == 0 添加,请在替换之前遵循计数检查,否则替换您的代码必须像
public void loadFragment(Fragment fragment){
FragmentManager fragmentManager = Objects.requireNonNull(getActivity()).getSupportFragmentManager();
// Begin Fragment transaction.
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();// Replace the layout holder with the required Fragment object.
if(fragmentManager.getFragments().size() == 0){
fragmentTransaction.add(R.id.outbound_dashboard_hourly_queued, fragment).commit();
}else {
fragmentTransaction.replace(R.id.outbound_dashboard_hourly_queued, fragment).commit();
}
// Commit the Fragment replace action.
}
请试试这个并告诉我。