活动生命周期 - 当活动退出异常时,整个应用程序是否退出?

时间:2013-10-10 13:48:40

标签: java android android-activity

我的Android应用程序中有两个活动:MainActivity和Second Activity,在SecondActivity中有一些代码会导致应用程序FC,例如"123".substring(10);。在某些设备中,当SecondActivity停止时,整个应用程序退出,但在某些设备中,只有SecondActivity在异常后关闭,MainActivity仍处于前台。

整个应用程序何时退出,何时只有抛出异常的Activity退出?

MainActivity

package com.niklaus.lifecycle;

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

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) this.findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }});
    }

    @Override
    protected void onPause() {
        super.onPause();
        System.out.println("Main Activity onPause");
    }

    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("Main Activity onResume");
    }
}

SecondActivity

package com.niklaus.lifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SecondActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Button btn = (Button) this.findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                "123".substring(10);
            }});
    }

    @Override
    protected void onPause() {
        super.onPause();
        System.out.println("Second Activity onPause");
    }

    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("Second Activity onResume");
    }
}

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.niklaus.lifecycle"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.niklaus.lifecycle.MainActivity"
            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=".SecondActivity"></activity>
    </application>
</manifest>

0 个答案:

没有答案