Android onNewIntent Uri is always null

二次信任 提交于 2019-12-01 09:26:13

You should call getData() for the intent argument or perform setIntent(intent) before obtaining URI. onNewIntent() doesn't set new intent automatically.

UPDATE: So, here're two ways that you can implement onNewIntent(). The first replaces the old intent with the new one, so when you call getIntent() later, you will receive the new intent.

@Override
protected void onNewIntent(final Intent intent) {
    super.onNewIntent(intent);
    // Here we're replacing the old intent with the new one.
    setIntent(intent);
    // Now we can call getIntent() and receive the new intent.
    final Uri uri = getIntent().getData();
    // Do something with the URI...
}

The second way is to use data from the new intent leave the old one as-is.

@Override
protected void onNewIntent(final Intent intent) {
    super.onNewIntent(intent);
    // We do not call setIntent() with the new intent,
    // so we have to retrieve URI from the intent argument.
    final Uri uri = intent.getData();
    // Do something with the URI...
}

Of course, you can use a combination of two variants, but do not expect to receive the new intent from getIntent() until you explicitly set it with setIntent().

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