我有一个可以由另一个应用程序B打开的应用程序A.这是从B打开A的代码:
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);
}
在App A中我有三个活动。主要活动,活动C和活动D.活动C在App B中用于调用App A.因此,清单中有一个intent-filter,如下所示:
<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>
活动C的唯一目的是确保应用程序处于正确状态,然后调用活动D.因为这可以在onCreate方法中完成,所以它看起来像这样:
@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();
}
这应该在理论上有效,并且在大多数情况下也可以手动杀死App A(主页按钮 - &gt; kill)。但是,在杀死App A并从App B成功打开它之后,当App B打开App A时,不再调用Activity C(请参阅下面的日志)。
---------------- 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
有人可以启发我,为什么会这样?我想知道这是一个错误还是我滥用了Activity生命周期/意图行动。
答案 0 :(得分:1)
这是因为你的Activity D没有完成,试图完成Activity D以便调用Acivity D onDestroy方法然后它不能这样做。希望这有帮助。