ezequielov
ezequielov

Reputation: 1

Problem with intent

I have 2 different class... de first one (MainSoup) is the main class and this class extends activity. The second class (View2) extends View. in View2 class is where i make my OnTouchEvent and my Canvas... I also have a frameLayout with 2 layout... in the first one i put multiple textViews. On top of this first layout i put the second one wich has nothing and here is where i draw with my Canvas and touch events. At this point everythings works just fine.

The problems begin when i want to make an intent... I put the intent in de Main class (MainSoup): Intent i = new Intent(this, org.me.androidsoup.MainSoup.class); startActivity(i); but i dont know how to trigger it (since the OnTouchEvent is in the View2 class).

And if i try to put it in the View2 class, i have troubles with the startActivity line, It doesnt recognize it and tells me to create a method call startActivity.

Upvotes: 0

Views: 151

Answers (2)

neteinstein
neteinstein

Reputation: 17613

Thinks briefly about it gave me 3 options:

  1. You take care of the onTouchEvent on the Activity, (you do findViewById and set the onTouchEvent to it, then you have context)

  2. You add a static Context variable to the Application Class, when each activity starts (onCreate) it sets the Contexts as this like:

    Application.context = this;

  3. Use the getContext() method on the View to get the Context, and do new Intent(getContext, ActivityYouWant.class);

I think the first and third are the most valid options. But you choose.

Upvotes: 0

DanielS
DanielS

Reputation: 555

startActivity() is a method that requires a context (it's actually a method defined by the Context class). Views have a method called getContext() that will return the context attached to that view. You could use that for invoking the Intent.

Hope it helps.

Upvotes: 3

Related Questions