1

I have creating emoji keyboard in android but i can add share this app button in keyboard view.

here my on click share method.

public void shareapp() {
    String shareText = getResources().getString(R.string.share_text);
    Intent sendIntent = new Intent();
    sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, shareText + "https://play.google.com/store/apps/details?id=" + getPackageName());
    sendIntent.setType("text/plain");
    Intent chooseIntent = Intent.createChooser(sendIntent, "Share this via");
    getApplication().getApplicationContext().startActivity(chooseIntent);
}

but on click the share app button an error occur see Error:

01-11 13:36:03.728 8795-8795/com.example.napturalistamoji E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.napturalistamoji, PID: 8795
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
    at android.app.ContextImpl.startActivity(ContextImpl.java:747)
    at android.app.ContextImpl.startActivity(ContextImpl.java:734)
    at android.content.ContextWrapper.startActivity(ContextWrapper.java:345)
    at com.example.napturalistamoji.service.KeyboardService.shareapp(KeyboardService.java:685)
    at com.example.napturalistamoji.service.KeyboardService.onClick(KeyboardService.java:566)
    at android.view.View.performClick(View.java:5721)
    at android.view.View$PerformClick.run(View.java:22620)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:7331)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

any one can help me to solve my problems?

David Wasser
  • 93,459
  • 16
  • 209
  • 274
Ashish Virani
  • 182
  • 1
  • 15
  • You need to call `addFlags()` on the `chooseIntent` that you get back from `createChooser()`. – David Wasser Jan 12 '18 at 17:59
  • Marking this as duplicate isn't really helpful, as unfortunately the linked duplicate question doesn't have any clear answers to OP's question. – David Wasser Jan 12 '18 at 18:00

1 Answers1

2

You need to set flag to your intent when starting activity like this

Intent chooseIntent = Intent.createChooser(sendIntent, "Share this via");
chooseIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().getApplicationContext().startActivity(chooseIntent);

You setting a flag on wrong intent.

Jakub Gabčo
  • 257
  • 2
  • 14