AlertDialog.Builder不会显示在当前片段

时间:2018-06-17 10:09:48

标签: android android-activity fragment alertdialog

所以我在TemplateHome活动中有这个名为appinfo的侧面菜单(默认片段是HomeFragment),它将在AlertDialog.Builder中显示应用版本

我能够很好地显示alertdialog,但每次点击appinfo菜单,该应用程序将始终转到HomeFragment / TemplateHome活动

现在有谁知道如何在当前片段中显示alertdialog?

这是我的菜单代码

public void switchhomefragment(MenuItem item) {
        fragment = null;
        Class fragmentClass = HomeFragment.class;

        switch (item.getItemId()) {
            case R.id.sidemenu_profil:
                fragmentClass = ProfileFragment.class;
                currentfragment = "home";
                break;
            case R.id.sidemenu_pencarian:
                fragmentClass = HomeFragment.class;
                currentfragment = "home";
                break;
            case R.id.sidemenu_pendaftaran:
                fragmentClass = PendaftaranFragment.class;
                currentfragment = "home";
                break;
            case R.id.sidemenu_fav:
                break;
            case R.id.sidemenu_tentang:
                fragmentClass = TentangFragment.class;
                currentfragment = "home";
                break;
            case R.id.sidemenu_info:
                infoaplikasi();
                break;
            case R.id.action_home:
                fragmentClass = HomeFragment.class;
                currentfragment = "home";
                break;
            case R.id.sidemenu_signout:
                editor.remove("sessioniduser");
                editor.commit();
                methodUmum.tologin(this);
                break;
            default:
                fragmentClass = HomeFragment.class;
                currentfragment = "home";
        }

        try {
            fragment = (Fragment) fragmentClass.newInstance();
        } catch (Exception e) {
            Log.d("templatehome", "isi error : " + e);
            e.printStackTrace();
        }

        fragmentManager.beginTransaction().addToBackStack(null).replace(R.id.flContent, fragment, currentfragment).commit();
        item.setChecked(true);
        mDrawer.closeDrawers();
    }

这是我调用switchhomefragment代码的地方

@SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        switchhomefragment(item);
        return true;
    }

这是AlertDialog.Builder代码

public void infoaplikasi() {
        dialog = new AlertDialog.Builder(TemplateHome.this);
        inflater = getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.info_aplikasi, null);
        dialog.setView(dialogView);
        dialog.setCancelable(true);
        dialog.setNegativeButton("TUTUP", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        dialog.show();
    }

这是我的info_aplikasi xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="@dimen/ukr16"
    android:paddingRight="@dimen/ukr16"
    android:paddingTop="@dimen/ukr16">

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingBottom="@dimen/ukr8"
        android:text="App Version"
        android:textAllCaps="true"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="version 2.1"
        android:textSize="16sp" />
</LinearLayout>

任何建议都有帮助,谢谢你

2 个答案:

答案 0 :(得分:1)

在我的应用中,弹出对话框,因此infoaplikasi方法中的代码正常。我认为问题是switchhomefragment方法。你称之为正确吗?在该方法的infoaplikasi();上放置一个断点,看看它是否被执行。另外,向我们展示R.layout.info_aplikasi,也许这是一个问题。

PS。在代码中使用适当的名称约定。可以在此处找到java的代码约定:http://www.oracle.com/technetwork/java/codeconventions-135099.html

答案 1 :(得分:0)

感谢我的不好,我才意识到我使用HomeFragment.class设置了fragmentclass值。这就是为什么警报会一直持续到HomeFragment

相关问题