简体   繁体   English

为什么 onNewIntent(Intent intent) 方法被调用两次?

[英]Why is the onNewIntent(Intent intent) method getting called twice?

I start a new activity with two parameters.我用两个参数开始一个新活动。

Intent intent = new Intent(WebTestActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);                  
Uri uri =  Uri.parse(url);
intent.setData(uri);
startActivity(intent);

And catch uri in onNewIntent method.并在onNewIntent方法中捕获uri

@Override
public void onNewIntent(Intent intent) {  //calls twice
    super.onNewIntent(intent);      
    Uri uri = intent.getData();
    new AsynkTask().execute(uri);
}

But the onNewIntent method is called twice for some unknown reason which doesn't seem to be right.但是由于某些不明原因, onNewIntent方法被调用了两次,这似乎是不正确的。

Intent intent = new Intent(WebTestActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);                  
intent.putExtra("url",url);
startActivity(intent)

In MainActivity;在主要活动中;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Bundle extras = getIntent().getExtras();
    String url = extras.getString(url);

}

Then, you can parse the url and use as Uri.然后,您可以解析 url 并用作 Uri。 By this way, method will not be called twice.这样方法就不会被调用两次。

onNewIntent(Intent intent) This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). onNewIntent(Intent intent) 对于在其包中将 launchMode 设置为“singleTop”的活动,或者如果客户端在调用 startActivity(Intent) 时使用了 FLAG_ACTIVITY_SINGLE_TOP 标志,则会调用此方法。

If it is called twice, it must have been called unintendedly: starting the activity twice?如果调用了两次,那肯定是无意中调用了:两次启动活动? Or manually calling the method?还是手动调用方法?

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

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