10

I have read a few articles and answers (including this one) regarding deprecated code, but I'm a bit confused as to how to handle (specifically) the deprecated Fragment event handler onInflate.

I have replaced my implementation of

public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState)

with

public void onInflate(Context context, AttributeSet attrs, Bundle savedInstanceState)

If I run my app on a < API23 machine, the replacement code is not called.

If I restore the original deprecated code (so that I now have both methods implemented) then the deprecated code is called, correct functionality returns, but a call is now being made to a deprecated method(?).

And when I run the app on an API23 machine, it appears that BOTH versions of the handler are called.

So the question is, what is happening here? If I am writing code which is supposed to run on both API23 and earlier versions, do I need to implement the deprecated methods as well as the new ones?

And if that's the case, do I need to hunt out and implement other deprecated methods "just in case"? (and, therefore, is there a list of these deprecated methods to "back-code" for?)


UPDATE :

I have now changed from using android.app.Fragment to android.support.v4.app.Fragment (i.e. from native fragments to support fragments) and the app is now performing as expected, with the replacement handler code running for all versions, and is stepping through the android source as expected.

But the question remains : WHY?

Why is the 'native' android.app.Fragment implementation ****ed up? Looking back through earlier questions then this issue was being discussed back in September 2015. So why is it still an issue? And why should there be a difference in the implementation of support and native fragments post API 11?

1
  • Thanks for the edit of depreciated to deprecated. I had never realised I was so dyslexic! :) Commented Mar 21, 2016 at 17:24

1 Answer 1

4

No, you should not implement

public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState)

When a method is deprecated, you can (generally) safely use the replacement function instead. Check Fragment's definitions of onInflate:

public void onInflate(Context context, AttributeSet attrs, Bundle savedInstanceState) {
        mCalled = true;
        final Activity hostActivity = mHost == null ? null : mHost.getActivity();
        if (hostActivity != null) {
            mCalled = false;
            onInflate(hostActivity, attrs, savedInstanceState);
        }
    }

 @Deprecated
    public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) {
        mCalled = true;
    }

As you can see, onInflate(Context context...) is an extension of onInflate(Activity activity...) and is backward compatible. By calling super.onInflate(context, attrs, savedInstanceState); inside your overridden method, you can safely assume that it will work on both API23 and earlier versions.

If I run my app on a < API23 machine, the replacement code is not called.

That seems strange, I can't reproduce it, my log is being executed with a code like this in a non-v23 machine:

@Override 
public void onInflate(Context context, AttributeSet attrs,
                                    Bundle savedInstanceState) {
        super.onInflate(context, attrs, savedInstanceState);
        Log.w(TAG, "I'm being executed");
    }

Make sure that:

  1. You're importing the support version of Fragment, android.support.v4.app.Fragment instead of android.app.Fragment and that you make use of getSupportFragmentManager() instead of getFragmentManager().
  2. You have the latest libraries of support-v4 and appcompat-v7 as well as compileSdkVersion 23 in your build.gradle

And when I run the app on an API23 machine, it appears that BOTH versions of the handler are called.

That can be explained by the onInflate(Context context...) code, where if it's called by an Activity, calls onInflate(Activity activity...)

EDIT

Regarding the update:

Native implementation of android.app.Fragment isn't supposed to be backward compatible. The code of android.app.Fragmentis generally much more simpler than that of android.support.v4.app.Fragment because of that fact.

In addition, there are some features in native implementation that cannot be introduced in support library either because it's hard by design or because it's not a priority for the developers. E.g. In native implementation, in the onInflate() source code there is some use of Transition to animate the entrance or exit of the new fragment. That is nowhere to be found in the support library.

4
  • Thanks for taking the trouble to answer. I can see why both versions of the handler are called for v23. I'm still getting no call to the replacement handler for versions pre-v23 though. Oddly I AM getting a log message "I/dalvikvm: Could not find method android.app.Fragment.onInflate, referenced from method OdometerFragment.onInflate" which seems odd. That and the fact that stepping through android Fragment code is showing the wrong source lines leads me to believe that I don't have the latest version of something. Commented Mar 21, 2016 at 21:20
  • 1
    Make sure that: 1) you're importing the support version of Fragment, android.support.v4.app.Fragment instead of android.app.Fragment and that you make use of getSupportFragmentManager() instead of getFragmentManager(). 2) that you have the latest libraries of support-v4 and appcompat-v7 as well as compileSdkVersion 23 in your build.gradle.
    – Sevle
    Commented Mar 22, 2016 at 0:28
  • Thanks for the further tip. I tried changing to 'support' fragments last night and it solved the problem for me. I have updated the question accordingly. I'm still rather perplexed as to why the native and support implementation are different though. Seems like a screw up somewhere to me. Yes - my specific problem is solved but app.android.Fragment isn't Commented Mar 22, 2016 at 8:20
  • 1
    Even though your edit question is an extension of the original, and may deserve a new post by itself, I've made an edit with some points that may answer your question. You might also want to check this.
    – Sevle
    Commented Mar 22, 2016 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.