简体   繁体   中英

Android: Starting a sharing intent closes my activity

In my fragment when I try to lunch a sharing intent, it closes my application. I want the intent to appear on top of my application and when the user finishes sharing and presses the back button, I want my application to appear. Here is my manifest file

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.joyfm.activity"
    android:versionCode="5"
    android:versionName="1.4" >

    <uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="17" />

   <uses-permission android:name="android.permission.INTERNET"/>
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:theme="@style/Theme.Sherlock"
        android:allowBackup="true"
        android:label="@string/app_name"
        android:icon="@drawable/logo_launcher">
        <activity
            android:theme="@style/Theme.Sherlock"
            android:name="com.joyfm.activity.MainActivity"
            android:configChanges="orientation|screenSize|keyboardHidden"
            android:label="@string/app_name" >
        </activity>
         <activity
            android:name="com.joyfm.activity.SplashActivity"
            android:theme="@style/Theme.Sherlock"
            android:launchMode="singleTask"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Here is my code in my EDITED fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    String authorInformation = "<i><BR>" + news.getAuthorInfo() + "<BR>\u00A9 www.myjoyonline.com</i>";

    ScrollView layout = (ScrollView) inflater.inflate(R.layout.news_viewer, container, false);
    TextView title = (TextView) layout.findViewById(R.id.txtTitle);
    TextView summaryDetails = (TextView) layout.findViewById(R.id.summaryDetails);
    TextView date = (TextView) layout.findViewById(R.id.txtNewsDate);
    TextView copyRight = (TextView) layout.findViewById(R.id.txtCopyRight);
    TextView newsDetails = (TextView) layout.findViewById(R.id.txtNewsDetails);
    ImageViewSpinner image = (ImageViewSpinner) layout.findViewById(R.id.thumbNail);

    title.setText(Html.fromHtml(news.getTitle()));
    summaryDetails.setText(Html.fromHtml(ITALIC_START + news.getDescription() + ITALICS_END));
    date.setText(Html.fromHtml(ITALIC_START + news.getDate() + ITALICS_END));
    newsDetails.setText(Html.fromHtml(news.getNewsItem()));
    copyRight.setText(Html.fromHtml(authorInformation));
    if(news.getThumbnailUrl().trim().length() > 0)
    {
        imageDownloader.download(image, news.getThumbnailUrl(), null);
    }else
    {
        image.setVisibility(View.GONE);
    }

    shareContentSubject = title.getText();
    shareContentDetails = newsDetails.getText();
    image.setClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {

                try
                {
                    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                     sharingIntent.setType("text/plain");
                     sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, SHARED_SUBJECT + shareContentSubject);
                     sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareContentDetails);
                     startActivity(Intent.createChooser(sharingIntent, "Share with"));          
                }catch(Exception ex)
                {
                    ex.printStackTrace();
                }
        }
    });
    return layout;
}

Please help, I have struggled with this and tried everything.

Register your Activity for the Share Intent in Android Mainfest file and check it once

 <activity
   android:name=".ActivityTest"
   android:label="@string/app_name" >
   <intent-filter>
     <action android:name="android.intent.action.SEND" />      
     <category android:name="android.intent.category.DEFAULT" />
      <data android:mimeType="text/plain" />    
   </intent-filter>
 </activity> 

Issue Resolved. I had this in my activity so it was destroying it when the intent pops up.

@Override
protected void onUserLeaveHint()
{
    super.onUserLeaveHint();
    finish();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM