【问题标题】:How to Hide Progress Dialog from another Fragment - Android如何从另一个片段中隐藏进度对话框 - Android
【发布时间】:2014-02-25 18:18:45
【问题描述】:
我有三个标签(使用 Fragments/ViewPager/SupportLibrary/FragmentActivity)。
左侧选项卡在初始加载时使用 ProgressDialog。但我默认使用中心选项卡。然而,当我第一次进入此活动并登陆该中心选项卡时,选项卡 #1 中的 ProgressDialog 显示在选项卡 #2(中心)上。
有没有办法防止这种情况发生?
【问题讨论】:
标签:
android
progressdialog
android-fragmentactivity
【解决方案1】:
我使用布尔值阻止了这种情况。我不认为这是一个非常干净的方式。只需在选择选项卡时将布尔值设置为 true 并仅在选择选项卡时执行异步任务。
所以在我的 MainActivity 中我有一个 3 的数组
public static boolean[] selected = new boolean[3];(静态与我使用的外部类有关,在您的情况下可能不需要)
我在每个选项卡上都有描述
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft)
{
getSupportFragmentManager().popBackStack();
if (tab.getContentDescription() == "tab1")
{
mViewPager.setAdapter(mAppSectionsPagerAdapter);
selected[0] = true;
selected[1] = false;
selected[2] = false;
mAppSectionsPagerAdapter.notifyDataSetChanged();
}
if (tab.getContentDescription() == "tab2")
{
mViewPager.setAdapter(mAppSectionsPagerAdapter);
selected[0] = false;
selected[1] = true;
selected[2] = false;
mAppSectionsPagerAdapter.notifyDataSetChanged();
}
if (tab.getContentDescription() == "tab3")
{
mViewPager.setAdapter(mAppSectionsPagerAdapter);
selected[0] = false;
selected[1] = false;
selected[2] = true;
mAppSectionsPagerAdapter.notifyDataSetChanged();
}
在片段中:
if (MainActivity.selected[0])
{
// asynctask fragment
}