1

I create a simple application in that I used DialogFragment, so I created feedback_dialog_framgment for store feedbacks in firebase, data was stored successfully but in OnComplete() my toast message was showing any msg, so check this

FeedbackDialog.java

public class FeedBackDialog extends DialogFragment implements View.OnClickListener {


    private static final String TAG = "Feedback";

    public EditText fname,fmobile,faddress, fmessage;
    public Button fsubmit , fcancel;
    private ImainActivity imainActivity;


    @Override
    public void onClick(View v) {

        switch (v.getId ()){
            case R.id.fsubmit:{
                String name= fname.getText ().toString ();
                String mobile= fname.getText ().toString ();
                String address= fname.getText ().toString ();
                String message = fmessage.getText ().toString ();
                if(!name.equals ( "" ) && !mobile.equals ( "" ) && !address.equals ( "" ) && !message.equals ( "" )){
                   ImainActivity imainActivity = new ImainActivity ( ) {
                       @Override
                       public void createNewFeedback(String name, String mobile, String address, String message) {
                           FirebaseFirestore db = FirebaseFirestore.getInstance ();

                           DocumentReference feedbackDocRef= db.collection ( "FeedBacks" ).document ();


                           String userId = FirebaseAuth.getInstance ().getCurrentUser ().getUid ();

                           FeedbackData feedbackData = new FeedbackData (  );
                           feedbackData.setFname ( name );
                           feedbackData.setFmobile ( mobile );
                           feedbackData.setFaddress ( address );
                           feedbackData.setFmessage ( message );
                           Date timestamp = null;
                           feedbackData.setTimestamp ( timestamp );
                           feedbackData.setUserid(userId);


                           feedbackDocRef.set ( feedbackData ).addOnCompleteListener ( new OnCompleteListener <Void> ( ) {
                               @Override
                               public void onComplete(@NonNull Task <Void> task) {
                                   if(task.isSuccessful ()){

                                       Toast.makeText ( getApplicationContext(), "Feedback Send Successfully!", Toast.LENGTH_SHORT ).show ( );

                                   }else{
                                       Toast.makeText ( getApplicationContext(), "Feedback didn't Send.", Toast.LENGTH_SHORT ).show ( );

                                   }

                               }
                           } ).addOnFailureListener ( new OnFailureListener ( ) {
                               @Override
                               public void onFailure(@NonNull Exception e) {
                                   }
                           } );
                       }
                   };
                   imainActivity.createNewFeedback ( name,mobile,address,message );

                    getDialog ().dismiss ();

                }else{
                    Toast.makeText ( getActivity (), "Enter All Fields...", Toast.LENGTH_SHORT ).show ( );
                }break;
            }
            case R.id.fcancel:{
                getDialog ().dismiss ();
                break;
            }
            case R.id.btn_close:{
                getDialog ().dismiss ();
                break;
            }
            default:
                throw new IllegalStateException ( "Unexpected value: " + v.getId ( ) );
        }
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach ( context );
        imainActivity = (ImainActivity) getActivity ();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );

        int style = DialogFragment.STYLE_NO_TITLE;
        int theme = android.R.style.Theme_Holo_Light_Dialog;
        setStyle ( style,theme );
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_feedback, container, false);
        fname = view.findViewById(R.id.fname);
        fmobile = view.findViewById ( R.id.fmobile );
        faddress = view.findViewById ( R.id.faddress );
        fmessage = view.findViewById(R.id.fmessage);
        fsubmit = view.findViewById(R.id.fsubmit);
        fcancel = view.findViewById(R.id.fcancel);

        fcancel.setOnClickListener(this);
        fsubmit.setOnClickListener(this);

        getDialog().setTitle("Feedback");

        return view;
    }
}
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Somesh
  • 23
  • 7

4 Answers4

1

a Toast is a kind of UI change, so you can't call it from worker threads like in your case Firebase callbacks, so you need to run it explicitly on UI/main thread.

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(requireActivity(), "My Toast", Toast.LENGTH_SHORT).show();
    }
});

UPDATE

If above not worked try below

private void showToast() {
    Handler handler = new Handler(Looper.getMainLooper());
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(mContext, "My Toast", Toast.LENGTH_SHORT).show();
        }
    }, 1000 );
}   
Zain
  • 37,492
  • 7
  • 60
  • 84
  • I did everything but error/my problem was not solved, pls tell me how to resolve this. – Somesh Aug 07 '20 at 19:38
  • @Somesh thanks for your comment, I just updated the answer, hope it works – Zain Aug 07 '20 at 20:00
  • Sorry but it's not working, is not showing any toast, do you have any other options for ..msg like toast, snack massage, alert msg or anything thing. I did snack message also it's also not showing any thing, on my feedback dialog fragment – Somesh Aug 08 '20 at 11:34
  • You may use Log class methods if you want to debug something – Zain Aug 08 '20 at 17:32
0

Try to change:

Toast.makeText ( getApplicationContext() ...

To:

Toast.makeText ( getActivity()....
Don Ha
  • 689
  • 5
  • 9
0

Try using feedbackDocRef.addOnSuccessListener() {...} instead of feedbackDocRef.addOnCompleteListener(){...} and remove the check for task successful i.e if(task.isSuccessful(){...}

snk
  • 41
  • 3
0

Because sub-events the Toast message is not Showing any message to resolve,

Try use 'await' with events

try {
    // Block on a task and get the result synchronously. This is generally done
    // when executing a task inside a separately managed background thread. Doing this
    // on the main (UI) thread can cause your application to become unresponsive.
    AuthResult authResult = Tasks.await(task);
} catch (ExecutionException e) {
    // The Task failed, this is the same exception you'd get in a non-blocking
    // failure handler.
    // ...
} catch (InterruptedException e) {
    // An interrupt occurred while waiting for the task to complete.
    // ...
}