1. Are you ready for the Galaxy S20? Here is everything we know so far!

Crossed out onAttach(activity) = deprecated ?

Discussion in 'Android Development' started by ac4android, Jan 22, 2016.

  1. ac4android

    ac4android Well-Known Member
    Thread Starter

    Hi, I was creating an interface (listener) between a fragment and an activity, using the onAttach() method

    AS crossed out the method name and wen I hover over it with the pointer, it says "deprecated".

    Can I still use the onAttach() method?

    I am too new to Java :(


    Attached Files:

    1. Download the Forums for Android™ app!


  2. Deleted User

    Deleted User Guest

    Unfortunately as the Android SDK evolves, things get changed. A deprecated method means that you can still use it, but warning that it may be removed from future versions of the interface, so at some point your code will break. It is a pain, but you have to either change your code, or find a way to support both versions. Here's a possible solution:

  3. This worked for me when i have userdefind interface 'TopSectionFragment' and its object activitycommander:

    //This method gets called whenever we attach fragment to the activity
    public void onAttach(Context context) {
    Activity a=getActivity();
    try {
    if(context instanceof Activity)
    }catch (ClassCastException e){
    throw new ClassCastException(a.toString());}


Share This Page