activity reload while orientation change in android - 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"

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 restarts my game on screen rotation (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.

Save activity state(not just some variables) on orientation change

I realized that there are lots of question on this topic already asked on SO.
But I don't even know the basic when it comes to saving the state of an activity.
(Refer Screenshot Below) When app launches,
1) ScrollView item 1,2,3,4 are visible
2) table containd data which is populated due to Gainer button.
As showed in below screenshots, While app is running in PORTRAIT mode, I
1)scrolled down to ScrollView item 4,5,6
2)pressed the Loser button so accordingly data in the table below the button changes.
3)I'll even change content of graph dynamically(which I had not done yet).
Now I switch to LANDSCAPE Mode so
1)ScrollView is showing ScrollView item 1,2,3,4
2)table is showing data which is populated due to pressing Gainer button.
3)graph is as it is as I've not changed it yet(which I will change later).
So what happens is when I change the orientation, my activity is getting re-launched. So if user is performing some task in one orientation and he changes the orientation, then whole progress will be lost.
I know I need to save the state of the activity and Restore it when orientation changes.
But I don't know from where to start and what to save.
ANY HELP WILL BE LIFE-SAVER !
Option #1: Override onSaveInstanceState() of your Activity and put whatever information you want in the supplied Bundle. Your new activity instance will receive that Bundle in onRestoreInstanceState() (or onCreate()). Here is a sample project demonstrating this.
Option #2: Override onRetainNonConfigurationInstance() of your Activity and return some object that represents your state. Your new activity instance can call getLastNonConfigurationInstance() to retrieve that object, so the new activity can apply that information. Be careful, though, not to have the old activity return something in the object that holds a reference back to the old activity (e.g., a widget, an instance of a regular inner class). Here is a sample project demonstrating this.
Option #3: Convert this activity to a fragment. Have the fragment call setRetainInstance(true); on itself during its initial setup. Add the fragment dynamically to some activity via a FragmentTransaction. Now, when the configuration changes, the fragment is retained, so all your widgets and state are retained. Here is an overly-complex sample application demonstrating this.
Those are the three recommended approaches nowadays.

Android - stop move to main activity when rotating the device/emulator

There is an application to load UI dynamically for a unique LinearLayout when button press.
The main UI is extends by Activity and others are extends by LinearLayout.The unique LinearLayout is in main UI.
Dynamically loading part is working smoothly.But when changing orientation of device or emulator it goes to main ui.I have a layout Stack to get current Layout(UI) at any given time.
I want to stop move to main UI when rotating the device/emulator.
Is it possible?
Thanks in advance.
It is probably because the activity is getting recreated on orientation change.
In your manifest file against the main activity add the following properties
<activity android:name=".YourMainActivity" android:configChanges="keyboardHidden|orientation"></activity>
I believew this happens because you don't correctly handle the event of the orientation change. You should override public void onConfigurationChanged (Configuration newConfig)method to handle this.
If you don't override it and don't force your actiivty to keep the orientation, when the orientation changes, the system will destroy your app and re-launch it. That's why you see the main UI again.
Solutions:
explicitly declare the orientation you want in the AndroidManifest.xml file, so such event will be ignored. something like: android:screenOrientation="portrait". See here for more choice.
override the onConfigurationChanged method and chage your UI as you want inside that method, or simply do nothing so that this event would be ignored.

Categories

Resources