question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

androidx lifecycle observer incompability

See original GitHub issue

Hey, after plenty of unsuccessful attempts on using the official youtube api together with androidx, I was recommended to use this library- which seems a whole lot more comprehensive and supported. Unfortunately, I’m still stuck with the detail that lifecycle requires observer to be of androidx type.

build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'com.google.android.gms.oss-licenses-plugin'

android {
     compileSdkVersion 28
     defaultConfig {
              applicationId "com.moomoo.testapp"
              minSdkVersion 21
              targetSdkVersion 28
              versionCode 1
              versionName "1.0"
     }

     buildTypes {
              release {
                  minifyEnabled false
                  proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
              }
      }
 }
 
dependencies {
     def lifecycle_version = "1.1.1"
     implementation fileTree(include: ['*.jar'], dir: 'libs')
     implementation 'com.google.android.material:material:1.0.0'                  // Material design
     implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'                    // Graphs
     implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:core:8.0.1'  // Youtube
     implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
}

public class SingleTestFragment extends Fragment  {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_single_test, container, false);

        YouTubePlayerView youtubePlayerView = rootView.findViewById(R.id.youtube_player_view);
        getLifecycle().addObserver(youtubePlayerView);  <---- issue with androidx and library
        youtubePlayerView.initialize(new YouTubePlayerInitListener() {
            @Override
            public void onInitSuccess(@NonNull final YouTubePlayer initializedYouTubePlayer) {
                initializedYouTubePlayer.addListener(new AbstractYouTubePlayerListener() {
                    @Override
                    public void onReady() {
                        String videoId = "6JYIGclVQdw";
                        initializedYouTubePlayer.loadVideo(videoId, 0);
                    }
                });
            }
        }, true);

        return rootView;
    }
}

I wouldnt mind doing the mentioned workaround, of manually releasing the player, if necessary - but I was also unable to figure that one out. Any suggestions?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:21 (10 by maintainers)

github_iconTop GitHub Comments

6reactions
setznercommented, Nov 8, 2018

While searching for solution to a different problem, I got this one working 👍 So now I’m using it (and you got a star, and soon license mention of course).

The solution was to add the two below attributes to my gradle.properties file

android.useAndroidX=true
android.enableJetifier=true
3reactions
PierfrancescoSoffritticommented, Nov 11, 2018

I’ve just updated the library to depend on androidx, new updated coming soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lifecycle - Android Developers
Lifecycle-aware components perform actions in response to a change in the lifecycle status of another component, such as activities and fragments.
Read more >
incompatible types androidx observer method - Stack Overflow
I'm trying to figure out MVVM.I figured out how to observe LiveData using ViewModel. Now I encountered a problem. private void ...
Read more >
What are Lifecycle-Aware Components in Android? - Medium
Every LifecycleOwner has Lifecycle, based on EVENT or STATE of this Lifecycle of LifecycleOwner, LifecycleObserver performs the action. Hands-On Project . We ......
Read more >
NI: TYPE_MISMATCH: "inferred type is Unit but Observer<T ...
Fragment import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LiveData import androidx.lifecycle.Observer import androidx.lifecycle.observe ...
Read more >
[Solved]-Android (Kotlin): Type mismatch in Observer-kotlin
this it should compile. viewModel.getSchedule().observe(this, object :androidx.lifecycle.Observer<in String> { override fun onChanged(t: String?) { TODO(" ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found