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

Android Studio how to make relative layouts circle around a relative layout?
Firebase query by child's child key
How to go back to Previous Activity?
how to count number of click on google map marker in android
Custom View layout not changed after size has changed
Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
How to display Name of devices connected to android hotspot programmatically
The right place to host shared tables between wordpress website and mobile apps
Clear Toolbar menu options added by fragment when it is replaced
Android - ImageSpan - How to center align the image at the end of the text
SIGSEGV Android error while using FutureTask
loopj android delete request return 404 page not found
How can we develop e commerce with different UI in android [duplicate]
ConstraintLayout Notification Badge
Unable to create call adapter for io.reactivex.Observable
How to use getMapAsync() instead of getMap()

Categories

HOME
embedded
apache-jena
pyqt
mysql-workbench
r-googlesheets
spring-data-mongodb
tivoli
nsbundle
nuget-package
missing-data
mvc5
lanczos
google-schemas
3nf
azure-iot-hub
aws-sdk
identity-management
sar
highstock
draft-js-plugins
maze
custom-post-type
syntax-highlighting
jasmine-node
amazon-quicksight
firebase-analytics
android-things
exchangewebservices
cppreference
livelink
complex-numbers
left-join
glib
card.io
jwplayer6
reason
alphabetical
loading
claims
berkeley-db-je
dapper-simplecrud
phpdbg
pom.xml
automapper-5
clarifai
sonarlint-eclipse
robomongo
fclose
geotiff
remedy
subnet
sundials
type-theory
d3-force-directed
renaming
grails-plugin
latin
iotivity
macaulay2
rspec2
wso2developerstudio
i386
udpclient
tracker-enabled-dbcontext
turbo-prolog
upx
lz77
concept-insights
vhosts
dllimport
forwarding
slash
haskell-warp
linked-tables
telepat
sonarqube-5.0
structuremap3
rickshaw
raygun
draw2d-js
file-not-found
getopt
openargs
cnf
kaleidoscope
http-status-code-410
preload
sqlclr
booksleeve
dopostback
zope.interface
azman
cdonts
diagrams
commerceserver2007
drupal-fivestar
ajax-polling
skype4java
feasibility
mathematical-notation
wordprocessingml
dojo-dnd
product-management

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