I have a new project with the implementation of the navigation box fragment template and MainActivity.
It provides me with the following relevant methods:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = getIntent(); token = intent.getStringExtra(EXTRA_TOKEN); mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer); mNavigationDrawerFragment.activityMain = this; mTitle = getTitle();
My MainActivity is triggered by a burst activity that receives the saved access token through EXTRA_TOKEN.
This is an override of the Navigation Box item selector in MainAcitivity:
@Override public void onNavigationDrawerItemSelected(int position) { // update the main content by replacing fragments FragmentManager fragmentManager = getSupportFragmentManager(); onSectionAttached(position + 1); switch(position) { case 0: fragmentManager.beginTransaction() .replace(R.id.container, FeedFragment.newInstance(token, "")) .commit(); break; case 1: fragmentManager.beginTransaction() .replace(R.id.container, PeopleFragment.newInstance("", "")) .commit(); break; case 2: if(qbloggedin) { fragmentManager.beginTransaction() .replace(R.id.container, MessagesFragment.newInstance(token, "")) .commit(); } break; default: break; } }
It launches three different fragments, depending on which element is selected in NavDrawer. When creating new fragments, the token string is passed to its constructor, which is stored in the fragment class for future use.
However, when you first start the application, it seems that onNavigationDrawerItemSelected is called before onCreate ! This leads to the fact that I pass the token of zero value to the fragments, causing them to get mixed up.
How is this possible? As far as I understand, the NavigationDrawerFragment function has not yet been configured!
I set breakpoints on both onCreate and onNavigationDrawerItemSelected switch position = 0 . onNavigationDrawerItemSelected really hit before onCreate .
How can I make sure to get the token first before trying to handle onNavigationDrawerItemSelected ?
Any help would be appreciated.
java android android-intent android-fragments navigation-drawer
l3utterfly
source share