-1

I'm using the Navigation Drawer, recently I changed build.gradle to compile the app with API 23. I noticed that the method onAttach has been deprecated. I found several solutions in this forum, Android Fragment onAttach() deprecated but none of them work, have you other solutions? Thank you

   public class NavigationDrawerFragment extends Fragment implements NavigationDrawerCallbacks {
 private NavigationDrawerCallbacks mCallbacks;
    private RecyclerView mDrawerList;
    private View mFragmentContainerView;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mActionBarDrawerToggle;
    private boolean mUserLearnedDrawer;
    private boolean mFromSavedInstanceState;
    private int mCurrentSelectedPosition;
 @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
mDrawerList = (RecyclerView) view.findViewById(R.id.drawerList);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mDrawerList.setLayoutManager(layoutManager);
        mDrawerList.setHasFixedSize(true);

        final List<NavigationItem> navigationItems = getMenu();
        NavigationDrawerAdapter adapter = new NavigationDrawerAdapter(navigationItems);
        adapter.setNavigationDrawerCallbacks(this);
        mDrawerList.setAdapter(adapter);
        selectItem(mCurrentSelectedPosition);


        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUserLearnedDrawer = Boolean.valueOf(readSharedSetting(getActivity(), PREF_USER_LEARNED_DRAWER, "false"));
        if (savedInstanceState != null) {
            mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
            mFromSavedInstanceState = true;
        }
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mCallbacks = (NavigationDrawerCallbacks) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException("Activity must implement NavigationDrawerCallbacks.");
        }
    }

    public ActionBarDrawerToggle getActionBarDrawerToggle() {
        return mActionBarDrawerToggle;
    }

    public void setActionBarDrawerToggle(ActionBarDrawerToggle actionBarDrawerToggle) {
        mActionBarDrawerToggle = actionBarDrawerToggle;
    }

    public void setup(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar) {
        mFragmentContainerView = getActivity().findViewById(fragmentId);
        mDrawerLayout = drawerLayout;
        mActionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, R.string.app_name, R.string.app_name) {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                if (!isAdded()) return;
                getActivity().invalidateOptionsMenu();
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                if (!isAdded()) return;
                if (!mUserLearnedDrawer) {
                    mUserLearnedDrawer = true;
                    saveSharedSetting(getActivity(), PREF_USER_LEARNED_DRAWER, "true");
                }

                getActivity().invalidateOptionsMenu();
            }
        };

        if (!mUserLearnedDrawer && !mFromSavedInstanceState)
            mDrawerLayout.openDrawer(mFragmentContainerView);

        mDrawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mActionBarDrawerToggle.syncState();
            }
        });

        mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
    }

    public void openDrawer() {
        mDrawerLayout.openDrawer(mFragmentContainerView);
    }

    public void closeDrawer() {
        mDrawerLayout.closeDrawer(mFragmentContainerView);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = null;
    }

    public List<NavigationItem> getMenu() {
        List<NavigationItem> items = new ArrayList<NavigationItem>();
        items.add(new NavigationItem(getString(R.string.nav1), ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_home_black_24dp, null)));
        items.add(new NavigationItem(getString(R.string.nav2), ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_content_paste_black_24dp, null)));
        items.add(new NavigationItem(getString(R.string.nav3), ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_format_list_bulleted_black_24dp, null)));
        items.add(new NavigationItem(getString(R.string.nav4), ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_receipt_black_24dp, null)));
        items.add(new NavigationItem(getString(R.string.nav5), ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_trending_down_black_24dp, null)));
        items.add(new NavigationItem(getString(R.string.nav6), ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_donut_large_black_24dp, null)));
        items.add(new NavigationItem(getString(R.string.nav7), ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_equalizer_black_24dp, null)));
        items.add(new NavigationItem(getString(R.string.nav8), ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_search_black_24dp, null)));
        items.add(new NavigationItem(getString(R.string.nav9), ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_settings_black_24dp, null)));

        return items;
    }

    void selectItem(int position) {
        mCurrentSelectedPosition = position;
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mFragmentContainerView);
        }
        if (mCallbacks != null) {
            mCallbacks.onNavigationDrawerItemSelected(position);
        }
        ((NavigationDrawerAdapter) mDrawerList.getAdapter()).selectPosition(position);
    }

    public boolean isDrawerOpen() {
        return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mActionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        selectItem(position);
    }

    public DrawerLayout getDrawerLayout() {
        return mDrawerLayout;
    }

    public void setDrawerLayout(DrawerLayout drawerLayout) {
        mDrawerLayout = drawerLayout;
    }

    public static void saveSharedSetting(Context ctx, String settingName, String settingValue) {
        SharedPreferences sharedPref = ctx.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString(settingName, settingValue);
        editor.apply();
    }

    public static String readSharedSetting(Context ctx, String settingName, String defaultValue) {
        SharedPreferences sharedPref = ctx.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE);
        return sharedPref.getString(settingName, defaultValue);
    }
Community
  • 1
  • 1
user2847219
  • 555
  • 1
  • 16
  • 27
  • Since API 23 a new method `onAttach(Context)` was introduced. It's a generalization of the former `onAttach(Activity)`. Override one of them and you're done. – Eugen Pechanec Jan 03 '16 at 21:26

1 Answers1

1

Not sure what was wrong with the solution that you found. but onAttach should accept a Context. An Activity is a Context so the following should work.

@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
    mCallbacks = (NavigationDrawerCallbacks) context;
} catch (ClassCastException e) {
    throw new ClassCastException("Activity must implement NavigationDrawerCallbacks.");
}

}

Yaw Asare
  • 548
  • 4
  • 10
  • so, like the solutions of the forum, no fragment is attacked. – user2847219 Jan 03 '16 at 20:17
  • I'm not sure I understand your problem. onAttach(Activity) is a depreciated method. The solution solves the problem of the method being depreciated. My question for you is, what is the error or problem with using onAttach(Context)? – Yaw Asare Jan 03 '16 at 20:29
  • If I use using onAttach(Context) the fragment not attaccked. – user2847219 Jan 03 '16 at 20:31
  • Im guessing that what you actually mean is that the fragment is not visible. onAttach does not display your fragment it is just the method that is called when the fragment is attached to the Activity that called it. Your fragment could not be displaying for a number of reasons. it could be the layout or another error somewhere in the code. Which I cannot help with unless you show more of the code. – Yaw Asare Jan 03 '16 at 20:35
  • No error, if I use onAttach(Activity) the fragment is displayed correctly. If I use onAttach(Context), the fragment is not displayed, white screen. – user2847219 Jan 03 '16 at 20:38