onNewIntent is not called

后端 未结 7 2096
暗喜
暗喜 2020-12-01 05:32

I have very strange situation.
Having one app, I decided to create another one from the code of first one.
I copied .xml files, copied .java files so that everythin

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 05:42

    Here's one situation that might bite you, along with a bit more information: onNewIntent is called as expected with this code:

    Intent intent = new Intent...
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    finish();
    startActivity(intent);
    

    but not with this code

    Intent intent = new Intent...
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(intent);
    finish();
    

    The reason for that is that, in the second code block, the new activity is added on top before calling finish for the current activity, so to get the second code block to work you must add the FLAG_ACTIVITY_CLEAR_TOP flag as suggested by some of the other answers, like this:

    Intent intent = new Intent...
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    

    Note that finish is not called at all here, because it is called for you. From the documentation for the FLAG_ACTIVITY_CLEAR_TOP flag:

    If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

提交回复
热议问题