简体   繁体   中英

Alert Dialog jump to other activity

I had 2 button in Alert Dialog , I want to make it like if "EXIT" clicked , it will jump to MainActivity , when "Restrat" clicked it will jump to B_Activity Thanks

alertDialog.setButton(AlertDialog.BUTTON_POSITIVE,"Exit", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {


                }

            });
            alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Restart", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                }

I want to make it like if "EXIT" clicked , it will jump to MainActivity

I guess you are on MainActivity when you open the dialog so, with a simple dialog.dismiss(); it will retour you to the MainActivity if not you'll have to make an intent as follows

alertDialog.setButton(AlertDialog.BUTTON_POSITIVE,"Exit", new DialogInterface.OnClickListener() {

  @Override
  public void onClick(DialogInterface dialog, int which) {
      dialog.dismiss(); //if you are on `MainActivity`
      Intent i = new Intent(getContext(), MainActivity.class); //if under this dialog you do not have your MainActivity
      getContext().startActivity(i);
  }
});

when "Restrat" clicked it will jump to B_Activity

You have to make an Intent as follows

alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Restart", new DialogInterface.OnClickListener() {

  @Override
  public void onClick(DialogInterface dialog, int which) {
     Intent i = new Intent(getContext(), B_Activity.class)
     getContext().startActivity(i);
  }

NOTE : After ask a question just read some basics about Android I'll explain a little bit the code.

Why use dialog.dismiss(); ?

Dismiss this dialog, removing it from the screen.

Why use Intent ?

An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.

What's this getContext() ?

Retrieve the Context this Dialog is running in and returns the Context used by the Dialog.

Hope this answer helped to solve your problem.

You can use Intent to start an Activity,like that :

Intent i = new Intent(YourCurrentActivity.this, TargetActivity.class); 
startActivity(i);

And write this code in the onClick method.

You can easily swith your activities with the following code:

Intent myIntent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(myIntent);

But please take a time and read the documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM