Search code examples
javaandroidandroid-activitydeprecatedandroid-fragmentactivity

How to resolve this deprected problem in OnActivityCreated?


I am getting 'onActivityCreated(android.os.bundle)' is deprecated.

package example.com.fragmentrecycler;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class ListFrag extends Fragment {
    RecyclerView recyclerView;
    RecyclerView.Adapter myAdapter;
    RecyclerView.LayoutManager layoutManager;
    View view;

    public ListFrag() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_list, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        
    }

    public void notifyDataChanged()
    {
        myAdapter.notifyDataSetChanged();
    }

}

enter image description here

This is that java file in which i am getting this error. This code is part of full project.I am not getting what wrong in my code.


Solution

  • onActivityCreated() is deprecated in API level 28.

    There is no error shown because no error exists. Deprecated means that a newer or better method exists to handle stuff. So you need to change onActivityCreated() to onCreate(). But as I see you don't need to call this a second time if you already have a Fragment with onCreateView(). Keep your project clear and make a new .java file instead. Don't code everything in one single file.

    • onCreate() is for Activity
    • onCreateView() is for Fragment