android - 从url打开后如何清除 Activity 中的 Intent 数据?

标签 android android-intent android-activity

我有Activity,可以直接从浏览器启动,调用url。

Activity 代码:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Uri data = getIntent().getData();
        Log.d(getClass().getName(), "onCreate data=" + data);

        getIntent().replaceExtras(new Bundle());
        getIntent().setAction("");
        getIntent().setData(null);
        getIntent().setFlags(0);

        if (data != null && isValidUrl(data)) {
            // open some special fragment
        } else {
            // open main fragment
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(MainActivity.class.getName(), "onDestroy");
    }

    //...
}

应用 list 代码:

    ...

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>

            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>

            <data
                android:host="example.com"
                android:pathPrefix="/"
                android:scheme="http"/>
        </intent-filter>
    </activity>

    ...

此代码完美运行。当我使用链接 (http://example.com/somepage.html) 从浏览器打开应用程序时,我得到以下输出:

D/com.package.MainActivity: onCreate data=http://example.com/somepage.html

但是如果我离开应用程序(使用后退按钮)并再次打开应用程序,从最近的菜单中,我得到相同的结果:

D/com.package.MainActivity: onDestroy
D/com.package.MainActivity: onCreate data=http://example.com/somepage.html

我想清除 onCreate 方法中的 Intent 数据。或者有没有办法检测应用程序何时从最近的菜单启动?

更新: 我试着这样做:

@Override
protected void onDestroy() {
    super.onDestroy();

    getIntent().replaceExtras(new Bundle());
    getIntent().setAction("");
    getIntent().setData(null);
    getIntent().setFlags(0);

    Log.d(MainActivity.class.getName(), "onDestroy");
}

但这并没有帮助。

最佳答案

这里是很旧的线程,但万一其他人正在寻找这个线程:

您可以通过检查标志 FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY 来检查您的 Intent 是否是由于从最近列表启动的应用程序而发送的

    if ((getIntent().getData() != null) && ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0)) {
            //Handle the url passed through the intent
        } else {
            //proceed as normal
        }

关于android - 从url打开后如何清除 Activity 中的 Intent 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34311501/

相关文章:

android - 不要拉伸(stretch) ViewPager 背景

android - 如何获取从默认图像库中选择的图像的正确方向

java - 从 onOptionsItemSelected 开始新 Activity

android - 当应用程序从后台进入前台时启动特定的 Activity

java - 使用操作栏选项卡在 fragment 之间切换

android - 这个 R.id.home 是什么?

android - C2DMBaseReceiver 类中的空指针异常报告(来自市场)

android - 在 android 中启动谷歌地图应用程序以获取行车路线

android - zxing onActivityResult 不在 Fragment 中调用,仅在 Activity 中

Android & Robotium - 需要额外的测试 Activity ?