Textview selectable share option crashing

152 views Asked by At

I want to share selected text in textview with the text selection toolbar. This is my textview.xml

<TextView
                android:id="@+id/txt_content"
                fontPath="fonts/Roboto-Regular.ttf"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:text="@{data}"
                android:bufferType="spannable"
                android:textColor="@color/md_black_1000"
                android:textIsSelectable="true"
                android:textSize="22sp" />

The textview content is a spannable with html code

binding.setData(SearchTextHighlight.highlight(searchKeyWord, Html.fromHtml(articles.getContent())));

I get the following error when i select share. I Hope my question is clear.

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:740)
                                                                       at android.app.ContextImpl.startActivity(ContextImpl.java:727)
                                                                       at android.content.ContextWrapper.startActivity(ContextWrapper.java:345)
                                                                       at android.widget.TextView.shareSelectedText(TextView.java:11284)
                                                                       at android.widget.TextView.onTextContextMenuItem(TextView.java:10833)
                                                                       at android.widget.Editor$TextActionModeCallback.onActionItemClicked(Editor.java:3681)
                                                                       at com.android.internal.policy.PhoneWindow$DecorView$ActionModeCallback2Wrapper.onActionItemClicked(PhoneWindow.java:4170)
                                                                       at com.android.internal.view.FloatingActionMode$4.onMenuItemSelected(FloatingActionMode.java:108)
                                                                       at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:801)
                                                                       at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:155)
                                                                       at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:954)
                                                                       at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:944)
                                                                       at com.android.internal.view.FloatingActionMode$5.onMenuItemClick(FloatingActionMode.java:136)
                                                                       at com.android.internal.widget.FloatingToolbar$FloatingToolbarMainPanel$1.onClick(FloatingToolbar.java:1298)
                                                                       at android.view.View.performClick(View.java:5698)
                                                                       at android.widget.TextView.performClick(TextView.java:10888)
                                                                       at android.view.View$PerformClick.run(View.java:22570)
                                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:158)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:7231)
                                                                       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)
1

There are 1 answers

1
Omar Dhanish On

add this line , to the Intent you used to share

Intent i = new Intent(.........);
//add this line
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);