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);
}}