0

Recently I was tampering Navigation Drawer and although I got it worked properly, the way I code(or what happens in code) is bothering me after running into some other questions about fragment lifecycle like this: Android Fragment onCreateView vs. onActivityCreated

Shortly, I have two classes, in MainActivity I created fragment object, took fragment id on my xml and sent those to the NavigationDrawerFragment class with setUp method. In NavigationDrawerFragment, I set up listeners and show fragment on the screen when prompted.

When I log this process, I got those:

W/sarah: Fragment - OnCreate
W/sarah: Fragment - OnCreateView
W/sarah: Activity - OnCreate
W/sarah: Fragment - SETUP // I did getActivity and getActivity.findViewById //calls before onActivityCreated is called
W/sarah: Fragment - OnActivityCreated

My Questions -along with my solution idea:

1- Is this code safe? I tried it dozens of times, never crushed but I don't want to be dependent on luck.

2- Instead of using getActivity.findViewById(R.id.fragment_navigation_drawer) in Fragment class, I added one more int parameter to setUp and sent R.id.fragment_navigation_drawer along with setUp method. Does this make difference to first version? Could you explain why or why not?-especially in terms of lifecycle-

I mean, using getActivity.findViewById(fragment_navigation_drawer) in Fragment or sending R.id.fragment_navigation_drawer from MainActivity thus no need to use getActivity.findViewById in Fragment (It worked again)

Here is the relevant code pieces:
Main:

  public class MainActivity extends AppCompatActivity {

        private Toolbar toolbar; // irrelevant to question, it is needed by ActionBarDrawerToggle

            @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            toolbar =(Toolbar)findViewById(R.id.app_bar)
            setSupportActionBar(toolbar);
            getSupportActionBar().setHomeButtonEnabled(true);
            Log.w("sarah", "Activity - OnCreate");
            NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
            //fragment_navigation_drawer is in activity_main xml.
            drawerFragment.setUp((DrawerLayout) findViewById(R.id.drawer_layout), toolbar); // 
        }
    ...
    }

Fragment:

public class NavigationDrawerFragment extends Fragment {
    private ActionBarDrawerToggle mDrawerToggle;
    private DrawerLayout mDrawerLayoout;

    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        Log.w("sarah", "Fragment - OnCreate");
    }
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.w("sarah", "Fragment - OnActivityCreated");
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Log.w("sarah", "Fragment - OnCreateView");
        return inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
    }

    public void setUp(DrawerLayout drawerLayout, Toolbar toolbar) {
        Log.w("sarah", "Fragment - SETUP");
        mDrawerLayoout = drawerLayout;
        mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) { //getActivity call
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);

                getActivity().invalidateOptionsMenu(); 
            }
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);

                getActivity().invalidateOptionsMenu();
            }
        };
             if(!mUserLearnedDrawer && !mFromSavedInstanceState ) //checks if the user never seen the drawer and first time fragment started. some shared pref calcs. not relevant.
                //getActivity.findViewById call here
                mDrawerLayoout.openDrawer(getActivity().findViewById(R.id.fragment_navigation_drawer));  //getActivity call

                mDrawerLayoout.setDrawerListener(mDrawerToggle);
}}
Community
  • 1
  • 1
sarah
  • 580
  • 1
  • 6
  • 24

1 Answers1

1
  1. Yes this code is safe and this link explains why http://developer.android.com/guide/components/fragments.html#Creating. The only way for your activity to continue after setContentView is for the fragment to be created and drawn since if I understand the question your NavigationDrawerFragment is being instantiated in the activity_main.xml file.

  2. Technically speaking you don't really need to do the setup method within the activity. The drawer can run its own setup method in onViewCreated because whenever the fragment is instantiated you can assume that the controlling activity has instantiated the toolbar/drawerlayout. To the question about whether it makes a difference whether you get the toolbar/drawerlayout from using getActivity() vs passing it into the method parameters, it shouldn't matter assuming your toolbar/drawerlayout will have a consistent id. If your toolbar/drawerlayout will always be instantiated with the same id then using getActivity() method is fine, if you plan to have different toolbars/drawerlayouts on different activities it might make more sense to have the activity pass in those objects as parameters.

bestdayever
  • 154
  • 6