当intent有Intent.FLAG_ACTIVITY_NO_HISTORY时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>
问题是在从图库中选择图像后,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是正确的行为。当您在调用下一个活动时选择 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?