Java – why is the onnewintent (intent) method called twice?
•
Java
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 capture the URI in the onnewintent method
@Override public void onNewIntent(Intent intent) { //calls twice super.onNewIntent(intent); Uri uri = intent.getData(); new AsynkTask().execute(uri); } }
But it seems incorrect that the onnewintent method is called twice for some unknown reason
Solution
Intent intent = new Intent(WebTestActivity.this,MainActivity.class);
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); }
You can then parse the URL and use URI In this way, the method will not be called twice
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码