コンピュータクワガタ

かっぱのかっぱによるコンピュータ関連のサイトです

Androidアプリ入門 No.60 ライフサイクル Activityのライフサイクル

ライフサイクル

Activityのライフサイクル

ここで、onPauseとonStop、またその他の状態の確認をする。まず、以下の条件でアプリケーションを作成する。

項目 内容
Project LifeTest
Build Target Android 1.6
Application name LifeTest
Package name sample.lt
Create Activity LifeTest1
Min SDK Version 4

Activityは、LifeTest1とLifeTest2の2つを使用する。まずは、LifeTest1用のレイアウトのmain.xmlを以下のようにする。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button
    android:id="@+id/button"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="LifeTest2を呼び出す"
    />
<TextView
    android:id="@+id/testView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    />
</LinearLayout>

次にLifeTest1.javaを以下のようにする。内容としては、ボタンを押したときにLifeTest2.javaを呼び出すことである。

package sample.lt;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class LifeTest1 extends Activity {
    private static final String LOG_TAG = LifeTest1.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(LOG_TAG, "onCreate");
        setContentView(R.layout.main);

        Button button = (Button) findViewById(R.id.button);
        TextView textView = (TextView) findViewById(R.id.testView);
        textView.setText("こんにちは");

        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), LifeTest2.class);
                intent.setAction(Intent.ACTION_VIEW);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(LOG_TAG, "onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(LOG_TAG, "onResume");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(LOG_TAG, "onRestart");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(LOG_TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(LOG_TAG, "onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(LOG_TAG, "onDestroy");
    }
}

次に、LifeTest2のレイアウトファイルである、main2.xmlを以下のように定義する。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="LifeTest2"
    />
</LinearLayout>

LifeTest2.javaの定義は以下のようにする。

package sample.lt;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class LifeTest2 extends Activity {
    private static final String LOG_TAG = LifeTest2.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(LOG_TAG, "onCreate");
        setContentView(R.layout.main2);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(LOG_TAG, "onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(LOG_TAG, "onResume");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(LOG_TAG, "onRestart");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(LOG_TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(LOG_TAG, "onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(LOG_TAG, "onDestroy");
    }
}

最後に、AndroidManifest.xmlファイルを以下のようにする。Activityを呼び出すのでその定義を追加するだけである。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="sample.lt"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".LifeTest1"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".LifeTest2">
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="4" />
</manifest>

ここでもう一度、動作を確認する。LifeTestを起動し、起動後にボタンを押し、戻るボタンで戻ると以下のようなログ出力になる。

LifeTestを起動するとLifeTest1のActivityが起動する。その際、onCreate、onStart、onResumeの順でメソッドが呼び出される。ここで、ボタンを押しLifeTest2のActivityが呼び出されるとLifeTest1のonPauseが呼び出されLifeTest1が一時停止される。その後、LifeTest2のonCreate、onStart、onResumeメソッドが呼び出される。その後にLifeTest1のonStopが呼び出される。
Activityの切り替えを行うと基本的には、呼び出し元のActivityはonPauseメソッドが呼び出されて、一時停止の状態になる。また、長期間元のActivityが使用されないとさらにonStopメソッドが呼び出される。
次に元のActivityに戻る場合を確認する。onPauseまでで停止していた場合には、戻るボタンでonResumeから復帰する。また、onStopまでで停止していた場合には、onRestart、onStart、onResumeの順で復帰する。
また、他のアプリケーションでメモリを大量に使用した場合にはプロセスがkillされる場合がある。その場合には、onCreateからの実行になる。
確認のために以下のアプリケーションを作成する。

項目 内容
Project name ManyMemory
Build Target Android 1.6
Application name ManyMemory
Package name sample.mm
Create Activity ManyMemory
Min SDK Version 4

ManyMemory.javaを以下のようにする。

package sample.mm;

import android.app.Activity;
import android.os.Bundle;

public class ManyMemory extends Activity {
    private String[] temp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        temp = new String[200000];
        for (int i = 0; i < temp.length; i++) {
            temp[i] = "" + i;
        }
    }
}

まず、先の例のLifeTestを実行する。実行後、「ホーム」ボタンを押して一度Activityを閉じる。ここまでのログを確認する。「ホーム」ボタンを押してもアプリケーション自体が終了していないのが確認できる。

この状態でManyMemoryを実行する。メモリを無駄に使用するアプリケーションのため実行には時間がかかる。起動後再度LifeTestを起動すると以下のようなログが出力される。ログを見るとわかるが、「Process sample.lt」has diedとなっており、そのpidが601となっている。pid601のプロセスが死んでおり、再度onCreateから実行されていることがわかる。

つまり、メモリを大量に消費するアプリケーションが実行された場合には起動中のアプリケーションが自動で終了されることがあるということがわかる。