Android不重用主要活动和(仅在使用后的PendingIntent行为)启动多个实例 [英] Android does not reuse main activity and starts multiple instances ( behavior only after using PendingIntent )

查看:123
本文介绍了Android不重用主要活动和(仅在使用后的PendingIntent行为)启动多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只有一个应用程序activity.In中的onCreate我显示一个通知,其上单击我展示我的活动,如果有必要重复使用它(或因此我认为)的code是:

I have an application with only one activity.In the onCreate I am showing a notification, on whose click I show my activity, reusing it if necessary ( or so I thought ) The code is:

package com.example.multiplestartupifusingpendingintent;

import android.app.Activity;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

    public static final String TAG = "hexor";

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

        Log.d(TAG,
                "onCreate " + getClass().getSimpleName()
                        + Integer.toHexString(hashCode()));

        NotificationManager notifMgr = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
        showReuseNotification(notifMgr);
    }

    private void showReuseNotification(NotificationManager notifMgr) {
        Intent reuseIntent = new Intent(this, MainActivity.class);
        reuseIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent reusePendingIntent = PendingIntent.getActivity(this, 2,
                reuseIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification.Builder builder = new Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(reusePendingIntent)
                .setContentTitle("Resue from memory");

        notifMgr.notify(2, builder.build());
    }

    @Override
    protected void onDestroy() {

        Log.d(TAG,
                "onDestroy " + getClass().getSimpleName()
                        + Integer.toHexString(hashCode()));
        super.onDestroy();
    }
}

清单:

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.multiplestartupifusingpendingintent.MainActivity"
            android:configChanges="orientation|screenSize"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我的问题是一个行为发生的事情,我不明白:

My problem is a behavior happening, that I don't understand:


  1. 如果我启动应用程序(第一次的onCreate调用),然后把它底色,然后点击桌面图标,同样的实例重用。在logcat中我可以看到仅有1的onCreate调用。这就是我想要的。

  1. If I start the application (first onCreate call ), then put it to backround, then click on its desktop icon, the same instance is reused. In logcat I can see just 1 onCreate call. It is what I want

如果我启动应用程序(第一次的onCreate调用),它放到后台,点击通知,然后单击桌面上的图标(第二的onCreate调用),然后我看到2中的onCreate调用logcat中。正如我preSS后,我两次去之前,我可以退出该应用程序,通常我看到2的onDestroy电话,因为我有2个相同的堆叠活动。

If I start the application ( first onCreate call ), put it to background, click the notification , then click the desktop icon ( second onCreate call ) , then I see 2 onCreate calls in logcat. As I press back I go twice before I can exit the app, and normally I see 2 onDestroy calls, since I have 2 identical stacked activities.

为什么过去的行为发生?为什么不Android的重用现有的活动,以及创建它们的2份堆叠?更奇怪,它为什么这样做只能在案件2 /为什么不这样做的情况下,1还?

Why is the last behavior happening? Why is Android not reusing the existing activity, and creates 2 copies of them stacked? More weird, why is it doing only in case 2/why is it not doing it in case 1 also?

感谢

推荐答案

在您发表评论第一种情况下,你的活动将暂停,并没有被破坏,所以从暂停状态转移到恢复状态。您可以检查活动生命周期,以更多地了解它。

In the first case you comment, your activity is paused and has not been destroyed, so it moves from paused state to resumed state. You may check the activity lifecycle to learn more about it.

您应该设置

android:launchMode="singleInstance"

将活动(在清单)和内接收新意向:

to your activity (in the manifest) and receive new intents inside:


    万一
  • 的onCreate(...)活动已被摧毁

  • onNewIntent(...)的情况下,该活动被暂停/恢复

希望它帮助。

这篇关于Android不重用主要活动和(仅在使用后的PendingIntent行为)启动多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆