Basic knowledge of the four major components of Android (1)

1 Android Components

       In 2018, I have already started 1/4. I feel that I can't indulge myself anymore. If I want to improve, I must take steps first. Writing a blog is my first step, and it is a testimony to whether I have grown this year. Colleagues, if there is something wrong with the writing, please give me more pointers. Your pointers will make me recognize myself more clearly. I would like to express my thanks in advance!

1、Activity

1. Function : It is one of the four major components of android, which can be used to display the media between view, app and user interaction.

2. Life cycle  onCreate onStart onResume onPause onStop onDestroy onRestart Seven life cycles.
Knowledge points:
①Activity visible life cycle onStart~onPause.
② In the onPause life cycle, you can do some data saving operations, which should not be too time-consuming, because when the interface jumps, only the onPause method is executed, and the next Activity can go to the onResume method.
③onStop can do some heavyweight recycling operations, and it should not be too time-consuming.
④onDestroy is the last life cycle, where we can do some recycling operations and release resources.

Some operations: Activity life cycle situation:
①When an Activity starts for the first time, it will go onCreate onStart onResume
②When A jumps to B's respective life cycle: if it is a transparent theme, A will go onPause, B will go onCreate onStart onResume, A will not go onStop, if it is an opaque theme, A will go onStop. The other life cycles are the same.
③ When returning to A, A will go onRestart onStart onResume
④When pressing the back button onPause onStop onDestroy (will pop out of the stack)
⑤ If the finish method is called directly in the onCreate method of the activity, the Activity will go onCreate onDestroy. In the development document, the three states of onResume, onPause, and onStop exist for a long time. If we directly call the finish method, there is no interaction, and this method will not be called.
⑤If the startup mode of an activity setting is: singleTask, on this page will go through those life cycles when starting this page: onPause, onNewIntent, onResume.

Activity statically loads fragments:
(1) The so-called static loading is to define a fragment, which is directly referenced by the name attribute in the dependent Activity layout file. The code is as follows:
<fragment    
    android:id="@+id/myFragment"    
    android:name="com.ugdao.activityandfragment.MyFragment"    
    android:layout_width="match_parent"    
    android:layout_height="wrap_content"></fragment>   
(2) The life cycle of the walk is as follows: the AAA label represents: Fragment, the aaa label represents: Activity (the life cycle of starting the walk)  
04-16 17:03:59.557 27124-27124/? E/AAA: onAttach:     
04-16 17:03:59.557 27124-27124/? E/AAA: onCreate:     
04-16 17:03:59.557 27124-27124/? E/AAA: onCreateView:     
04-16 17:03:59.567 27124-27124/? E/aaa: onCreate:     
04-16 17:03:59.567 27124-27124/? E/AAA: onStart:     
04-16 17:03:59.567 27124-27124/? E/aaa: onStart:     
04-16 17:03:59.567 27124-27124/? E/aaa: onResume:     
04-16 17:03:59.567 27124-27124/? E/AAA: onResume:
(3) Press the back button to destroy the activity. The life cycle of the activity is as follows: (Fragment leads the Activity)  
04-16 17:17:05.297 28898-28898/com.ugdao.activityandfragment E/AAA: onPause:     
04-16 17:17:05.297 28898-28898/com.ugdao.activityandfragment E/aaa: onPause:    
04-16 17:17:05.687 28898-28898/com.ugdao.activityandfragment E/AAA: onStop:     
04-16 17:17:05.687 28898-28898/com.ugdao.activityandfragment E/aaa: onStop:    
04-16 17:17:05.687 28898-28898/com.ugdao.activityandfragment E/AAA: onDestroy:    
04-16 17:17:05.687 28898-28898/com.ugdao.activityandfragment E/AAA: onDetach:    
04-16 17:17:05.687 28898-28898/com.ugdao.activityandfragment E/aaa: onDestroy:  
The life cycle of Activity dynamically loading fragments is as follows: (loading) (customize a fragment to dynamically replace the corresponding layout in the code)  
04-16 17:31:49.177 30550-30550/? E/aaa: onCreate:     
04-16 17:31:49.177 30550-30550/? E/aaa: onStart:     
04-16 17:31:49.177 30550-30550/? E/aaa: onResume    
04-16 17:31:57.857 30550-30550/com.ugdao.activityandfragment E/AAA: onAttach:     
04-16 17:31:57.857 30550-30550/com.ugdao.activityandfragment E/AAA: onCreate:     
04-16 17:31:57.867 30550-30550/com.ugdao.activityandfragment E/AAA: onCreateView:     
04-16 17:31:57.867 30550-30550/com.ugdao.activityandfragment E/AAA: onStart:     
04-16 17:31:57.867 30550-30550/com.ugdao.activityandfragment E/AAA: onResume  
The life cycle of pressing the back key to exit the walk is:
04-16 18:07:13.837 884-884/com.ugdao.activityandfragment E/AAA: onPause:   
04-16 18:07:13.837 884-884/com.ugdao.activityandfragment E/aaa: onPause:   
04-16 18:07:14.217 884-884/com.ugdao.activityandfragment E/AAA: onStop:   
04-16 18:07:14.217 884-884/com.ugdao.activityandfragment E/aaa: onStop:   
04-16 18:07:14.217 884-884/com.ugdao.activityandfragment E/AAA: onDestroy:   
04-16 18:07:14.217 884-884/com.ugdao.activityandfragment E/AAA: onDetach:   
04-16 18:07:14.217 884-884/com.ugdao.activityandfragment E/aaa: onDestroy:  

Life cycle when Activity is abnormal (minSdkVersion 15)

(1) When we start the activity normally, we will take the three life cycle methods of onCreate onStart onResume

(2) When we switch from portrait to landscape, the life cycle is as follows:

04-17 02:38:31.374 20350-20350/com.ugdao.activityandfragment E/aaa: onPause:   
04-17 02:38:31.386 20350-20350/com.ugdao.activityandfragment E/aaa: onStop:   
04-17 02:38:31.386 20350-20350/com.ugdao.activityandfragment E/aaa: onDestroy:   
04-17 02:38:31.532 20350-20350/com.ugdao.activityandfragment E/aaa: onCreate:   
04-17 02:38:31.534 20350-20350/com.ugdao.activityandfragment E/aaa: onStart:   
04-17 02:38:31.534 20350-20350/com.ugdao.activityandfragment E/aaa: onRestoreInstanceState:   
04-17 02:38:31.536 20350-20350/com.ugdao.activityandfragment E/aaa: onResume:

(3) On this basis, switch to the vertical screen, and the life cycle is as follows:

04-17 02:56:19.770 21306-21306/com.ugdao.activityandfragment E/aaa: onPause:   
04-17 02:56:19.779 21306-21306/com.ugdao.activityandfragment E/aaa: onStop:   
04-17 02:56:19.779 21306-21306/com.ugdao.activityandfragment E/aaa: onDestroy:   
04-17 02:56:19.845 21306-21306/com.ugdao.activityandfragment E/aaa: onCreate:   
04-17 02:56:19.846 21306-21306/com.ugdao.activityandfragment E/aaa: onStart:   
04-17 02:56:19.847 21306-21306/com.ugdao.activityandfragment E/aaa: onRestoreInstanceState:   
04-17 02:56:19.850 21306-21306/com.ugdao.activityandfragment E/aaa: onResume:  

(4) In the manifest file, configure the configChanges attribute for the activity, and set the attribute to orientation|screenSize. The code is as follows:

<activity android:name=".MainActivity"<span style="background-color:rgb(255,0,0);"> android:configChanges="orientation|screenSize"</span>>  
     <intent-filter>  
       <action android:name="android.intent.action.MAIN" />  
  
       <category android:name="android.intent.category.LAUNCHER" />  
     </intent-filter>  
</activity>  

After switching the horizontal and vertical screens, the life cycle will not be followed: only the onConfigurationChanged method will be used, and the horizontal and vertical screens will only be used once.

04-17 03:02:53.806 21684-21684/com.ugdao.activityandfragment E/aaa: onConfigurationChanged:

(5) In the manifest file, configure the configChanges attribute for the activity, and set the attribute to orientation|keyboardHidden. The code is as follows:

Result: same as 4.

important point:

When minSdkVersion >=13

Neither orientation nor orientation|keyboardHidden|screenSize will recall their respective lifecycles.

When minSdkVersion<13

(1) When the android:configChanges of the Activity is not set, each life cycle will be called again when the screen is cut, and it will be executed once when the horizontal screen is cut, and twice when the vertical screen is cut.

(2) When setting the android:configChanges="orientation" of the Activity, each life cycle will be called again when the screen is cut, and it will only be executed once when the screen is cut horizontally or vertically.

(3) When setting the android:configChanges="orientation|keyboardHidden" of the Activity, the screen cut will not re-call each life cycle, only the onConfigurationChanged method will be executed

How to disable horizontal and vertical screen switching:

1. Set the android:screenOrientation attribute value in the activity in AndroidManifest.xml to achieve this.

(1)竖屏:android:screenOrientation="portrait"

(2)横屏:android:screenOrientation="landscape"

2. Set it in the Java code by code similar to the following (this method is not recommended, and it will be slow when the large app starts in different directions)

(1)竖屏: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)

(2)横屏:setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

3. If you want to completely prohibit flipping, ignore the switching caused by gravity induction, (it does not work on the simulator, it is correct on the real machine)

(1) Ignore gravity: android:screenOrientation="nosensor"

Activity four startup modes:

(1)standard:默认模式,无论我们将要启动的activity在栈中的位置是否是在栈顶或栈的其它位置,都会重新创建一个实例。

(2)singleTop:栈顶模式:如果我们要启动的activity是在栈顶,在栈中就不会创建新的实例,会直接复用,如果不在栈顶,就会重新创建一个实例。

(3)singleTask:栈中单实例模式,我们将要启动的activity,如果其在栈中有实例就会直接复用这个实例,如果不是在栈顶,其上面的实例会出栈,其回到栈顶。

(4)singleInstance:特点是:启动界面的时候,它会创建一个新栈,栈中只有这一个实例,多用于跨进程通讯时候用。如:来电话显示界面。

service

1、简介:

service是Android四大组件之一,其可以在后台长时间的执行工作,它不和用户产生Ui交互。如果没有显示指定service的进程,那么service和activity是运行在当前app中的Ui Thread中,其不能做耗时操作,如果想做耗时操作,需要开启子线程进行。

2、service的启动方式及生命周期

(1)start方式启动:生命周期:

   onCreate onStartCommand onDestroy

  一般使用以下两种方法创建一个startService

  ①继承Service类

 ②继承IntentService类,intentService类父类是Service,若不同时处理多个请求,优先选择IntentService,重写onHandleIntent()方法,此方法已经默认帮我们开启了一个子线程,操作完成后不需要我们手动调用stopService,onHandleIntent()方法会主动调用此方法。当我们调用startService(intent),会立即回调startCommand方法。onHandleIntent方法是在onStartCommand方法之后执行。

注意:

①在项目中有可能多次启动了此service,但是当我们调用stopService()方法或者是stopSelf方法此服务就会停止。

②若系统正在调用多个onStartCommand请求,如果其中一个请求启动时,需要停止此请求,为了避免把整个服务停掉,需要选择stopSelf(int)方法,此方法的参数就是启动请求的id,当我们调用stopSelf(int)方法时候,此id会和startCommand方法中的启动id进行比较,如果相等,就停止,如果在调用stopSelf(int)之前,service又接受了一个新的start请求,那么这个启动请求的id与stopSelf(int)传递的id不一样,就不能提停止此服务,这样就解决了这个问题。

(2)service绑定方式:生命周期

  onCreate onBind onUnbind onDestroy

 注意:创建bound service 必须重写onBind回调方法,因为此方法返回一个IBind接口,此接口是组件和service的通讯的桥梁。

组件绑定此服务的时候,会获取IBind接口,通过类型转换,可以获取此service的实例,通过实例可以调用service的方法。代码如下:

}  
       ...  
     
       bindService(new Intent(MainActivity.this, MyService.class), serviceConnection, Context.BIND_AUTO_CREATE);  
         
       ...  
  
   }  
  
   ServiceConnection serviceConnection = new ServiceConnection() {  
       @Override  
       public void onServiceConnected(ComponentName componentName, <span style="background-color:rgb(255,0,0);">IBinder iBinder</span>) {  
  
           MyService myservice = (MyService) iBinder;  
           myservice.stopSelf(1);  
       }  
  
       @Override  
       public void onServiceDisconnected(ComponentName componentName) {  
  
       }  
   };  

注意点: 我们创建service时候一定要在清单文件中进行配置,如果是通过Android studio自动生成的不用配置。


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324651756&siteId=291194637