0

我有一个应用程序(我们称之为“SendingApp”),它试图通过在Button A上调用它来启动我的应用程序:

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.example.sendingapp");
launchIntent.putExtra("my_extra", "AAAA"));
startActivity(launchIntent);

这在按钮 B上:

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.example.sendingapp");
launchIntent.putExtra("my_extra", "BBBB"));
startActivity(launchIntent);

在我自己的应用程序(我们称之为“ReceivingApp”)中,我在 Manifest 中定义了一个启动器活动,如下所示:

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

在 ReceivingApp 中我的MainActivity类的 onCreate 方法中,我收到额外的内容并将其输出到 TextView,如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = getIntent();
    if(intent != null) {
        Bundle extras = intent.getExtras();
        if(extras != null && extras.getString("my_extra") != null){
            ((TextView)findViewById(R.id.test_text)).setText(extras.getString("my_extra"));
        } else {
            ((TextView)findViewById(R.id.test_text)).setText("NORMAL START");
        }
    }
}

通过点击其图标或开始从 Eclipse 调试它来正常启动 ReceivingApp 工作正常,并且 TextView 读取“正常启动”。

然后,当我通过按后退按钮关闭 ReceivingApp 并转到 SendingApp 并按按钮 A 时,ReceivingApp 将启动并按应有的方式显示 AAAA。如果我再次返回并按下按钮 B,则会启动 ReceivingApp 并显示 BBBB。到目前为止,一切都很好。

当我在任务列表或应用程序管理器中强制退出 ReceivingApp 然后转到 SendingApp 并按下按钮 A 时,ReceivingApp 将启动并显示 AAAA(仍然正确)但是当我返回并按下按钮 B 时, ReceivingApp 将启动但不调用onCreate,因此不显示 BBBB 但仍显示 AAAA,就像它已被带到前台但没有收到任何意图一样。在 ReceivingApp 中按下后退按钮也表明没有 MainActivity 的新实例被放入活动堆栈中。

关闭 ReceivingApp 并通过单击其图标启动它可修复此行为。但我需要它能够接收不同的意图,即使它在接收到第一个意图时没有运行。

有没有人遇到过这种行为?我接收数据的代码是错误的还是可能是 Android 错误?

4

0 回答 0