Home
› Uncategorized
As mentioned in the post "Activity will be re-started when screen orientation changed", the old activity will be destroyed when orientation changed. And also mentioned in last exercise "EditText keep no change after orientation changed", the states of some views will be kept. But it's not always true; for example, refer to the exercise in "GridView", the GridView will always reload photos from SD Card when orientation changed.
To maintain the old states, we can overriding the method onRetainNonConfigurationInstance() to return the old activity. Via the returned activity object, we can retrieve the fields of the old activity.
Please notice that the method onRetainNonConfigurationInstance() is deprecated, If you are targeting HONEYCOMB or later, consider instead using a Fragment with Fragment.setRetainInstance(boolean).
Modify the java code in the exercise "GridView loading photos from SD Card", override the method onRetainNonConfigurationInstance() to return the activity object, this. In orCreate() method, check if old activity exist by calling getLastNonConfigurationInstance(). If the returned object not null, means old activity exist.
Download the files.
To maintain the old states, we can overriding the method onRetainNonConfigurationInstance() to return the old activity. Via the returned activity object, we can retrieve the fields of the old activity.
Please notice that the method onRetainNonConfigurationInstance() is deprecated, If you are targeting HONEYCOMB or later, consider instead using a Fragment with Fragment.setRetainInstance(boolean).
Modify the java code in the exercise "GridView loading photos from SD Card", override the method onRetainNonConfigurationInstance() to return the activity object, this. In orCreate() method, check if old activity exist by calling getLastNonConfigurationInstance(). If the returned object not null, means old activity exist.
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GridView gridview = (GridView) findViewById(R.id.gridview); //Check if old activity exist MainActivity oldActivity = (MainActivity)getLastNonConfigurationInstance(); if(oldActivity == null){ myImageAdapter = new ImageAdapter(this); gridview.setAdapter(myImageAdapter); String ExternalStorageDirectoryPath = Environment .getExternalStorageDirectory() .getAbsolutePath(); String targetPath = ExternalStorageDirectoryPath + "/test/"; Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG).show(); File targetDirector = new File(targetPath); File[] files = targetDirector.listFiles(); for (File file : files){ myImageAdapter.add(file.getAbsolutePath()); } // Get memory class of this device, exceeding this amount will throw an // OutOfMemory exception. final int memClass = ((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE)) .getMemoryClass(); // Use 1/8th of the available memory for this memory cache. final int cacheSize = 1024 * 1024 * memClass / 8; mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { // The cache size will be measured in bytes rather than number of items. return bitmap.getByteCount(); } }; }else{ Toast.makeText(getApplicationContext(), "oldActivity exist", Toast.LENGTH_LONG).show(); myImageAdapter = oldActivity.myImageAdapter; gridview.setAdapter(myImageAdapter); } } @Override public Object onRetainNonConfigurationInstance() { // TODO Auto-generated method stub return this; }

-- Delivered by Feed43 service
Related Posts
There is no other posts in this category.Popular
-
The Avengers – Iron Man Mark VII 1.1 APKThe Avengers – Iron Man Mark VII 1.1 APK Suit up and Blast off in the First-Ever Fully-Interactive …
-
[Android] The Dark Knight Rises v1.1.1 APK by GameloftThe Dark Knight Rises is one of the hottest games just released by Gameloft in July 2012 on Androi…
-
Angry Birds Space v1.0.1 for AndroidAngry Birds Space v1.0.1 for Android The #1 mobile game of all time blasts off into space! All new …
-
How To: Copy And Paste In Android Mobile'>The copy, cut, paste and select the features Android has many nuances. In this how-to, we will foc…
-
[Android] Battleloot Adventure HD v1.0.7 (apk + data)In Battleloot Adventure HD , let's roll out and your adventure is about to begin. In the kingdo…
-
[Android] Zuma's Revenge HD v1.0.8 Free Download [Mediafire]Zuma’s Revenge Android , similar to Bonsai Blast, is one of the most fascinating puzzle game fr…
-
DROID RAZR - Motorola SmartphonesDROID RAZR DROID RAZR brings the best the Motorola has to offer in smartphones. They’ve really thr…
Post a Comment
Post a Comment