android in practice_Preparing for configuration changes(MyMovies project)
来源:互联网 发布:电信4g是什么网络模式 编辑:程序博客网 时间:2024/06/05 00:08
Any configuration change will terminate the currently visible Activity and restart it using the new system configuration, so always be prepared for interruptions.
You need to perform tasks asynchronously and want to ensure that a worker thread always sees a valid instance of the Activity that created it, even if that Activity has
been destroyed.
Our idea is as follows: we’ll keep a reference to an Activity in the worker class, but we’ll make sure to reset it whenever that Activity instance changes due to a configuration
the Activity manages the worker instance it’s hosting.the worker manages the activity instace too.
Gracefully managing worker threads across configuration changes:
public class WorkerActivity extends Activity { private Worker worker; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //check whether a worker object is being passed from a previous instance of this Activity class worker=(Worker)getLastNonConfigurationInstance(); //if worker is not retained,create new one if(worker==null){ worker=new Worker(); worker.execute(); } worker.connectContext(this);//bind current activity } @Override protected void onDestroy() { super.onDestroy(); worker.disconnectContext();//unbind current activity } //pass worker to next activity @Override public Object onRetainNonConfigurationInstance() { return worker; }}Worker implementation that can (dis)connect its hosting Activity:
/* * keep a reference to an Activity, making sure that its hosting Activity can bind to the worker B and release itself */public class Worker extends AsyncTask<Void, Void, String> { private Activity context; //bind new activity instance public void connectContext(Activity context) { this.context = context; } //release current activity instance public void disconnectContext() { this.context = null; }@Overrideprotected String doInBackground(Void... params) {// TODO Auto-generated method stub try { Thread.sleep(3000);} catch (InterruptedException e) {}return "Work done!";} //Only interact with UI if Activity is valid protected void onPostExecute(String result) { if (context != null) { /* * in cases where our Activity gets destroyed without being recreated, * as is the case when hitting the back button, or in out-of-memory situations. */ Toast.makeText(context, result, Toast.LENGTH_LONG).show(); } }}
connect/disconnect pattern.One restriction our solution has is that it only works for activities. The Service class doesn’t define the onRetainNonConfigurationInstance method, so it can’t keep track of task objects it hosts (at least not that way).
Another restriction is that you can’t use the Activity instance in doInBackground because that method doesn’t run on the UI thread, but the task thread, and it’s not
guaranteed that it will always see the correct Activity instance.
If you absolutely can’t live with these restrictions, we have good news as well. The ignition Android application library (http://github.com/kaeppler/ignition) defines an implementation of AsyncTask called IgnitedAsyncTask that allows you to run jobs that are agnostic to the concrete type of their hosting Context, and which make sure
that in all three callback methods you’ll always see the correct context instance.
- android in practice_Preparing for configuration changes(MyMovies project)
- android in practice_Managing threads in thread pools(MyMovies project)
- android in practice_Implementing jobs with AsyncTask(MyMovies project)
- android in practice_Displaying splash screens with timers(MyMovies project)
- 【Android】Configuration Changes
- Changes in Android 7 Nougat
- Android 使用Headless Fragment 处理Configuration Changes情况
- Handling network configuration changes
- Activity configuration changes
- Android:JNI Local Reference Changes in ICS
- Android:JNI Local Reference Changes in ICS
- Notification Changes in Android Wear 2.0
- android 7.0 changes for NDK developers
- android in practice_Using a Service for caching data(portfolio project)
- C++ AMP: Changes in VS 11 Beta for C++ AMP
- Android for MSM Project
- Crosswalk Project for Android
- Android for MSM Project
- Java中的RTTI和反射机制
- Your First Step to the Silverlight Voice/Video Chatting Client/Server
- 三年规划(2012-2014年)
- 关于servlet和jsp路径表达方式的不同
- set intersection问题求解(python版)
- android in practice_Preparing for configuration changes(MyMovies project)
- hdu 2492
- 基于jQuery的一个简单聊天室
- soj 1162. Sudoku[Special judge] 1317. Sudoku
- 解决lein在Administrator目录的方法
- without base relocation /fixed:no
- Keil中预定义的字符串
- 黑马程序员-关于java中变量的总结
- 运动估计与运动补偿