开发者

How to change activity in android?

开发者 https://www.devze.com 2023-02-24 07:18 出处:网络
I am making simple game. I have 3 activities. First activity is main menu (FirstActivity class). First activity has following code:

I am making simple game. I have 3 activities. First activity is main menu (FirstActivity class).

First activity has following code:

Intent i = new Intent(this, SecondActivity.class);  
startActivityForResult(i, ACTIVITY_CREATE);

It calls second activity where, second activity has following code:

setContentView(new MovementView(this));

MovementView class extends SurfaceView. MovementView class is where I write all game code.

So I want to make when lives == 0, it changes to ThirdActivity. How do I do that?

I have tried putting code similar to FirstActivity's class but it doesn't seems to work.

If I put

((Activity)getContext()).finish();

it just jum开发者_开发知识库ps back to FirstActivity, but that's not what I need, I need it to go to ThirdActivity class.


Finally I can answer, had to wait 24h.
So in the end the only variant which I managed to make work was like this:
FirstActivity class:
on click

private void startGame() {
        Intent i = new Intent(this, SecondActivity.class);
        startActivity(i);
    }

SecondActivity class:

protected void onCreate(Bundle savedInstanceState) {
setContentView(new MovementView(this));
}        
    @Override
    protected void onDestroy() {
        Intent i = new Intent(this, GameOver/*GameOver=ThirdActivity*/.class);
        startActivity(i);
        super.onDestroy(); 
    }

MovementView class:

if (lives == 0){            
updateThread.setRunning(false);
((Activity)getContext()).finish();
}

I don't know is that the difference between onDestroy and onStop but both work. The only unpleasant thing is that when lives == 0 it goes back to FirstActivity (for like half second) and only then to GameOver class.


It didn't work when you tried it because you're trying to create an intent from a View, not an Activity. You'll need to use the view's context (getContext()) instead of this. You'll also need to use startActivity rather than startActivityForResult, unless you're actually planning to use the result (although casting the context to an Activity should be fairly safe if you're only using it from your own code).


Since you start Activity2 with ..ForResult(), you could also set the 2nd Activity's result (with setResult(GAME_OVER) before you finish it) so that Activity1 starts Activity3 in onActivityResult() if it makes sense that Activity2 is not there anymore when Activity3 was shown...

RE: @Yoni Samlan, well if I put

Intent i = new Intent(this, ThirdActivity.class);
startActivityForResult(i, ACTIVITY_CREATE);

It shows The constructor Intent(MovementView, Class) is undefined for first line, and The method startActivityForResult(Intent, int) is undefined for the type MovementView for the second line.

You should pass your Activity as first argument and not the view, and call Activity.startActivityForResult()... So pass your Activity to your view and use it there as described.


with the MovementView constructor you have to remember the activity, tu use it to launch the third activity.

private Activity a;

public MovementView(Acitivity a) {
  this.a = a;
  // your code
}

somewhere in MovementView, replace

Intent i = new Intent(this, ThirdActivity.class);  

by

Intent i = new Intent(a, ThirdActivity.class);  
0

精彩评论

暂无评论...
验证码 换一张
取 消