Getting data from remote server and screen orientation issue in tablet - android

What I expected in my application is data coming from the server and everything, even the view, are dynamically created. The activity layout is divided into two parts. On the left is a list and on the right side are dynamic fragment changes. Whenever the orientation changes the server call is made again (As the activity is destroyed and re-created). This is working fine.
There are 3 fragments(2 are Maps) that have to be called from inside of each other on the right side of the layout.
Now I dont want this thing to happen. I don't want everything to refresh again and start from scratch.
I can't set configChange in the manifest, as the image sizes in the layout for landscape and portrait are somewhat different.
What is needed is a layout change on orientation change without re-creating the activity.
I have tried using onSaveInstanceState() and onRetainNonConfigurationInstance() but couldn't get it to work.

I think your solution is to set the configChange on your manifest AND create different layouts for portrait and landscape so you can have the results you are expecting: No refresh of the activity, and control the sizes of your ImageViews.

Related

My app is not working properly when screen is rotated

I am creating an app on android studio. The app is working fine but whenever I rotate the screen, the layout is changing, the items on the screen are getting overlapped and sometimes the app restarts.
What are the possible solutions? Is preventing the screen from rotation a good option? Please help. Thanks in advance.
You need to set orientation in AndroidMenifest.xml.Use keyboardHidden attribute if you are getting any issue in keyboard.
You can set you activity's screen orientation to portrait, by this your activity will never rotate, but if you want to support landscape version then you have to create a landscape layout as well and put the layout in layout-landscape folder, but you have to take care of your data also.
Choice is yours
You can prevent your activity to recreate when orientation changes. Just add this in your activity tag in Manifest file.
<activity
android:name=".YOUR_ACTIVITY_NAME"
android:configChanges="keyboardHidden|orientation|screenSize" />
Android Activites start from the beginning when you rotate a device. So your Activity Lifecycle methods like
onCreate(), onStart(), onResume()
will be called again.
There are two possible solutions, the first one is by Rob here.
<activity
android:name=".ACTIVITY"
android:configChanges="keyboardHidden|orientation|screenSize" />
This will handle the orientation changes itself.
Another solution is to create different layout types in different layout folders. The folders will be
layout-land, layout-sw600dp-land(for 7inch tablets) and layout-sw720dp-land(for 10inch tablets)
So when you rotate a device, the layouts in this folders will be automatically inflated by the system. There will be cases like when you want to preserve the current state of the activity, for this refer to this link
No, preventing the orientation to be landscape is not a good solution. Many tablets are landscape-only, and your app won't display correctly on those.
The correct thing to do is to save your state in onSaveInstanceState() and then read it in onCreate() when the activity is re-created (the savedInstanceState variable will not be null). For this to work, the data that you are saving must be Serializable or Parcelable.
If you want to save non-serializable data or you have something running in the background, you can use a Fragment with setRetainInstance(true); and no layout. See this link.
The view will be created again, yes, but you will set the saved values so that it will look as it was before the rotation.

Android: After orientation change breaks connection between fragments

Application has two fragments: the first one contains a small representation of pager with photos, and the second one contains full screen pager. The second fragment replaces the first and passes a page number to the previous every time it changes. I made connection between my fragments just like Android Developers says.
Everything works till device orientation doesn't change. The first fragment is not recreated until it is not on top of stack, that is why all page number changes after that are missed for first fragment.
I do not really what to disable views destroy on orientation change, but looks like it is the only way.
What is the best solution?
In your manifest file in your parent activity in which fragments are write following line :
android:configChanges="keyboardHidden|screenSize|orientation"
Let me know if that works for you. Best of luck :)
When you change the orientation, the activity gets rebuild, thus essentially destroying the fragment that was built before the change in orientation.
Perhaps you would wish to refer to this https://developer.android.com/guide/topics/resources/runtime-changes.html on retaining the state during a change in configuration.

Black Screen is showing for a while when screen orientation is changing

I am using one listview and one dynamic listview in the same xml but the problem I am facing is while changing the orientation in nexus 7 the screen is going black screen for a while and adjusting all the view in this time and In manifest file I specified screensize,orientation,keyboard-hidden for config attribute at activity attribute.I am handling required orientation changes in onConfigurationChanged() but still the black screen is showing for a while.Can you suggest me where I need to make the changes to work in properly
As the code length of code extends no of lines I am sharing my code with below link
Thanks in adavanc
https://docs.google.com/document/d/1dCIxgqX0vZIEbWfxWpLNkEDpu_-vd7HDfcGlYSInbHc/
You can't do a lot things in onCreate() and onResume(). any file operation and network operation should go to asynctask. the only thing you should do is manipulate UI. see http://developer.android.com/training/articles/perf-anr.html

Offering different _functionality_ (not merely layout) based on orientation?

I have a monitoring app that displays a set of four parameters as animations (for example, it displays the temperature using a thermometer graphic). I also have one screen which plots all the four parameter values on a chart.
My requirement is this:
Whenever the device is in Landscape mode, the chart should be displayed.
Whenever the device is back in portrait mode, the earlier animation screen should be displayed.
I'm wondering if it is possible to provide different functionality based on the orientation. I see 2 options
Override Activity.onConfigurationChanged() - but then what? Can i do a setContentView() at this point? Or, can i launch a different Activity (one which displays the Chart in a separate, lanscape-mode-only Activity)?
Override Application.onConfigurationChanged() method. But I frankly haven't a clue as to how to proceed with this.
Yes you can call setContentView() inside Activity.onConfigurationChanged(). You can check orientation and accordingly perform some tasks, i.e. run different code based on orientation.
Normal behaviour is that when orientation changes, system kills Activity and creates a new one. You can prevent this with android:configChanges="orientation" in you activity manifest. In this case you need to override the onConfigurationChanged(..) method, which will be called when change happens. See handling runtime changes.

appWidget Orientation Code

I have code in an appwidget that I want to run when the phone's orientation changes on the home screen, ie like when the keyboard flips out. I have an image that I want to change in an imageview in my appwidget. I can't use different layouts linked to the orientation (ie "layout" and "layout-land") because I don't know the name of the image file until runtime, it is created at runtime. Is there anyway to trigger code to run only if the home screen is shown, my appwidget is active and the orientation just changed?
I could listen for a configuration_change broadcast but that will run everytime the phone switches to landscape or portrait and I only want it to happen when the homescreen is shown. I cannot think of any good way in android to do this. Thanks
Ryan
There isn't a good way of doing this for images generated at runtime.
One approach would be to build both versions of the image and have two ImageViews that are visible depending on the layout/layout-land being used. (So the RemoteViews would update both ImageViews, but only the correct one would be visible.)
Jeff, what is the reason for this limitation ? I would love to be able to use images generated at runtime, or construct the RemoteView on the runtime as well, rather than use the precompiled XML - this is so inflexible.
There is more info at http://groups.google.com/group/android-developers/browse_thread/thread/0eb7c016be05e0b9/d31b312e2fa8530d
Basically update the widget from onConfigurationChanged() in an Activity and add android:configChanges="orientation" for that Activity in your manifest. Bad thing is, this doesn't cover all use cases.

Categories

Resources