JoanG
JoanG

Reputation: 35

Android: startActivity() from AlertDialog

I am trying to use a Button in an AlertDialog to view a webpage.

The problem is that this AlertDialog is located inside a class that extends ItemizedOverlay and doesn't extend Activity, so Eclipse underlines it and gives the following error:

The method startActivity() is undefined for the type new DialogInterface.OnClickListener(){}

I would like to launch activities from the PositiveButton, NeutralButton and NegativeButton, but I am not able to.

In case you need more context, the main Activity is a MapView with some ItemizedOverlay, and I would like to launch a webpage with directions or another activity that does that.

Upvotes: 2

Views: 12228

Answers (5)

Venky
Venky

Reputation: 11107

Take a look at this

In these you write your desired code inside the Positive Button,You can start an Activity by using Intent..

startActivity(new Intent (YouPage.this,NewPage.class));

Upvotes: 1

Harshad
Harshad

Reputation: 8014

If you have the context say context.startActivity();

if you dont have context try to get it from getContext(); or getApplicationContext(); or getBaseContext();

Upvotes: 0

Sunil Pandey
Sunil Pandey

Reputation: 7102

Pass context of the activity in constructor of that class and create a field of type activity and store context in that activity and then

say

Activity activityClass;

and initialize it from your constructor

and start activity from that activiytClass

activityClass.startActivity();

Upvotes: 0

Anju
Anju

Reputation: 9479

Construct a constructor in the class which contains your alert with Context object as a parameter. Assign it to a Context variable.

Use this context variable for creating the intent.

Intent intent = new Intent(mContext, "Your next activity to be shown");//mContext is the Context variable over here.
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);    
mContext.startActivity(intent);

Upvotes: 6

Farhan
Farhan

Reputation: 13390

Actually the answer is provided by you in your question. i.e startActivity is undefined in ClickListener functions. so in oncreate function, initialize a Global variable like

Activity myActivity = this;

then in onclicklistener, start another activity from this.

myActivity.startActivity(xxxxxxxx);

This could be one answer.

Upvotes: 2

Related Questions