在Android中,FLAG_ACTIVITY_NEW_TASK标志用于启动一个新的任务栈,并将活动放入该任务栈中。当使用此标志启动活动时,该活动不会恢复先前的实例,并且不会调用onNewIntent方法。如果需要在新任务栈中启动活动并调用onNewIntent方法,可以使用以下解决方案:
Intent intent = new Intent(context, YourActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(intent);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handleNewIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleNewIntent(intent);
}
private void handleNewIntent(Intent intent) {
// 处理新的意图
}
通过添加FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_SINGLE_TOP标志,可以确保活动在新任务栈中启动,并且在已存在的活动实例上调用onNewIntent方法。