Article From:https://www.cnblogs.com/Shadowplay/p/9453435.html

Activitiesbrief introduction

activities”

opens the manifest file and adds a & lt; activity & gt; element as & lt; Application & gt; element child element to define activity. For example,

<manifest ... >
  <application ... >
      <activity android:name=".ExampleActivity" />
      ...
  </application ... >
  ...
</manifest >

     

> > intent filters is a very powerful feature on the Android platform. They provide the ability to start an activity based not only on explicit requests, but also on ambiguous requests. For example, a clear request will be reported.The complaint system opens the sending mail activity in Gmail app. In contrast, an ambiguous request tells the system to “open any activity that can send mail.” When the system UI asks the user what application to use when performing the task, this is I.Ntent filters is at work.

can use this feature by defining & lt; intent-filter & gt; attributes in & lt; activity & gt; elements. < intent-filter> the definition of the element contains a &lt.; action> elements and optional < category> elements and < data> elements. These elements can be combined to specify the type of intent that your activity can respond to. For example, the following code snippet showsShows how to configure activity that sends text data and receives requests from other activity:

<activity android:name=".ExampleActivity" android:icon="@drawable/app_icon">
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
</activity>

val sendIntent =" Intent().apply {
    action = Intent.ACTION_SEND
    type = "text/plain"
    putExtra(Intent.EXTRA_TEXT, textMessage)
}
startActivity(sendIntent)

<manifest>
<activity android:name="...."
   android:permission=”com.google.socialapp.permission.SHARE_POST”/>

       In order to call SocialApp, your app must configure permission in the SocialApp adapter:

<manifest>
   <uses-permission android:name="com.google.socialapp.permission.SHARE_POST" />
</manifest>

      For more information about permissions and security, check out Security and Permissions.

Managing the life cycle of activity

     In the life cycle of activity, activity has gone through many states. You can handle these transitions through a series of callbacks. These callbacks will be introduced in the next section.

onCreate()

     The callback method must be implemented, which is created when the system creates activity. The implementation of this method must initialize the basic components of the activity created. For example, the views created in app and the data bound to the list should be completed here. Most important of all,

The setContentView () method must be invoked in this method to define the layout of the user interface of activity.

      When the onCreate () method is executed, the next callback is onStart ().

onStart()

      When the onCreate () method is executed, activity enters the started state and becomes visible to the user. This callback method includes the final preparation of the front-end activity that makes the current activity interactive with the user.

onResume()

      The system calls this method only before the activity interacts with the user, at which point the activity is at the top of the activity stack and gets all the user input. Most of the core functions of a app are implemented in the onResume method.

      onPause()Methods are always invoked after the onResume method.

onPause()

      The system calls the onPause () method when activity loses focus and goes into a pause state. For example, when a user clicks the “back” or “recent” button, this happens. When the system calls the onPasuse () method for activity, it is technically based.Say, the activity is still partially visible, but usually indicates that the user is leaving the activity, and that the activity will quickly enter a stopped or resumed state.

      If the user expects to update UI, the activity in suspension can continue to update UI. For example, a activity that contains map navigation or media player. Even if these activity lose focus, users still want UI to continue to update.

      You should not save programs or user data, invoke networks, or perform database transactions in the onPause () method. For more information about data preservation, please refer to Saving and restoring activity state..

onStop()

     The system will call the onStop () method when activity is no longer visible to the user. This may be because the activity is being destroyed, a new activity is starting, or an existing activity is in a recovery state and is being overwrittenThe stopped activity. In all these cases, the stopped activity is no longer visible.

      If activity is returning the state of interaction with the user, the next callback method for the system call is onRestart (), which is called if the activity is completely terminated.

onRestart()

      When the activity in the stop state is about to restart, the callback will be invoked by the system. The state of onRestart () restoring activity to the activity stopped. The method always follows the onStart () method.

onDestroy()

      The system calls this method before activity destroys.

      This callback is the last callback method that activity receives, and onDesty () is usually implemented to ensure that all resources are released when the activity or process containing the activity is destroyed. This section only focuses on the lifecycle of activity.Mediate. For more detailed handling of the activity lifecycle and its callbacks, please refer to the activity life cycle.