Activity onStop()方法调用两次

时间:2018-05-01 07:39:28

标签: android android-pendingintent activity-lifecycle

在锁定屏幕上从PendingIntent启动另一个活动会导致当前活动(onStop()状态)onStop()被调用两次。下面是测试代码。这是android或bug的预期行为吗?

  import android.app.PendingIntent;
    import android.content.Intent;
    import android.support.v4.app.NotificationCompat;
    import android.support.v4.app.NotificationManagerCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;



    public class Activity2 extends AppCompatActivity{


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_2);
        Log.w("app_test", "onCreate()");
        showNotification();
    }
    @Override
    protected void onResume() {
        Log.w("app_test", "onResume()");
        super.onResume();
    }

    @Override
    protected void onStop() {
        Log.w("app_test", "onStop()");
        super.onStop();
    }
    public void showNotification() {
        Intent intent = new Intent(this, TestActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "CHANNEL_ID")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Title")
                .setContentText("Content text")
                .setContentIntent(pendingIntent)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);
        NotificationManagerCompat.from(this).notify(102, mBuilder.build());
    }
}

以下是日志结果

W/app_test: onCreate()
W/app_test: onResume()
W/app_test: onStop()
W/app_test: onStop()

0 个答案:

没有答案