On Android API 19 (4.4) the intent.createChooser method causes IntentServiceLeak

后端 未结 2 609
醉酒成梦
醉酒成梦 2021-01-12 09:33

Running my app on the new Android KitKat device (API 19, 4.4) I get \"Copied to Clipboard\" everytime I try to create an Intent chooser. This is happening on Youtube, Tumbl

2条回答
  •  逝去的感伤
    2021-01-12 09:59

    @clu Has the answer right, just backwards lol. It should be this:

    //Create the intent to share and set extras
    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
    sendIntent.putExtra(Intent.EXTRA_TEXT, message);
    sendIntent.setType("text/plain");
    
    //Check if device API is LESS than KitKat
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT)
        context.startActivity(sendIntent);
    else
        context.startActivity(Intent.createChooser(sendIntent, "Share"));
    

    This build check can be shortened to a one-liner as well:

    //Create the intent to share and set extras
    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
    sendIntent.putExtra(Intent.EXTRA_TEXT, message);
    sendIntent.setType("text/plain");
    
    //Check if device API is LESS than KitKat
    startActivity(Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT ? sendIntent : intent.createChooser(sendIntent, "Share"));
    

提交回复
热议问题