Using the intention of sharing in a "liquid manner",

background

many times, we want to allow the user to share something in Android applications.

this can be done using the following function:

public static Intent prepareSharingIntent() { final List<Intent> targetedShareIntents = new ArrayList<Intent>(); final Intent shareIntent = new Intent(Intent.ACTION_SEND); final Intent smsIntent = new Intent(Intent.ACTION_VIEW); targetedShareIntents.add(smsIntent); shareIntent.setType("text/plain"); smsIntent.setType("vnd.android-dir/mms-sms"); final String invitationMessage = "check out my App!"; smsIntent.putExtra(Intent.EXTRA_TEXT, invitationMessage); shareIntent.putExtra(Intent.EXTRA_TEXT, invitationMessage); shareIntent.putExtra("sms_body", invitationMessage); smsIntent.putExtra("sms_body", invitationMessage); shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Check out this app!"); smsIntent.putExtra(Intent.EXTRA_SUBJECT, "Check out this app!"); shareIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[] {})); final Intent chooserIntent = Intent.createChooser(shareIntent, "Share using..."); chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); return chooserIntent; } 

Problem

some users (like me) have many applications that can handle sharing intentions.

this means that sometimes there is a big delay (maybe a second or even more in some cases) from the moment I click to share something, while the dialog is displayed, so this is a rather undesirable behavior.

I have my patience, but maybe some users will think that the application is β€œlaggy” because of this.

In fact, I noticed that this problem occurs in many applications (including the application for the play store). the user does not receive feedback that the device is currently doing something to display a dialog.

Question

what can be done to make it more fluid or at least show some kind of dialogue of progress until a real dialogue appears?

+4
source share
1 answer

How about this: you put your prepareSharingIntent () code in AsyncTask and run this task when the application starts. Then you can save the global link to "chooserIntent" (for example, in the Application class) and show it when the link is not null and the user clicks the sharing button.

Be sure to run it with a low priority so as not to slow down the launch of applications.

 private class LongOperation extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); chooserIntent = prepareSharingIntent(); return "Executed"; } @Override protected void onPostExecute(String result) { } @Override protected void onPreExecute() { } @Override protected void onProgressUpdate(Void... values) { } } 
0
source

Source: https://habr.com/ru/post/1496525/


All Articles