android


How to retain instance when orientation change while communicating with server


in a fragment my app commuicating with webserver now iwant that when orientation changed the fragment retain its instance but its not happening i want every thing remain persistance during orientation changed how is it possible my code is
#Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
protected void onPreExecute()
{
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(context,R.style.CustomDialogTheme);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setMessage("Processing...");
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected String doInBackground(Object... params)
{
// TODO Auto-generated method stub
if(type.equals("register"))
{
/*String url="http://waqasbrosltd.co.uk/index.php/register";
Network network=new Network();
Gson gson = new Gson();
String registerData=gson.toJson(user);
try
{
answer=network.registerUser(url,registerData);
}
catch (ClientProtocolException e)
{
}
catch (IOException e)
{
}
finally
{
closeProgressDialog();
closeDialogbox();
}
}*/
return null;
}
public void closeProgressDialog()
{
if (pDialog.isShowing())
{
pDialog.dismiss();
}
}
iwant my progress dialog also retain its instance but it notr retain
When the orientation changes android will destroy all activities and fragments and recreates them again. This is helpful because it also handles resource changes for you. For example if you have different layout file for portrait and landscape mode it automatically switch them for you.
But you can easily stop this process by handling orientation change yourself. If you do so android will not destroy anything on orientation changes and everything will be persistence. If any resource changes is required you should do it yourself!
Watch this youtube video to see how to do it
https://www.youtube.com/watch?v=Y5J6BURMrHQ&list=PLonJJ3BVjZW6hYgvtkaWvwAVvOFB7fkLa
In your project manifest.xml inside activity tag put this line
android:configChanges="keyboardHidden|orientation|screenSize"
for example:
<activity
android:name="com.example.MainActivity"
android:label="#string/app_name" android:windowSoftInputMode="stateHidden" android:configChanges="keyboardHidden|orientation|screenSize">
</activity>
and override this method in your activity
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
}
Hope this will help you.
You need a Service. A Service is your way of telling Android that you wanna do something that is not UI-dependant.
The problem is not that your Activities are being destroyed, but rather that you rely on your Activities to handle your communication tasks. This approach is problematic in many other aspects other than orientation change, like when the OS decides to destroy your background Activity stacks when in need of resources.
Transactions need to be handled by a Service. It is a Context object that is not being destroyed by orientation changes, and also refrains Android from shutting down your process when in need of resources.
There is a great Google IO lecture about implementing REST architecture using an Activity, a database and a Server object.
Even if you don't implement the whole solution, you can learn a lot from how the lecture addresses this issue.

Related Links

formatting inputstream data from database in Android
sending my JSON object from the selected index on a listview to my next activity (android)
Converting Milliseconds String to formatted date String in SimpleCursorAdapter
aplication crash and take me this errors
How to access Network provider latitude and longitude value with 4.0 or 4.0+.?
App Crash on Orientation Change (just when I set 2 different backgrounds for portrait and landscape modes)
Which database type is applicable to Android application for contents available in server?
What went wrong with my first attempt at running an Android app?
Android Status Bar Icon Only
Mount ext2 sd card with BusyBox
ListView with multicolumn and color
Listview with image different for each items
Call web service in android 4.1
How to call same alert Dialog again and again?
Populate Spinner from string array source, with a String ArrayAdapter
How can i set the default location to my place when showing the mapview

Categories

HOME
c99
serialization
apiblueprint
package
gradient
google-cloud-functions
android-json
g++
drop-down-menu
startup
resultset
redis-sentinel
stellar.js
eip
standards
fileserver
cfml
sencha-touch
asp.net-core-webapi
simpleitk
rubymotion
backpack-for-laravel
ios10.2
pchart
event-store
scriptella
flask-sqlalchemy
data.stackexchange.com
opentk
gpio
tpm
tizen-tv
constexpr
catch-unit-test
login-script
android-maps-extensions
pop3
python-import
navigator
swagger-php
jpad
sessionstorage
linq-to-objects
metawidget
doc
apollostack
dapper-simplecrud
libgphoto2
proget
term
convex-optimization
reshape
lsyncd
juice-ui
stax
redistributable
mta
rspec2
spreadjs
cda
fink
realsense
inf
vhosts
grgit
r-package
fiware-monitoring
ess
rubaxa-sortable
typeconverter
adehabitathr
mdichild
ss7
windows-embedded-standard
kendo-dataviz
modular
video-codecs
wp7test
sqlclr
resource-management
intersect
panda3d
frameworkelementfactory
midlet
database-create
infobright
django-paypal
squishit
binomial-heap
hbm2java
cldc
sifr3
soappy
image-capture

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App