selecting key from openkeychain fails
k-9 does not accept the cryptographic key from openkeychain.

The key has been created newly by openkeychain using plain defaults from the
wizzard, except for uploading to the internet being off (as these are tests
yet only).

Steps in detail:

In k-9, after selecting the key from the list, the app takes a short breath
for about 1 sec. and then simply behaves as if it was newly started. Looks
like a quick app restart after a crash or so (but there is no crash message).
When entering the menu Cryptography / My key, there is still "no key
selected".

Tried with K-9/OpenKeyChain 5.502/4.9.2 as well as 5.403/4.9.

Android version: 7.1.2 / LineageOS. Security options were off for both
for both apps.


diff --git a/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpKeyPreference.java b/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpKeyPreference.java
index 15eb44693..dd4b5373d 100644
--- a/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpKeyPreference.java
+++ b/plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpKeyPreference.java
@@ -73,6 +73,15 @@ public class OpenPgpKeyPreference extends Preference {
 
     @Override
     protected void onClick() {
+        bindServiceAndGetSignKeyId(new Intent());
+    }
+
+    private void bindServiceAndGetSignKeyId(final Intent data) {
+        if (mServiceConnection != null && mServiceConnection.isBound()) {
+            getSignKeyId(data);
+            return;
+        }
+
         // bind to service
         mServiceConnection = new OpenPgpServiceConnection(
                 getContext().getApplicationContext(),
@@ -81,7 +90,7 @@ public class OpenPgpKeyPreference extends Preference {
                     @Override
                     public void onBound(IOpenPgpService2 service) {
 
-                        getSignKeyId(new Intent());
+                        getSignKeyId(data);
                     }
 
                     @Override
@@ -252,7 +261,7 @@ public class OpenPgpKeyPreference extends Preference {
         public SavedState(Parcel source) {
             super(source);
 
-            keyId = source.readInt();
+            keyId = source.readLong();
             openPgpProvider = source.readString();
             defaultUserId = source.readString();
         }
@@ -284,11 +293,11 @@ public class OpenPgpKeyPreference extends Preference {
 
     public boolean handleOnActivityResult(int requestCode, int resultCode, Intent data) {
         if (requestCode == REQUEST_CODE_KEY_PREFERENCE && resultCode == Activity.RESULT_OK) {
-            getSignKeyId(data);
+            bindServiceAndGetSignKeyId(data);
             return true;
         } else {
             return false;
         }
     }
 
-}
\\ No newline at end of file
+}
['plugins/openpgp-api-lib/openpgp-api/src/main/java/org/openintents/openpgp/util/OpenPgpKeyPreference.java']