I have an app (lets call it "SendingApp") that is trying to launch my app by calling this on Button A:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.example.sendingapp");
launchIntent.putExtra("my_extra", "AAAA"));
startActivity(launchIntent);
and this on Button B:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.example.sendingapp");
launchIntent.putExtra("my_extra", "BBBB"));
startActivity(launchIntent);
In my own app (lets call that the "ReceivingApp") I have a launcher activity defined in the Manifest as such:
<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>
In the onCreate method of my MainActivity class in the ReceivingApp I receive the extra and output it to a TextView like this:
@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");
}
}
}
Starting The ReceivingApp normally by tapping its icon or starting to debug it from Eclipse works fine and the TextView reads "NORMAL START".
When I then close the ReceivingApp by pressing the back button and go to the SendingApp and press Button A, the ReceivingApp starts and displays AAAA as it should. If I go back again and press Button B the ReceivingApp is launched and displays BBBB. So far, so good.
When I force quit the ReceivingApp either in the task list or in the application manager and then go to the SendingApp and press Button A, the ReceivingApp will launch and display AAAA (still correct) but when I then go back and press Button B, the ReceivingApp will launch but not call onCreate and therefor not display BBBB but still show AAAA, just like it had been brought to foreground but had not received any intent. Pressing the back button in the ReceivingApp also shows that no new instance of MainActivity had been put on the stack of Activities.
Closing the ReceivingApp and starting it by clicking its icon fixes this behaviour. But I need it to be able to receive different intents, even when it was not running when it receives the first intent.
Has anyone come across this behaviour before? Is my code to receive the data wrong or could that be an Android bug?