Issue
I have an app that so far consists of two Activities:
The Main Menu Activity.
The Game Activity
The Main Menu Activity contains a button that starts the Game Activity with the following code:
public void onClick(View clickedButton)
{
switch(clickedButton.getId())
{
case R.id.buttonPlay:
Intent i = new Intent("apple.banana.BouncingBallActivity");
startActivity(i);
break;
}
When the user is done with the Game Activity, he presses the back button. This calls the onPause() method first, which pauses the animation thread of the game. It then calls the onStop() which calls finish() on the activity altogether. The user is returned to the Main Menu activity. The code is outlined below:
public class BouncingBallActivity extends Activity{
private BouncingBallView bouncingBallView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bouncingBallView = new BouncingBallView(this);
bouncingBallView.resume();
setContentView(bouncingBallView);
}
@Override
protected void onPause()
{
super.onPause();
bouncingBallView.pause();
}
@Override
protected void onResume()
{
super.onResume();
bouncingBallView.resume();
}
@Override
protected void onStop()
{
super.onStop();
this.finish();
}
}
The problem is that this only works if I launch the application from Eclipse. When I click on the app icon, the game starts from the Game Activity. The main menu activity does not appear.
I am not clear about why this happens. It could be something to do with the manifest. I've pasted the relevant portions below:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".BouncingBallActivity"
android:label="@string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="apple.banana.BouncingBallActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MainMenu"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I'd really appreciate any help with this. Thanks.
Solution
The call to finish()
does not belong in your onStop()
method. If you want to end the game when the user presses back, place it in your onPause()
. The reason the app picks up from the game activity on subsequent launches (through the Android launcher interface) is it never left there.
If you only want to end the game when the user presses the Back key, and not from other pauses, then you'll need to catch incoming keys and finish()
if the key is Back.
Answered By - mah
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.