SlowDeepCoder
SlowDeepCoder

Reputation: 882

startActivity() or any other suggestions?

In my apps menu class I want a button to start an activity. This doesn't work with the StartActivity() method becaue it's not a subclass to Activity at all. So how should I do?

If you have read the "Beginning Android Games" you can see in the 6th chapter when he do a snake game how its shold look like when I want to start an activity.

//Daniel

Upvotes: 0

Views: 223

Answers (3)

Blundell
Blundell

Reputation: 76458

Either pass your context with the constructor like below or pass it into a static method:

 private Context context;

 public MenuClass(Context context){
     this.context = context;
 }

 private void someMethod(){
     // Do your stuff
     startNextActivity();
 }

 private void startNextActivity(){
      context.startActivity(context, OtherClass.class);
 }

Static method:

 public static void startNextActivity(Context context){
      context.startActivity(context, OtherClass.class);
 }

 // Use
 MenuClass.startNextActivity(someContext);

Upvotes: 2

J. Maes
J. Maes

Reputation: 6892

Are you using an intent?

Intent it = new Intent(firstActivity.this, secondActivity.class);
startActivity(it); 

Pass the context to that class and call startActivity with the intent.

 Intent it = new Intent(firstActivity.this, secondActivity.class);
 context.startActivity(it); 

Upvotes: 2

Diego Torres Milano
Diego Torres Milano

Reputation: 69198

Use Context.startActivity() if you are launching from a class that is not an Activity.

Upvotes: 0

Related Questions