adjust / flutter_sdk

This is the Flutter SDK of
MIT License
56 stars 47 forks source link

Initializing Adjust SDK using `Stateless` widget #89

Closed namini40 closed 1 year ago

namini40 commented 1 year ago

Hi there, hope you doing great! According to the documentation for calling onResume and onPause methods you have to use WidgetsBindingObserver and override didChangeAppLifecycleState method. if you use StatefulWidget at the root everything will be ok. the question is I have a Flutter app which all the widgets are Stateless. I could add observer using root widget constructor. but how to dispose it?


import 'package:flutter/material.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp( MyApp());
}

class MyApp extends StatelessWidget with WidgetsBindingObserver {
  MyApp({Key? key}) : super(key: key) {
    WidgetsBinding.instance.addObserver(this);
    // Adjust config and start
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        // Adjust on resume
        break;
      case AppLifecycleState.inactive:
        // TODO: Handle this case.
        break;
      case AppLifecycleState.paused:
        // Adjust on pause
        break;
      case AppLifecycleState.detached:
        // TODO: Handle this case.
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

I mean in this senario what can we do for initializing Adjust?

thanks

namini40 commented 1 year ago

any ideas on this one?

uerceg commented 1 year ago

Hi @namini40,

When it comes to Adjust Flutter SDK, we are interested in receiving app lifecycle callbacks because that information is needed for Android part of the SDK to be able to properly perform sessions tracking (we need information about app going to background and foreground). In theory, usage of WidgetsBindingObserver in order to get these activity lifecycle callback methods is something we saw as a way how Flutter devs were doing this mostly back in the days when we wrote the docs. Truth is that you are not really forced to follow that pattern in case that doesn't suit you for whatever reason. Only thing which you should keep in mind for Adjust SDK setup is that "you should make a call to Adjust.onResume() whenever your app comes to foreground and a call to Adjust.onPause() whenever your app goes to background". How exactly you intend to do that, that is fully up to you. If the way we suggest in our docs is not working for your app's use case and you know of better way to do that, we'd love to hear about it and update our docs in case there's room for improvement.

Cheers

namini40 commented 1 year ago

thanks for clear response. I'll comment anything I found about other ways to do so.