简体   繁体   English

onNewIntent不会被调用

[英]onNewIntent not get called

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!!. 但是,在活动开始之后,在onNewIntent方法中不会调用finish() !!

WebActivity.java 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();
    }
}

FYI 费耶

Debug at first and Remove below unwanted line 首先调试并在不需要的行下面删除

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

Call finish() within Intent Intent中调用finish()

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 . 您也可以尝试使用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. 也可以使用noHistory属性设置。

It is because of the onNewIntent is not being called!!! 这是因为onNewIntent没有被调用!!!

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. FLAG_ACTIVITY_SINGLE_TOP使得onNewIntent被调用。 This FLAG_ACTIVITY_SINGLE_TOP will not start a new Activity . FLAG_ACTIVITY_SINGLE_TOP 将不会启动新的活动

I was using onNewIntent for search implementation in Android. 我在Android上使用onNewIntent进行搜索。 I came across the problem that onNewIntent wasn't being called when I used the Go button on the keyboard in the emulator. 我遇到了一个问题,当我使用模拟器键盘上的“执行”按钮时,没有调用onNewIntent I solved the issue by placing my code for handling the intent in the onCreate method also. 我通过将用于处理意图的代码也放在onCreate方法中解决了该问题。 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. 这就带来了一个问题,因为只要活动也从以前的状态恢复,就会调用onCreate 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. 解决方案:使用if (savedInstanceState==null)来确定活动是否从先前的状态中恢复,或者它是全新的搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM