android: concepts and examples of life-cycle Activity

Introduction: Programming is different from the C main function is initiated by a program in the Android system is to start the program by using the Activity; understand the use of Activity before, we must first look at some of the concept of Activity.

  Activity has a lifecycle, to create and manage their own lifecycle status callback method call system; Activity to manage their life cycle by;

  Activity in the life cycle will be instantiated, Activity instance is one of the key components of the Android app;

  Activity instance After starting will be concentrated on the task stack to manage, Activity startup mode will affect the task stack management;

  All Activity manifest.xml need now declared prior to use, generally created automatically when the Activity Statement;

<manifest >
    <application>
        <activity android:name=".OneActivity" />
        <activity android:name=".TwoActivity">
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="text/plain" />
            </intent-filter>
        </activity>
        <activity android:name=".ThreeActivity">
           <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
    </application>
</manifest >      

1, activity life cycle

  Watch the video when it was called in, after we take calls, always I want to continue to watch the video before, rather than re-watch the video;

  More functional decomposition is logic is this: After the jump ActivityB from ActivityA, and then return to the state before the ActivityA (equivalent to the microcontroller interrupt), rather than re-start ActivityA;

  Activityt divided by the life cycle of a different state, so when other Activity returns or calls, you can jump to some corresponding state rather than start over.

  1.1 life cycle approach

    Activity within the life cycle of each Activity is not necessary to perform the complete status of all methods;

    For constructing off operation, it is necessary to ensure release or terminate within the corresponding lifecycle methods;

    All the life-cycle methodology is as follows:

    1.1.1, onCreate (): must be called initialization Activity, examples of the Activity;

            After onCreate () method performs complete into the created state, Activity will not stay in its current state, but calls onStart () method immediately;

    1.1.2, onStart (): always immediately follow the onCreate ();

            After onStart () method implementation of End started to enter the state, Activity will not stay in its current state, but calls onResume () method immediately;

    1.1.3, onResume (): always followed in onStart () after;

            Activity with the system before the user interaction will be called the current method, capture user input and interaction;

            Most back-end logic is executed in this state until the current Activity loses focus;

    1.1.4, onPause (): Activity loses focus when the current method will be called to enter the paused state;

            Residence time of the paused state is not long, a small part of the resources will be in the onPause () method is released;

    1.1.5, onStop (): When Activity Other Activity is covered, or are not visible after the current method is called the release of most of the resources;

            At this point Activity example still in memory; if the user returns from other Activity will call onRestart () started to re-enter the state,

            Or call onDestory () to destroy the current instance of the Activity;

            Can () method will be permanently stored in the database information or in the room onStop;

            Activity Activity you want to call another app, you need to set up two Activity in the activity manifest the permission rights for SHARE_POST.

    1.1.6, onDestory (): when a user is revoked Activity, or call .finish (), or a configuration change, the current method calls;

            Activity ensure successful examples of cancellation, resources and processes to release resources in and out;

            Since the configuration enables Activity instances forced to write off, it still holds the information to be written off Activity in onSaveInstanceState system () method,

            Examples of state before the next can be restored after re-create the Activity;

2, activity jump method

  2.1, show jumping: Jump to tell what Activity Activity;

    2.1.1, new one the Intent, the way to specify the type of activity, then call startActivity () method, effort worry;

Intent intent = new Intent ( this , Bactivity.class);
startActivity(intent);

    2.1.2, the second wording of beans flavored with aniseed;

Intent intent = new Intent( );
intent.setClass(this , 另一个activity.class);
startActivity( );

    2.1.3, beans flavored with aniseed third wording: Only the middle row is not the same as 2.1.2;

intent.setClassName (. the Activity the this ,. ". COM username package name class name .Activity name.");

    2.1.4, beans flavored with aniseed fourth wording: Only the middle row is not the same as 2.1.2;

 intent.setComponent ( new new . the Component (the Activity the this ,. ".. COM class package name User name Name .Activity name"));

  2.2 Implicit Jump : Tell Activity Activity which can perform a task, you can jump to them;

    For implicit activity needs to jump acticity add <intent-filter> jump to assist in the manifest, it is not necessary to jump the display; 

<activity>
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>
</activity>

  2.2.1 All activity is required in the manifest declared before use, where the Action is manifest in the <intent-filter> name of the action;

Intent intent = new Intent();
intent.setAction("android.intent.action.MAIN"); 
startActivity(intent);

  2.3, with the value of the jump: https://www.cnblogs.com/caesura-k/p/11909907.html

3, activity of the data transfer, the data is added to the intent to pass;

  3.1, the incoming data (the operation Activity1)

    3.1.1, transfer of intent method: it is essentially a new method for a Bundle in intent, and then pass key name, key value;

 intent.putExtra (key name, key value);

    3.1.2, wrote a Bundle type, equivalent to the 3.1.1 package to write their own; (String type, key name plus "" key is String)

Bundle bundle = new Bundle( );

bundle.putString("键名",键值);

intent.putExtras(bundle);

  3.2, the data extracted (operation in the Activity2 ): String str = getIntent () getExtras () getString ( " keys");.

Bundle bundle = getIntent().getExtras();
String str1 = bundle.getString("键名");
int number = bundle.getInt(键名);

4, activity in the startup mode

  Definition of activity is managed by the task stack (Stack out after advanced), the task stack default name is the activity where the package name;

  There are four kinds of activity a startup mode, different modes of activity to start the task stack management, you can set mainfest in:

<activity
    android:launchMode="standard">
</activity>

  4.1, standard: standard mode

    An activity per actuation will push onto the stack, the return key is pressed each time, to move it out from the stack (pop) a current activity;

    Intent to jump from the current activity to use the current activity, will start a new activity instance, call onCreate ();

    Activity remaining three added a limitation on the basis of the standard mode;

  4.2, singleTop: Task stack multiplexed mode

    If the top of the stack activity is going to jump in activity, namely jump from the current activity to current activity, it does not call onCreate (),

    OnNewIntent () call multiplexing activity top of the stack;

  4.3, singleTask: within Task stack multiplexed mode

    If you want to start the activity already exists in the stack, then onNewIntent () call multiplexing stack activity;

    After the stack and clears the current activity (current activity above the activity) of all activity;

  4.4, singleInstance: Global singleton

    Each activity will be configured to start a task stack for global multiplexing, other app can also reuse the current app of the global task stack;

    As long as existed prior to this activity, it will call onNewIntent activity before multiplexing ();

  Those that pop out of the activity of it? It could be written off yet? Because the system configuration and written off?

Guess you like

Origin www.cnblogs.com/caesura-k/p/11934910.html
Recommended