Sunday, May 1, 2016

[Android][Resolved] overrides deprecated method in android.app.fragment


Project Information :

compileSdkVersion 23
buildToolsVersion "23.0.2"
minSdkVersion 19
targetSdkVersion 23

Problem code:

 package com.example.fragmentandroid;

import android.app.Activity;
import android.app.ListFragment;
import android.content.Context;

public class HeadlinesFragment extends ListFragment{
    OnHeadlineSelectedListener mCallback;

    // Container Activity must implement this interface
    public interface OnHeadlineSelectedListener {
        public void onArticleSelected(int position);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

}

The example HeadlinesFragment class extends class ListFragment, and  class ListFragment extends Fragment class, so let have a look of Fragment doc:
http://developer.android.com/reference/android/support/v4/app/Fragment.html#onAttach(android.app.Activity)



 From the page you could found onAttach(Activity activity) is deprecated and need to use method onAttach(Context context) intend.

Old method which's deprecated :

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

New method

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }
You can keep using activity (type Activity) as the the argument to onAttach method since Type Activity is the subclass of class Context.

Reference:
http://developer.android.com/training/basics/fragments/communicating.html
http://stackoverflow.com/questions/32083053/android-fragment-onattach-deprecated
http://developer.android.com/reference/android/support/v4/app/Fragment.html#onAttach(android.app.Activity)

No comments :

Post a Comment