Activity restarts my game on screen rotation (Android) - android

I know this question was already asked, but mine is a little different:
I have 2 different layout files for my game; one for portrait mode and one for landscape. When I rotate the screen, the onCreate method restarts my game (creates all the elements again). I donĀ“t want this to happen, so I wrote this line in the manifest:
android:configChanges="orientation"
It works, onCreate is not called, but the new layout is not being showed properly!
I tried to put the following code in my Activity, but it just keeps doing weird things:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.gameview);
}
how can I fix this?
thanx guys

First of all understand how orientation changing in android works:
By default activity restarts on orientation changed event (and goes throw onCreate).
If you write android:configChanges="orientation" in manifest - it means it will not be recreated, but just remeasure and redraw view tree.
Comments about your code:
If you have different layout for different orientations - you have to recreate activity on orientation changed.
Method setContentView should called just once per activity lifecycle.
General way to handle this situation is:
Save game state in method onSaveInstanceState
In onCreate method restore game state if it is supplied (savedInstanceState param is not null).
Remove listening of configuration changing from manifest.

You should use onRetainNonConfigurationInstance() for save state and for restore state getLastNonConfigurationInstance() for any objects. Or hard-code set android:screenOrientation="portrait/landscape" in manifest.

Related

Activity was restored twice when the orientation changes

I am having some problems with an activity of my Android aplication.
I have discovered that my app enters twice in 'onCreateView()' method of this activity when orientation changes, and it is causing me problems. When I click the corresponding button and this activity starts is all ok, but whithin this activity when the orientation changes I have some problems because enters twice, i.e. activity restarts twice ...
... Does anyone know the behaviour of an activity when orientation changes? Why does this happen?
Thanks so much.
Since onCreateView() is not part of the Activity lifecycle, but rather is part of the Fragment, I would assume you have a fragment somewhere in your activity.
Also I would assume you add this fragment manually using FragmentManager and FragmentTransaction. Since Android keeps all the added fragments between orientation changes, it is quite possible that you don't check if your Fragment is already present in this activity, so you add it again which causes additional onCreateView to be called.
If this is the case - you need to check if your savedInstance is null within onCreate. If it is - you add fragment. Otherwise activity is getting restored, so fragment is already there.
A lot of assumptions for the answer, I know. But I'm still mastering my telepathy skills :)
When orientation changes onDestroy() is called, followed by onCreate().
For more : http://developer.android.com/guide/topics/resources/runtime-changes.html
Try to add this in AndroidMainfest.xml in your activity:
android:configChanges="orientation|screenSize|keyboardHidden"
This means not to recreate the activity when orientation changes.

Facebook login window close when screen rotates

I have implemented facebook into my application.
Login process is working properly. When I click on facebook image then it opens facebook login window.
But When I rotate the emulator then it Close the login window.
Any solution.
Add this in manifest file for your Activity
android:configChanges="keyboardHidden|screenSize|orientation"
The approach I took was to not allow the OS to restart your activity after the layout configuration change. To accomplish this, add this line within activities that you want to prevent from restarting in your manifest file:
<activity
android:configChanges="orientation|keyboard"
...
>
Optionally, you can handle the configuration change in code in case there are some layout changes you want to make manually, such as reloading a new view from XML. This is done by overwriting the onConfigurationChanged() method in your Activity class:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
//Handle config changes here, paying attention to
//the newConfig.orientation value
super.onConfigurationChanged(newConfig);
}
Problem may be because of screen orientation configuration change, you can try one of following solutions:
As some of the answers suggested, you could distinguish the cases of
your activity being created for the first time and being restored
from savedInstanceState. This is done by overriding
onSaveInstanceState and checking the parameter of onCreate.
You could lock the activity in one orientation by adding
android:screenOrientation="portrait" (or "landscape") to
in your manifest.
You could tell the system that you meant to handle screen changes
for yourself by specifying android:configChanges="screenOrientation"
in the tag. This way the activity will not be recreated,
but will receive a callback instead (which you can ignore as it's
not useful for you).

activity reload while orientation change in android

I have developed an application and it contains web view.
My issue
When my phone's orientation changes from portrait to landscape the whole application loads again and the web view reloads showing the first page of website.
So I am getting confused about the screen orientation or saving the data during that phase, so how do I fix it...
Yes, in Android the Activity is destroyed and recreated when your change the screen orientation. Thus you need to restore your applications state on recreation. On way is to use the callback method onSaveInstanceState() which you can use to save the state in a Bundle.
The Activities are explained here: http://developer.android.com/guide/components/activities.html and I suggest you take a look on that page for examples and more detailed instructions.
The default behaviour is to restart the activity when a configuration change happens (such as orientation change).
To override this you need to tell the system you'll handle orientation change yourself by adding this to your manifest file in your <activity> element:
android:configChanges="keyboardHidden|orientation|screenSize"
You may also want to override onConfigurationChanged which will be called when such a change happens.
See http://developer.android.com/guide/topics/manifest/activity-element.html#config
The Android Activity lifecycle clearly indicates this behaviour .
What happens
Whenever you start an Activty is gets created(after onStart() method) the
onCreate(Bundle onSavedInstance)
The variable onSavedInstance mentioned above initially recieves null as nothing is saved.
But during the orientation the whole layout hierarchy has to adjust according to the new mode from an existing mode(from portrait->landscape or vice-versa).This change may remove the previous data that you had in your activity.So to avoid such a problem(loosing data), there is a method
onConfigurationChange(Bundle saveSomething)
This method will be called for you to handle some configuration changes into this,
The default implementation will save you some data like some text in an editText.
Note This method as far the specs goes should be used to save some trivial data.
Example
Suppose you had applied a background colour to the activty layout and now you rotated it default implementation won't save it but onConfigurationChange if you want you can save it like this
saveSomething.putInt("color",1);
Inside onCreate
protected void onCreate(Bundle onSavedInstance){
if(onSavedInstance!=null){
int color=onSavedInstance.getInt("color");
if(color==1){
setBackgroundColor(Color.BLACK);
}
}
}
Add the following line inside the activity element of your manifest file you will be handling the changes in configuration
android:configChanges="keyboardHidden|orientation|screenSize"

How to know when the app is open (and not when onCreate is called)?

I want to track app opens in android but the problem is that onCreate for the main activity could be called multiple times when the orientation change. is there another way to track app open ?
Other methods than onCreate() could be used :
OnStart()
OnRestart()
OnResume()
You'll want to consult this page of the documentation in order to decide which one fits your needs better as they all get called in different situations.
Alternatively, you could always handle the orientation change yourself, that way the onCreate() method wouldn't be called when the screen rotates:
Modifications to the Manifest file:
<activity
android:name=".ActivityName"
android:configChanges="keyboardHidden|orientation" />
Add this method in the Activity :
/** {#inheritDoc} */
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
Hope this answers your question.
Do you want to consider handling orientation changes by yourself? That way, onCreate is not called when orientation changes (which is not a very good idea if you have different layouts for potrait and landscape). You can then rely on onStart method which is called when the screen is turned off/on, or when the activity resumes.
The activity is open when onCreate is called, and closes when onDestroy is called. Each onCreate will always be paired with an onDestroy; when the orientation changes, the activity is (by default) first destroyed, then recreated in the new orientation.

Activity still recreated with configChanges

As I understand it, if you insert 'android:configChanges="orientation"' into the activity in the manifest, the activity will not be destroyed and recreated on an orientation change. To test this, I created a dead-simple app which does NOTHING. Then I inserted 'android:configChanges="orientation"' in the manifest. Then I added the following method:
#Override
public void onConfigurationChanged(Configuration newConfig) {
Log.v(TAG,"onConfigurationChanged:");
super.onConfigurationChanged(newConfig);
}
However, I'm still seeing onCreate() being called. The activity is still being recreated.
As if that weren't strange enough, I don't see onConfigurationChanged() being called when I put the emulator into landscape mode (Ctrl-F11). It's only called when I go back to portrait mode. Shouldn't it be called both ways? Isn't the configuration (orientation) being changed when I go into landscape as well as portrait modes? This makes no sense.
Anyway, this whole activity and orientation thing is driving me crazy.
Your screen size changes from 1200x800 to 800x1200 (for instance).
Since API 13, this will also raise a screenSize config change. The fix is:
android:configChanges="keyboardHidden|oritentation|screenSize"
However, I'm still seeing onCreate() being called. The activity is still being recreated.
The emulator emulates a device with a side-slider keyboard. The android:configChanges value that matches your - would be keyboardHidden, generally used in conjunction with orientation to handle non-keyboard devices (e.g., android:configChanges="keyboardHidden|orientation").
That being said, android:configChanges is not recommended in most cases. Use dynamic fragments and setRetainInstance(true), or use onSaveInstanceState() and onRetainNonConfigurationInstance() to allow the activity to be destroyed and recreated.

Categories

Resources