DavidPrabhu
DavidPrabhu

Reputation: 45

Android: How to go from MainActivity to Surfaceview

I have an Activity GamePanel which extends Surfaceview with the context code below.

public GamePanel(Context context)
{
super(context);
this.mContext = context;
mContext = getContext();

//add the callback to the surfaceholder to intercept events
getHolder().addCallback(this);

thread = new MainThread(getHolder(), this);

//make gamePanel focusable so it can handle events
setFocusable(true);
}

Now I used this to go to my MainMenu class from GamePanel.

Intent intent = new Intent(mContext, MainMenu.class);
                mContext.startActivity(intent);

Here is my question: How do I go back from the MainMenu Activity to the Gamepanel Activity that extends Surfaceview when I press a button?

EDIT: Here is the top line of my GamePanel Activity:

public class GamePanel extends SurfaceView implements SurfaceHolder.Callback

Upvotes: 2

Views: 1128

Answers (1)

Vladyslav Matviienko
Vladyslav Matviienko

Reputation: 10871

As I said, it is not an Activity. Please stop calling it an Activity. It is a SurfaceView, which is a subclass of View, and is not a subclass of Activity.
To start activity, you just need a Context instance.
Every View can provide an instance of Context by calling getContext()
So to start another Activity from a View, you can call

Intent intent = new Intent(getContext(), MainMenu.class);
                mContext.startActivity(intent);
getContext().startActivity(intent );

Upvotes: 1

Related Questions