当intent有Intent.FLAG_ACTIVITY_NO_HISTORY时onActivityResult不执行

发布于 2024-11-26 05:40:40 字数 2040 浏览 0 评论 0原文

A.java

public class A extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.d(A.class.getName(), "OnCreate");

        Intent intentB = new Intent(this, B.class);
        intentB.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivity(intentB);
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(A.class.getName(), "onActivityResult");
    }
}

B.java

public class B extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.d(B.class.getName(), "OnCreate");

        Intent imagesIntent = new Intent(Intent.ACTION_GET_CONTENT);
        imagesIntent.setType("image/*");
        Intent openGalleryIntent = Intent.createChooser(imagesIntent, "pic");
        startActivityForResult(openGalleryIntent, 2);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(B.class.getName(), "onActivityResult");
        Log.d(B.class.getName(), data.getData().toString());
    }

}

AndroidMenifest.xml

<uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".A"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="B"></activity>

    </application>

问题是在从图库中选择图像后,onActivityResult()方法B类的类不执行。

不过,如果从intentB实例中删除intentB.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY),则B类的onActivityResult()可以正常执行。

A.java

public class A extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.d(A.class.getName(), "OnCreate");

        Intent intentB = new Intent(this, B.class);
        intentB.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivity(intentB);
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(A.class.getName(), "onActivityResult");
    }
}

B.java

public class B extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.d(B.class.getName(), "OnCreate");

        Intent imagesIntent = new Intent(Intent.ACTION_GET_CONTENT);
        imagesIntent.setType("image/*");
        Intent openGalleryIntent = Intent.createChooser(imagesIntent, "pic");
        startActivityForResult(openGalleryIntent, 2);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(B.class.getName(), "onActivityResult");
        Log.d(B.class.getName(), data.getData().toString());
    }

}

AndroidMenifest.xml

<uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".A"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="B"></activity>

    </application>

The problem is after selecting image from gallery, onActivityResult() method of class B class does not execute.

Though if intentB.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY) is removed from intentB instance then onActivityResult() of class B executes fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹉夏雨初晴づ 2024-12-03 05:40:40

这是正确的行为。当您在调用下一个活动时选择 FLAG_ACTIVITY_NO_HISTORY 作为标志时。子活动将从堆栈中删除,因此一旦您移出,它就会简单地完成。

参考

问题是你为什么要使用该标志?

This is the correct behavior. When you select FLAG_ACTIVITY_NO_HISTORY as the flag while calling the next activity. The child activity is removed from the stack and hence once you move out it would simply finish.

Reference

The question is why do you even want to use that flag?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文