当活动恢复时,意图再次发送

时间:2015-09-03 10:31:15

标签: android android-intent

我正在使用共享意图让用户在我的应用程序中共享图像,因此当用户共享图像时,我的活动会将图像发送到服务器并显示在应用程序中。问题是,当用户返回并再次恢复应用程序时,会再次发生相同的意图过程。

这是我在onCreate()方法

中调用的意图处理代码
public void handleIntents(){

    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();


    if (Intent.ACTION_SEND.equals(action) && type != null) {
        if ("text/plain".equals(type)) {
           // handleSendText(intent); // Handle text being sent
        } else if (type.startsWith("image/")) {
            handleSendImage(intent); // Handle single image being sent
        }
    } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
        if (type.startsWith("image/")) {
            //handleSendMultipleImages(intent); // Handle multiple images being sent
        }
    } else {
        // Handle other intents, such as being started from the home screen
    }

}

这里是清单声明

<activity
        android:name=".HomeScreen"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateHidden"
        android:launchMode="singleTask">

        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*" />
           <!-- <data android:mimeType="audio/*" />
            <data android:mimeType="video/*" />-->
        </intent-filter>

    </activity>

因此,当我消耗了意图时,除非再次创建新意图,否则不应该发生。

注意:我建议请创建相同的情况,尝试系统上的代码。

我想要的是;我分享了一个从画廊到这个应用程序的图像。现在我按下然后从最近打开这个活动,

  1. 不应尝试重新发送图像。

    当我返回并再次选择图像并与我的应用程序共享时 它应该。

2 个答案:

答案 0 :(得分:1)

在您返回相同的活动后,Android将重复使用相同的意图以确保可以实现相同的状态,在处理图像后尝试setIntent(null)

从你的评论我认为我现在得到用例,  发生这种情况是因为活动没有完成所以每次调用onCreate方法因为屏幕旋转或系统重新创建活动(转到背景等),onCreate被调用并将通过你在第一次用于启动活动的相同意图,在这种情况下你可以做的是检查你在onCreate方法上收到的Bundle savedInstanceState,如果它是null那么它第一次创建活动时,您应该上传图像,否则正在重新创建活动,您无需上传图像。 此外,因为图像可能已成功上传,也可能没有成功上传,屏幕可能会旋转,上传停止等(这取决于您的上传方式,AsyncTaks,线程,服务等),您可能实际上想要尝试上传图像再次出现,在这种情况下,您可以按照@eduyayo的建议进行操作并使用额外的图像来表示图像已经上传

答案 1 :(得分:0)

你总是可以覆盖在onResume()上调用的内容。

现在想到的另一个想法是,你可以使用threads.And onResume,你确保线程不会启动。如何布尔应该做的工作。我会给你写一些伪代码,所以你能明白我在说什么。

Boolean name = false;

onCreate(){
  if(name = false){
          Run ur thread now
     }
}
onResume(){
  if(name = false){
          Run ur thread now
     }
}

应该做这个工作,伙伴。让我知道你是否需要更多的信息。我稍后会回来。

相关问题