Reputation: 32
Here is the structure of my application.
MainActivity.java calls FragmentActivity.java,
FragmentActivity.java calls GameView.java
GameView.java calls Thread.java.
Basically all the gaming logic will be handled by GameView and its thread. I don't know how to prevent from restarting the game when there is a orientation change.
If i paused the thread and resume it, the app crashes and also i can not use onSaveInstanceState method in Gameview.java
Any help?
Upvotes: 0
Views: 113
Reputation: 822
It is completely possible through the Android manifest. Just add in the activity declaration, in which you want to disable the restarting the following attribute:
android:configChanges="orientation|screenSize"
Then you can overwrite the onConfigurationChanged()
in your activity and you get the callback which event just happened. In your case the orientation change. And with this approach the activity doesn't restart, when your orientation changes.
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
Upvotes: 1
Reputation: 25194
You can have some control over what happens on orientation change, by defining an Application
class in your manifest file, and overriding public method onConfigurationChanged()
.
Base class for those who need to maintain global application state. You can provide your own implementation by specifying its name in your AndroidManifest.xml's tag, which will cause that class to be instantiated for you when the process for your application/package is created.
You just need to check for newConfig == Configuration.ORIENTATION_LANDSCAPE
and such. At this point you might want to reload resources to get things working.
Upvotes: 0
Reputation: 604
You are looking for onResume() and on pause() ... You store values into a bundle in pause and instantiate those values on the resume method and show a dialog as this happens to prevent the user from noticing the obvious.
Upvotes: 0