Progamer
Progamer

Reputation: 188

Wrong Activity is opened after killing App

I have an App A that can be opened by another App B. This is the code that opens A from B:

    Intent intent = new Intent("com.example.EXAMPLE_ACTION");
    String string = "testString";
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_TEXT, string);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    // Verify that the intent will resolve to an activity
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }

In App A I have three Activities. The Main Activity, an Activity C and an Activity D. Activity C is used from App B to call App A. Thus, there is a intent-filter in the manifest that looks like this:

<activity android:name=".ActivityC">
    <intent-filter>
        <action android:name="com.example.EXAMPLE_ACTION"/>

        <category android:name="android.intent.category.DEFAULT"/>

        <data android:mimeType="text/plain"/>
    </intent-filter>
</activity> 

The only purpose of Activity C is to make sure that the app is in the right state and then call Activity D. Since this can be all done in the onCreate method, it looks like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("Test Intent", "Activity C onCreate");
    ----- make state correct if necessary
    Intent intent = new Intent(getApplicationContext(), ActivityD.class);
    startActivity(intent);
    finish();
}

This should work in theory and it does in most cases also if the App A is killed manually (home button -> kill). However, after killing App A and opening it successfully from App B, Activity C is not called anymore when App B is opening App A (see log below).

---------------- App A is started
Test Intent: MainActivity onCreate
Test Intent: MainActivity onStart
Test Intent: MainActivity onResume
Test Intent: MainActivity onPause
Test Intent: MainActivity onStop
---------------- App A is opened from App B
Test Intent: ActivityC onCreate
Test Intent: ActivityD onCreate
Test Intent: ActivityD onStart
Test Intent: ActivityD onResume
Test Intent: ActivityC onDestroy
Test Intent: ActivityD onPause
Test Intent: ActivityD onStop
Test Intent: MainActivity onDestroy
---------------- App A was killed manually
Test Intent: ActivityC onCreate
Test Intent: ActivityD onCreate
Test Intent: ActivityD onStart
Test Intent: ActivityD onResume
Test Intent: ActivityC onDestroy
Test Intent: ActivityD onPause
Test Intent: ActivityD onStop
---------------- App A is opened by App B
Test Intent: ActivityD onStart
Test Intent: ActivityD onResume

Can somebody enlighten me, why this happens? I'm wondering if it's a bug or if I'm misusing the Activity lifecycle/intent action.

Upvotes: 0

Views: 154

Answers (1)

Sardar Khan
Sardar Khan

Reputation: 841

This happened because your Activity D is not completly finished try to finish the Activity D so that Acivity D onDestroy method gets called and then it cannot do that . hope this help.

Upvotes: 1

Related Questions