Exception after rotation in Android 10 when using ACTION_SEND intent

I’m getting an Exception after rotation in Android 10 when using ACTION_SEND intent. Earlier Android versions are OK (no exception).

I’ve created a bare-bones Android app that shows the problem. It is a single activity with a button. Pressing the button displays the Share dialog. Rotate the screen. App goes away (on Android 10).

package com.example.test1;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity
{
  
    Button btnTest;
    
    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        
        btnTest = findViewById( R.id.btnTest );
        btnTest.setOnClickListener( v -> this.DoTest1()  );
    }
    
   
    void DoTest1()
    {
        Intent sendIntent = new Intent();
        sendIntent.setAction( Intent.ACTION_SEND );
        sendIntent.putExtra( Intent.EXTRA_SUBJECT, "subject" );
        sendIntent.putExtra( Intent.EXTRA_TEXT, "text" );
        sendIntent.setType( "text/plain" );
        this.startActivity( sendIntent );
    }
}

Exception occurs here:

[C:AndroidSdksourcesandroid-29androidcontentContextWrapper.java]

@Override
public ApplicationInfo getApplicationInfo() 
{
    return mBase.getApplicationInfo(); // <-- mBase is null !
}

Anyone else seeing this ?

Answer

You need to add Intent.createChooser.

Builds a new ACTION_CHOOSER Intent that wraps the given target intent, also optionally supplying a title. If the target intent has specified FLAG_GRANT_READ_URI_PERMISSION or FLAG_GRANT_WRITE_URI_PERMISSION, then these flags will also be set in the returned chooser intent, with its ClipData set appropriately: either a direct reflection of getClipData() if that is non-null, or a new ClipData built from getData().

  Intent shareIntent = Intent.createChooser(sendIntent, null);
  startActivity(shareIntent);