1

I am taking the Udacity nanodegree program for Android Basics. I am on the Tour Guide App project, which requires a ViewPager, a TabLayout, and Fragments.

When I follow various tutorials and examples, I create a TestAdapter.java class. It begins with this:

package com.example.android.tourguide001;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

public class TestAdapter extends FragmentPagerAdapter {
    public TestAdapter(FragmentManager fm) {
        super(fm);
    }
}

The line "super(fm);" has a line through "super" and the hovertext says that the Fragment Manager is deprecated.

I have tried various imports. I've tried different constructors. I keep reaching an impasse, and it is clear to be that I have a fundamental misunderstanding of how Fragments and Fragment Managers work.

I went to YouTube, found a student who had made a functional Tour Guide App, and got a copy of his code to study. In his Adapter class, He has the same imports as mine, and he has essentially the same code as I have, yet his Fragment Manger is not deprecated.

His code in this case is:

package com.example.tourguideapp;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

public class ViewAdapterPage extends FragmentPagerAdapter {

    public ViewAdapterPage(FragmentManager fm)
    {
        super(fm);
    }
}

I cannot figure out what the difference is between his app and mine. Can anyone explain how deprecation errors work, and what might be going on in this case?

EDIT: THANKS FOR THE HELP! THE ANSWER IS...

...in the app build gradle dependencies! Copying mine to his broke his, and copying his to mine fixed mine. So now I get to study the dependencies more.

It feels so much better to have a clue!

  • I guess that you targeting different SDK versions than author of code that you copied. You can find them in app/build.gradle `targetSdkVersion`. – Gralls May 13 '20 at 20:39
  • `What causes Android Studio to flag the FragmentManager as deprecated?` well, the fact that it is deprecated :) this is not an error as you have mentioned but is a warning that you should not be using this class anymore, look [here](https://developer.android.com/reference/java/lang/Deprecated) – a_local_nobody May 13 '20 at 20:44
  • @Gralls I found that section, and there is a difference: his minSdkVersion is 15, mine is 22. Merely changing that number and syncing didn't make a difference to my deprecated line, so I assume there's more to it than flipping a few digits. – pain strumpet May 13 '20 at 20:52
  • @a_local_nobody Your sense of humor and mine aren't very different, I see. ;-) If it's not an error... will I be able to continue building the app with it as-is, then run it successfully? – pain strumpet May 13 '20 at 20:54
  • I would say you have to check the version of androidx library you are using and the author of the video + check when the video was taken. Google deprecated it not that long ago. – Borislav Kamenov May 13 '20 at 21:12
  • @BorislavKamenov If both files have the same import androidx.fragment.app.whatever lines, but the androidx versions are different, where would I find and how would I change the version? – pain strumpet May 13 '20 at 21:22
  • In (app) build.gradle dependencies { ... } you will be able to find the current version – Borislav Kamenov May 13 '20 at 21:24
  • @BorislavKamenov That is where the answer lies! – pain strumpet May 13 '20 at 21:50

1 Answers1

1

As a second argument to FragmentPagerAdapter constructor you have to explicitly pass the behaviour type. use super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)

Note: Take a look at JavaDocs about BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT

Indicates that only the current fragment will be in the {@link Lifecycle.State#RESUMED} * state. All other Fragments are capped at {@link Lifecycle.State#STARTED}.

Borislav Kamenov
  • 561
  • 4
  • 12
  • I did find that bit of code in another example, and when I used it in my app, the error went away. My underlying issue remains, though: I do not know why two apps that appear identical to me have different outcomes. It must be the case that I don't know what I'm looking at, and that's what I'm trying to understand. – pain strumpet May 13 '20 at 20:57