-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[quick_actions] Implementing sharedpreferences approach for killed apps #1800
[quick_actions] Implementing sharedpreferences approach for killed apps #1800
Conversation
…even if the app was in the background
# Conflicts: # packages/quick_actions/CHANGELOG.md # packages/quick_actions/lib/quick_actions.dart # packages/quick_actions/pubspec.yaml
|
Why not just open the default flutter app and set it to |
collinjackson
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
I have a strong preference that we don't have this plugin depend on the shared_preferences plugin. If necessary, we can do things in the Java side of the plugin that uses Android SharedPreferences but it feels like it shouldn't be necessary here.
I'm wondering if it would be possible add some extras in the intent returned by getLaunchIntentForPackage(context.getApplicationContext().getPackageName()) and use that to pass the action info to main activity, where it could be retrieved from the registrar.activity() the call to registerWith.
|
@collinjackson Thank you for your review and especially for your suggestion! |
|
@collinjackson Can't we just launch the launcher Activity and forward the bundle arguments to the router in the dart side? No need to shared preferences. |
|
@BugsBunnyBR one issue I can see with your approach is that it's breaking the current behavior of the plugin on the Android side. Right now, when you click on a quick actions shortcut, and the app was already open, it just reopens it and delivers the action to the app. But with your |
|
@diesersamat , Yeah, it is a breaking change, since the current behavior is not the intent behavior, at least in the Android platform.
I am not saying not to merge your PR, it seems to fix a problem that exist in the current implementation, but I felt I should point that there is a design problem in the plugin. |
| // Get the Intent that started this activity and extract the string | ||
| Intent intent = getIntent(); | ||
| String type = intent.getStringExtra("type"); | ||
| if (channel != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran your code and this channel check will make the opening fail when channel != null.
Steps to reproduce:
- Install and open the app normally.
- Minimize the app by tapping in the home button.
- Use the app shortcut.
- Exit the app using the back button. ## Not the home button.
- Use the app shortcut. ## "nothing happens".
Possible fix that is not a breaking change:
In ShortcutHandlerActivity class :
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String type = intent.getStringExtra("type");
startActivity(getIntentToOpenMainActivity(this, type));
finish();
launchIntentForPackage.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
launchIntentForPackage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I know about this issue and it isn't something I wanted to fix in this PR. If you can, please, check the current version of the plugin and make sure that the bug was there before my fix. I don't know if I should fix it in this PR, maybe you can open an issue or even submit a PR? Or do you think that it's better to fix the bug in this PR? Anyway, I will check your solution, because I also faced this issue and was thinking about the ways to fix it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@collinjackson ☝️ ? Another PR? It seems small enough to land together with this version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BugsBunnyBR I know why does this happen. When you click the "Back" button, the activity is being killed. So, the activity is killed now, but channel reference and QuickActionsPlugin class instance is still alive. So, later, when you click on the shortcut, it tries to send a message to the channel, because it is not null — here is the difference with the bug fixed in this PR. But as you know, the view is already detached, and you'll get only this message in logcat FlutterView.send called on a detached view, channel=. Unfortunately, you cannot put a callback there to react on this error case and, maybe, start the activity again, because of API design. I've opened an issue a week ago flutter/flutter#35628 but maybe it is possible to fix without changes in FlutterNativeView API design...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. I know how to solve the problem. Just remove the static for the channel. In general, having static fields that are associated with context are a bad ideia. But removing the static makes that we need some other few changes, like listening to onNewIntent, that is why I think another pr will be needed to have a proper handling.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@diesersamat Can you please add a unit test for the added MethodChannel call?
It seems like there should be some sort of iOS implementation for getPassedIntent, even if it doesn't do anything.
The name getPassedIntent seems a bit confusing, since really what you're getting is the name of the action. Maybe calling it getLaunchAction or something like that would be clearer.
@BugsBunnyBR, if you're willing to share code for your suggested handling of the back button case, I think including that fix in this PR would be a great addition, but it won't block merging it.
| android:name=".QuickActionsPlugin$ShortcutHandlerActivity" | ||
| android:exported="false" | ||
| android:relinquishTaskIdentity="true" | ||
| tools:targetApi="lollipop" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just curious, what is this for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need tools:targetApi to hide a warning in android studio regarding android:relinquishTaskIdentity — the previous one. We can use targetApi — lollipop as the plugin anyways works on devices from API at least 25. Also, in case, if the app is killed and we open it using the shortcut, it will work fine but only the first time. If you click the Home button after that and use the same shortcut again it will not work the second and following times without android:relinquishTaskIdentity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you tried android:noHistory="true" ? As android:relinquishTaskIdentity="true" works only on API +25, all users in APIs below that will not have the same behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, exactly the same API version Android shortcuts and its classes were added ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh shortcuts are API 25+. So there is no problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will wait the merge to make another PR. I found that some more changes are needed. Sorry for the trouble.
Method renaming Added ios placeholder for getLaunchAction method
|
Thanks for the contribution. @BugsBunnyBR looking forward to your follow-on PR for the back button issue. |
|
@diesersamat, if you may, please try this #1856 . |
…ps (flutter#1800) * Implementing "intent extras" approach instead of shared preferences as suggested * Added some tests Method renaming Added ios placeholder for getLaunchAction method
Description
This PR addresses this issue. The issue is that Quick actions crash when the app is killed.
The problem for Android is located here, in QuickActionsPlugin.java.
When the user clicks on the quick actions item, the plugin opens
ShortcutHandlerActivity, and it is a simple one:So, as you can see, it just opens the activity, sends the message to the channel, and finishes the activity. If this approach is fine if the app is already launched, — you will just see the activity behind
ShortcutHandlerActivity, — it doesn't work if the app is killed. Just because after closing the helperShortcutHandlerActivityyou don't have any open activities left. One thing which could help is to open the main launcher activity if it is not open yet.And one more thing to notice is in quick_actions.dart. As you can see, the call
channel.invokeMethodis executedonCreate()of the very first activity,ShortcutHandlerActivity, and the method handler inquick_actions.dartis set ininitialize(), so, in this case with opening the killed app, handler is not set at the time wheninvokeMethodis executed. You have to save the intention to call the quick action somewhere, for example, in shared preferences and check later.That's exactly what was implemented in this PR.
Related Issues
issue 31235
Checklist
Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes (
[x]). This will ensure a smooth and quick review process.///).flutter analyze) does not report any problems on my PR.Breaking Change
Does your PR require plugin users to manually update their apps to accommodate your change?