activity life cycle 시작과 끝

IT(Old)/Android 2007. 12. 10. 16:52

Method Description Killable? Next
onCreate() Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.

Always followed by onResume()

No onResume()
    onStart() Called when the activity is becoming visible to the user.

Followed by onResume() if the activity is being first created, or onRestart() if it is being shown again after previously being stopped.

No onRestart() or onResume()
onRestart() Called after your activity has been stopped, prior to it being resumed again.

Always followed by onResume().

No onResume()
    onResume() Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.

Followed by either onFreeze() if another activity is being resumed in front of it, or onPause() if this activity is being finished.

No onFreeze() or
onPause()
onFreeze() Allows you to save away your current state, when your activity is being paused and another one resuming to interact with the user. After being paused, the system may at any time need to stop (or even outright kill) your application in order to claim resources for the current foreground activity. If this should happen, the state you supply here will later be given back to you onCreate() when a new instance of your activity is started to interact with the user.

Always followed by onPause().

No onPause()
onPause() Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns.

Followed by either onResume() if the activity returns back to the front, or onStop() if it becomes invisible to the user.

Yes onResume() or
onStop()
onStop() Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed.

Followed by either onRestart() if this activity is coming back to interact with the user, or onDestroy() if this activity is going away.

Yes onStart() or
onDestroy()
onDestroy() The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method. Yes nothing

Note the "Killable" column in the above table -- for those methods that are marked as being killable, after that method returns the process hosting the activity may killed by the system at any time without another line of its code being executed. Thus you should take advantage of onFreeze() (for saving your current UI state) and onPause() (for writing any edits to persistent storage) so that the activity will be correctly restored to its current state in the event that it is killed. See the Process Lifecycle section for more information on how the lifecycle of a process is tied to the activities it is hosting.

For those methods that are not marked as being killable, the activity's process will not be killed by the system starting from the time the method is called and continuing after it returns. Thus an activity is in the killable state, for example, between after onPause() to the start of onResume().

android GUI vs XML (android/samples/app)

IT(Old)/Android 2007. 12. 4. 12:23

forwarding.xml

사용자 삽입 이미지
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

    <TextView
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_weight="0"
        android:paddingBottom="4dip"
        android:text="@string/forwarding"/>

    <Button id="@+id/go"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="@string/go">
        <requestFocus />
    </Button>

</LinearLayout>
-----------------------------------------------------------------------------------------------------
2. hello_world.xml

사용자 삽입 이미지

<TextView xmlns:android="http://schemas.android.com/apk/res/android" id="@+id/text"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:gravity="center_vertical" android:textAlign="center"
    android:text="@string/hello_world"/>

-----------------------------------------------------------------------------------------------------
3. save_restore_state.xml

사용자 삽입 이미지
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

    <TextView id="@+id/msg"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_weight="0"
        android:paddingBottom="4dip" />

    <TextView
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_weight="0"
        android:paddingBottom="4dip"
        android:text="@string/saves_state"/>

    <EditText id="@+id/saved"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/green"
        android:text="@string/initial_text">
        <requestFocus />
    </EditText>

    <TextView
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_weight="0"
        android:paddingTop="8dip"
        android:paddingBottom="4dip"
        android:text="@string/no_saves_state"/>

    <EditText
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/red"
        android:text="@string/initial_text">
    </EditText>

</LinearLayout>

-----------------------------------------------------------------------------------------------------
4. receive_result.xml

사용자 삽입 이미지

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

    <TextView
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_weight="0"
        android:paddingBottom="4dip"
        android:text="@string/receive_result_instructions"/>

    <TextView id="@+id/results"
        android:layout_width="fill_parent" android:layout_height="10dip"
        android:layout_weight="1"
        android:paddingBottom="4dip"
        android:background="@drawable/green">
    </TextView>

    <Button id="@+id/get"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="@string/receive_result_result">
        <requestFocus />
    </Button>

</LinearLayout>
-----------------------------------------------------------------------------------------------------
5. redirect_enter.xml

사용자 삽입 이미지

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

    <TextView
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_weight="0"
        android:paddingBottom="4dip"
        android:text="@string/redirect_enter"/>

    <Button id="@+id/go"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="@string/go">
        <requestFocus />
    </Button>

</LinearLayout>

-----------------------------------------------------------------------------------------------------
6. translucent_background.xml

사용자 삽입 이미지
<TextView xmlns:android="http://schemas.android.com/apk/res/android" id="@+id/text"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:gravity="center_vertical" android:textAlign="center"
    android:text="@string/translucent_background"/>

-----------------------------------------------------------------------------------------------------
7. TranslucentFancyActivity.java

사용자 삽입 이미지

protected void onCreate(Bundle icicle)
    {
        // Be sure to call the super class.
        super.onCreate(icicle);

        // Have the system blur any windows behind this one.
        getWindow().setFlags(WindowManager.LayoutParams.BLUR_BEHIND_FLAG,
                WindowManager.LayoutParams.BLUR_BEHIND_FLAG);
       
        // Apply a tint to any windows behind this one.  Doing a tint this
        // way is more efficient than using a translucent background.  Note
        // that the tint color really should come from a resource.
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.tintBehind = 0x60000820;
        getWindow().setAttributes(lp);
       
        // See assets/res/any/layout/translucent_background.xml for this
        // view layout definition, which is being set here as
        // the content of our screen.
        setContentView(R.layout.translucent_background);
    }