按OK后,Android显示对话框警报未退出屏幕

时间:2016-02-04 08:45:58

标签: android

我在片段中开发了一个show dialog alert框。按下框中的“确定”按钮后,它不会退出屏幕。显示对话框警报会在屏幕上显示提示。按OK后我不想退出应用程序,但我希望它返回到HomeScreen。按OK后如何确保显示对话框警告框退出屏幕?

这是代码

    public class FavouriteListFragment extends Fragment {

    public static final String ARG_ITEM_ID = "favorite_list";
    private SharedPreference sharedPreference;
    private StaggeredGridView mStaggeredView;
    TextView tv;
    ImageView iv;
    String text;
    String favouriteUrl;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_favourite_staggeredgridview, container, false);
        mStaggeredView = (StaggeredGridView) rootView.findViewById(R.id.staggeredview);
        mStaggeredView.setOnScrollListener(scrollListener);
        sharedPreference = new SharedPreference();

       // iv=(ImageView)rootView.findViewById(R.id.imageView);
        text = sharedPreference.getValue(getActivity());

        sharedPreference.saveFavourite(getActivity(), text);

        String[] photoUrl;
        photoUrl = new String[10];

        if(photoUrl==null){
            Toast.makeText(getActivity(),"no favourite list",Toast.LENGTH_SHORT).show();
            showAlert(getResources().getString(R.string.no_favorites_items),
                    getResources().getString(R.string.no_favorites_msg));
        } else {
            if (photoUrl.length == 0) {
                showAlert(
                        getResources().getString(R.string.no_favorites_items),
                        getResources().getString(R.string.no_favorites_msg));
            }
        }

        for (int index = 0; index < photoUrl.length; index++) {

            photoUrl[index]=text;
            if(text==null){
                showAlert(
                        getResources().getString(R.string.no_favorites_items),
                        getResources().getString(R.string.no_favorites_msg));
            }
            else {
                StaggeredGridViewItem item;
                item = new FavouriteGridItem(getActivity(), photoUrl); //pass one image of index
                mStaggeredView.addItem(item);
            }
        }

//        URL url = null;
//        try {
//            url = new URL(text);
//        } catch (MalformedURLException e) {
//            e.printStackTrace();
//        }
//        Bitmap bmp = null;
//        try {
//            bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        iv.setImageBitmap(bmp);
        return rootView;

    }

    private void showAlert(String string, String message) {

        if (getActivity() != null && !getActivity().isFinishing()) {
            AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
                    .create();
            alertDialog.setMessage(message);
            alertDialog.setCancelable(false);

            // setting OK Button
            alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            //getActivity().finish();
                            getFragmentManager().popBackStackImmediate();
                        }
                    });
            alertDialog.show();
        }
    }

    private StaggeredGridView.OnScrollListener scrollListener = new StaggeredGridView.OnScrollListener() {
        public void onTop() {
        }

        public void onScroll() {

        }

        public void onBottom() {

        }
    };

    @Override
    public void onResume() {
        super.onResume();
    }
}

0 个答案:

没有答案