Search code examples
javaandroidandroid-studioandroid-dialogfragment

android: how to show ProgressDialogFragment in MainActivity


I got some problem on code below, when i build the app it show error at the MainActivity side. Below is the code: ProgressDialogFragment.java

public class ProgressDialogFragment extends DialogFragment
{

    public static ProgressDialogFragment newInstance() {
        ProgressDialogFragment frag = new ProgressDialogFragment ();
        return frag;
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setCancelable(false);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        ProgressDialog dialog = new ProgressDialog(getActivity(), getTheme());
        dialog.setTitle(getString(R.string.updatename_loading_title));
        dialog.setMessage(getString(R.string.updatename_loading_message));
        dialog.setIndeterminate(true);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        return dialog;
    }

}

MainActivity.java

public void doPositiveClick(DialogFragment dialog) {
                Dialog dialogView = dialog.getDialog();
                EditText editName = (EditText) dialogView.findViewById(R.id.editName);
                String updateName = editName.getText().toString().trim();
                    login.setName(updateName);
                    new HttpAsyncTask().execute(uNameUrl);
                    dialogView.dismiss();
            }

        private class HttpAsyncTask extends AsyncTask<String, Void, String> {

                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                   progressDialog.show(MainActivity.getFragmentManager(),"loading");
                }

                @Override
                protected String doInBackground(String... urls) {

                    // Do Action Here
                }
    }

I want to show loading after submit, but got error at line below:

progressDialog.show(MainActivity.getFragmentManager(),"loading"); 

Error Message below show after i build the app.

Error:(216, 45) error: non-static method getFragmentManager() cannot be referenced from a static context

How can i fix this? Thanks

@Edit I fixed the first error, i just forgot to add this on ProgressDialogFragment.java

@Override
    public void show(FragmentManager manager, String tag) {
        if (manager.findFragmentByTag(tag) == null) {
            super.show(manager, tag);
        }
    }

for prevent duplicate dialog when rotate the mobile. Now it got second error

Error:(40, 5) error: method does not override or implement a method from a supertype
Error:(43, 18) error: no suitable method found for show(android.app.FragmentManager,String)
        method DialogFragment.show(android.support.v4.app.FragmentManager,String) is not applicable
        (argument mismatch; android.app.FragmentManager cannot be converted to android.support.v4.app.FragmentManager)
        method DialogFragment.show(FragmentTransaction,String) is not applicable
        (argument mismatch; android.app.FragmentManager cannot be converted to FragmentTransaction)

Solution

  • You are call outer class method in inner class, you should change MainActivity.getFragmentManager() to MainActivity.this.getFragmentManager()