0

FragmentComments is a fragment,which has a button.And on this button press a dialog opens(asking whether you want to exit or not).If no is pressed,it does nothing and closes dialog,when yes is pressed,it finishes the current activity,and opens some other random activity.BUT I want it to open another specific activity (say newactivity).How can I do this ?

public class FragmentComments extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View view = inflater.inflate(R.layout.fragment_comments, container, false);
            Button submit = (Button) view.findViewById(R.id.submit);

            submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());

                    // set title
                    alertDialogBuilder.setTitle("Submit");

                    // set dialog message
                    alertDialogBuilder
                            .setMessage("Are you sure,you want to submit the feedback ?")
                            .setCancelable(false)
                            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int id) {
                                    // if this button is clicked, close
                                    // current activity
                                    FragmentComments.this.getActivity().finish();

//What should I add here to finish the current activity as well as open a new other activity(say newactivity).

                                }
                            })
                            .setNegativeButton("No",new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int id) {
                                    // if this button is clicked, just close
                                    // the dialog box and do nothing
                                    dialog.cancel();
                                }
                            });

                    // create alert dialog
                    AlertDialog alertDialog = alertDialogBuilder.create();

                    // show it
                    alertDialog.show();

                }
            });
            return view;
        }
    }
Somya Arora
  • 87
  • 11
  • 1
    FragmentComments.this.getActivity().finish(); startActivity(new Intent(getActivity(),NewActivity.class)); – Krupal Shah Mar 11 '16 at 15:36
  • Thanks,It is working. – Somya Arora Mar 11 '16 at 15:47
  • Possible duplicate of [How to start new activity on button click](http://stackoverflow.com/questions/4186021/how-to-start-new-activity-on-button-click) – Vucko Mar 11 '16 at 15:51
  • This is literally just a question on how to start an activity... And instead of searching on your own, you post it here :) – Vucko Mar 11 '16 at 15:52

0 Answers0