Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: startActivity() in a class that extends View

I have 3 classes: MainActivity,DrawView and SecondActivity. MainActivity just use DrawView to draw something on the screen. How can I start SecondActivity when i touch the screen on an Bitmap, I know where is the bitmap(in the code). The onTouchEvent function from DrawView class.

@Override
public boolean onTouchEvent(final MotionEvent ev) {
     switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN: {

            float posX = ev.getX();
            float posY = ev.getY();
            if(play.touched(posX, posY)==true){

            } else {
            }
            return true;
        }

}
     return false;
}

The function play.touched(posX,posY) return a boolean value(I made a class that handel the position of the Bitmap). How can I start Second Activity from here?

like image 259
Felix Avatar asked Jul 12 '26 07:07

Felix


1 Answers

        if(play.touched(posX, posY)){
            Intent intent = new Intent(getContext(), SecondActivity.class);
            ((Activity)getContext()).startActivity(intent);
        } else {
        }
like image 116
Cristian Avatar answered Jul 13 '26 20:07

Cristian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!