13KZ
13KZ

Reputation: 1335

Started an Activity from an AlertDialog?

I'm working on an Android project which allow the user to display an AlertDialog. This contains controls like checkbox textview and two buttons (dismiss, validate).

So I'm trying to start an Activity from this AlertDialog to an Activity by the intent which it seems impossible.

public Intent (Context packageContext, Class<?> cls)

I already ready read a lot of thread but anyone really helpful

Is there a another way to figure this out?

EDIT 1:

The code below describe my Class InProgressAlertDialog

public class InProgressAlertDialog extends Dialog implements View.OnClickListener{

    public InProgressAlertDialog(Context context) {

    }
    public void onClick(View v) {
       // where I dismiss the AlertDialog or Start an Activity
    }

 private void initialiseControls(xxxxx)
 {  
  //where initialize all my controls
 setContentView(R.layout.xxxxxxxxxx);
 linearMain =  (LinearLayout)findViewById(R.xxxxxxxxx.yyyyyyyy);
 linearMain.setOrientation(LinearLayout.VERTICAL);

 linearButton = new LinearLayout(_context);
 btnValide = new Button(_context);
 btnValide.setOnClickListener(this);
 linearButton.addView(btnValide);

 btnCancel = new Button(_context);
 btnCancel.setOnClickListener(this);
 linearButton.addView(btnCancel);
 }

So how can I start an Activity from this class on my onClick Method?

Upvotes: 0

Views: 94

Answers (3)

sarthakmeh
sarthakmeh

Reputation: 90

try this

AlertDialog.Builder builder = new AlertDialog.Builder(this);

   builder.setMessage("YOUR MESSAGE")
     .setPositiveButton("Yes", dialogClickListener)
     .setNegativeButton("No", dialogClickListener)
     .show();   

DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

  public void onClick(DialogInterface dialog, int which) {
        switch (which){
        case DialogInterface.BUTTON_POSITIVE:
            Log.d("yes","working");//Yes button clicked
            startActivity(new Intent(activity.this,MainActivity.class));
            break;

      case DialogInterface.BUTTON_NEGATIVE:
            dialog.dismiss();   //No button clicked
            break;
        }
    }
};

Upvotes: 1

Renaud Mathieu
Renaud Mathieu

Reputation: 376

An Intent provides a facility for performing late runtime binding between the code in different applications.

Its most significant use is in the launching of activities, where it can be thought of as the glue between activities.

Assuming your AlertDialog belongs to an Activity. Here is a code sample to go back to your Home activity for example.

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);

Upvotes: 1

Piovezan
Piovezan

Reputation: 3223

Try something like this:

new AlertDialog.Builder(YourActivity.this)
.setPositiveButton("Start Activity", new OnClickListener() {
    public void onClick(DialogInterface arg0, int arg1) {
        Intent intent = new Intent(YourActivity.this, NewActivity.class);
        YourActivity.this.startActivity(intent);
    }
})
.setNegativeButton(android.R.string.cancel, null)
.create().show();

Upvotes: 3

Related Questions