0

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?

erinys
  • 273
  • 1
  • 4
  • 18
  • "just like it had been brought to foreground but had not received any intent" -- you will get the `Intent` in `onNewIntent()` if an existing instance of the activity is being brought back to the foreground for one reason or another. – CommonsWare Oct 20 '15 at 23:48
  • Thaks for your comment. onNewIntent is also not called on the second intent when the ReceivingApp had been forced closed before launching it through the first intent. – erinys Oct 21 '15 at 01:43
  • Not sure if this helps, Background Activity onCreate(Bundle) method will be called with the savedInstanceState it had previously supplied in onSaveInstanceState(Bundle) so that it can restart itself in the same state as the user last left it. In your case, you might not want the previous state. So I was thinking if you can just kill the receiving app when you exit, and it will be a fresh start whenever you start the activity. – KennethLJJ Oct 21 '15 at 04:24
  • killing the activity every time the user left it is not desirable as the activity is rather heavy and it would take a while to come back to the activity. Is there no way to just resume the activity with the new intent and then do something with it without it needing to restart or load a second instance? – erinys Oct 22 '15 at 19:45

0 Answers0