igorcar1994
igorcar1994

Reputation: 67

Q: Android - How to start one activity from another without button click

If I have a rectangle in main activity which goes from left to right(using simple thread) until it touches the end of the screen, how can I make main activity switch to another activity when collision happens? Edit:Added code for activity.I have method in "zmija" which checks for collision.

Main Activity

ZmijicaSV zmija;
private static int score=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    zmija=new ZmijicaSV(this);
    zmija.setOnTouchListener(this);
    setContentView(zmija);
}

@Override
protected void onPause() {
    super.onPause();
    zmija.pause();
    }


@Override
protected void onResume() {
    super.onResume();
    zmija.resume();

}



@Override
public boolean onTouch(View v, MotionEvent event) {

    boolean up=zmija.sp.isUp();
    boolean right=zmija.sp.isRight();
    boolean down=zmija.sp.isDown();
    boolean left=zmija.sp.isLeft();


    switch(event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            if(up) {
                zmija.sp.setUp(false);
                zmija.sp.setRight(true);
                increaseScore();
                break;
            }

            if(right) {
                zmija.sp.setRight(false);
                zmija.sp.setDown(true);
                increaseScore();
                break;
            }

            if(down) {
                zmija.sp.setDown(false);
                zmija.sp.setLeft(true);
                increaseScore();
                break;
            }
            if(left) {
                zmija.sp.setLeft(false);
                zmija.sp.setUp(true);
                increaseScore();
            break;
        }


    }
        return true;
    }

public void increaseScore()
{
    score++;

}
public int getScore()
{
    return score;
}
public void setScore(int s)
{
    score=s;
}

public boolean Collision()
{
    if(zmija.checkCollision())
        return true;
    else
        return false;
}

Upvotes: 1

Views: 797

Answers (1)

user3956566
user3956566

Reputation:

import android.content.Intent;

If you're calling it from a surface view

if(collision) 
startActivity(new Intent(context, GameOver.class)); 

Where context is the context is the Context you have used to create your surface view.

If you are calling an activity from within an activity, you can call it like so:

startActivity(new Intent(this, SecondActivity.class));

Upvotes: 2

Related Questions