onNewIntent not get called

北城余情 提交于 2019-12-11 04:55:38

问题


I have started a new activity of itself by calling start activity. But after the activity get started, in the method onNewIntent the finish() is not get called!!.

WebActivity.java

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    if (intent.getStringExtra("url") != null) {
        Intent intent1 = new Intent(getBaseContext(), WebActivity.class);
        intent1.putExtra("url",intent.getStringExtra("url"));
        startActivity(intent1);
        finish();
    }
}

回答1:


FYI

Debug at first and Remove below unwanted line

super.onNewIntent(intent);
setIntent(intent); 

Call finish() within Intent

Intent intent1 = new Intent(getBaseContext(), WebActivity.class);
intent1.putExtra("url",intent.getStringExtra("url"));
finish();
startActivity(intent1)

You can also try with FLAG_ACTIVITY_NO_HISTORY .

If set, the new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished. This may also be set with the noHistory attribute.




回答2:


It is because of the onNewIntent is not being called!!!

I put this on where this activity is get called.

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

then its works like a charm!!.

It is because of calling new activity. And FLAG_ACTIVITY_SINGLE_TOP makes onNewIntent get called. This FLAG_ACTIVITY_SINGLE_TOP will not start a new Activity.




回答3:


I was using onNewIntent for search implementation in Android. I came across the problem that onNewIntent wasn't being called when I used the Go button on the keyboard in the emulator. I solved the issue by placing my code for handling the intent in the onCreate method also. This needs to be done to make sure that the intent action is handled when a fresh instance of the activity is started.

This posed a problem as onCreate is called whenever the activity is restored from a previous state too. So, the intent handling method gets called even when an orientation change occurs.

Solution : Use if (savedInstanceState==null) to determine if activity is being restored from a previous state, or is it a fresh search.



来源:https://stackoverflow.com/questions/42197583/onnewintent-not-get-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!