7

I'm using app links with uni_links library in my flutter app.

There is a problem that whenever I clicked my app link in another application(like Google Calendar), the OS opens my app again.

Somehow I have a duplicate of that. Here is my screenshot that what exactly happened:

my screenshot

I just want to open my last app that exists in background.

What should I do to fix this problem?

Aref
  • 746
  • 10
  • 27

2 Answers2

11

Well according to this answer,

I should change android:launchMode="singleTop" to android:launchMode="singleTask" of my MainActivity in manifest.xml.

Aref
  • 746
  • 10
  • 27
1

If you are using uni_links, for android:launchMode="singleTask" you have to use _handleIncomingLinks to receive new intents

StreamSubscription? _sub;
void _handleIncomingLinks() {
if (!kIsWeb) {
  // It will handle app links while the app is already started - be it in
  // the foreground or in the background.
  _sub = uriLinkStream.listen((Uri? uri) {
    if (!mounted) return;
    print('got uri: $uri');
    
  }, onError: (Object err) {
    if (!mounted) return;
    print('got err: $err');
  });
 }
}

If using android:launchMode="singleTop" you have to use _handleInitialUri to receive new intents

bool _initialUriIsHandled = false;


Future<void> _handleInitialUri() async {

if (!_initialUriIsHandled) {
  _initialUriIsHandled = true;
  try {
    if (!mounted) return;
    final uri = await getInitialUri();

    if (uri != null) {
      //todo
    }
  } on PlatformException {
    // Platform messages may fail but we ignore the exception
    print('falied to get initial uri');
  } on FormatException catch (err) {
    if (!mounted) return;
    print('malformed initial uri');
  }
 }
}

for more detail about methods check this sample

Erfan Eghterafi
  • 4,344
  • 1
  • 33
  • 44