1

check below my code,. this code is in my adapter. but the thing is my phone is totally supported app and shared but the lower version mobile not supported its forced to closed when click on share.

 share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String shareBody = "\t\t DIL KI BAAT \n\n" +sayari[position].toString().trim()+"\n\n\n"+ "https://play.google.com/store/apps/details?id="
                    + appPackageName;

            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            sharingIntent.setType("text/plain");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
            context.startActivity(Intent.createChooser(sharingIntent, context.getResources()
                    .getString(R.string.app_name)));
        }
    });

3 Answers3

4

You need to add this flag not for the sharing intent, but for intent you are starting activity with:

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);

Intent startIntent = Intent.createChooser(sharingIntent, context.getResources().getString(R.string.app_name));
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startIntent);
Vladyslav Matviienko
  • 10,610
  • 4
  • 33
  • 52
3

Do this before calling startActivity

sharingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); .If you don't use the activity context this is required.

Dishonered
  • 8,449
  • 9
  • 37
  • 50
1

Best way to get out of this issue is to start activity from adapters parent activity. Either use context object or use an Interface, pass them as parameter to adapter's constructor. Invoke a method in your activity using passed reference. Here is an example:

Create an interface:

public interface ActivityInteractor{
    public void showDetails();
}

Let your Activity imlement it:

public class MyActivity extends Activity implements ActivityInteractor{
    public void showDetails(){
        //do stuff
    }
}

Then pass your activity to ListAdater:

public MyAdapter extends BaseAdater{ private ActivityInteractor listener;

public MyAdapter(ActivityInteractor listener){
    this.listener = listener;
}

} And somewhere in adapter, when you need to call that Activity method:

listener.showDetails();
Ashwani Kumar
  • 834
  • 3
  • 16
  • 30