I have setup a new project with the template implementation of Navigation Drawer Fragment and a 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();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
My MainActivity is started by a splash activity which gets a saved access token via the EXTRA_TOKEN.
This is the override of the Navigation Drawer item select listener in the 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 starts three different fragments depending on which item is selected in the NavDrawer. While instantiating the new fragments, the token string is passed into its constructor, which is saved in the fragment's class for further use.
On the first start of the App however, it seems that onNavigationDrawerItemSelected is called before onCreate! This results me passing a null value token into the fragments, causing them to be all messed up.
How is this possible? As I understand it, the NavigationDrawerFragment should not have been setup yet!
I set breakpoints on both onCreate and on onNavigationDrawerItemSelected switch position = 0. onNavigationDrawerItemSelected is indeed hit before onCreate.
How can I make sure to get the token first before trying to handle the onNavigationDrawerItemSelected?
Any help would be appreciated.
I believe I figured this out as it was happening to me for anyone who searches this and can't find the answer.
If you use the Android Studio DrawerActivity then there is boilerplate code that they create for you. In this code in the activity_main.xml or whichever XML your DrawerActivity sets as its' content view, there is a tag.
When setContentView() is called in onCreate(), this fragment is automatically created and so technically onCreate() is still being called first but then the onNavigationDrawerItemSelected() method is called before anything else in create. Since setContentView is typically kept up top, this causes problems when trying to store the state of the fragments in your drawer.
Simply move any code that checks for savedInstanceBundle above setContentView() and it will fix the problem.
Example with comments:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// THIS IS WHERE YOU CHECK FOR SAVED INSTANCE
// Check for frag
if (savedInstanceState != null) {
Log.i(TAG, "Get QuestionDayFragment");
mQuestionDaysFragment = (QuestionDaysFragment) getSupportFragmentManager().getFragment(savedInstanceState, QUESTION_DAY_FRAGMENT);
}
// View injection
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
// THIS IS WHERE THE CODE WAS BEFORE
// THIS WOULD BE CALLED AFTER onNavigationDrawerItemSelected()
// Singleton injection
LifeboxApplication.graph().inject(this);
// Toolbar
setSupportActionBar(mToolbar);
// FB
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
// Drawer
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
}
You could move the intent to a constructor and save your tokens there like so:
Intent i;
......
public FragmentConstructor() {
i = getIntent();
token = intent.getStringExtra(EXTRA_TOKEN);
}
What I had to do to make it work was to check if the page has loaded before executing onNavigationDrawerItemSelected
private Boolean loaded=false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Your code here
this.loaded=true;
}
public void onNavigationDrawerItemSelected(int position) {
if (!this.loaded){
return;
}
I also agree with using a boolean to check if onCreate() has finished loading. My only other suggestions is that for a quick fix you can use onSectionAttached(int number) to process each item selected instead of onNavigationDrawerItemSelected.
Related
Actionbar.removealltabs removes all my tabs. However, the remaining space used to hold the tabs are still there. As a result my actionbar looks like a very tall actionbar with a empty bottom half.
Using support actionbar with appcompatactivity.
I've tried calling Actionbar.removealltabs in both my fragment and Mainactivity... same result.
Tabs pre-destruction
Tabs Destroyed, leaving behind it's empty space
How it originally looked before adding tabs. This is my desired result
Here's my Fragment that created the tabs.
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mViewPager = view.findViewById(R.id.reddit_search_pager);
mActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
setViewPager();
setTabs();
}
public void setTabs() {
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabSelected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
}
};
for (int i = 0; i < 3; i++) {
String tabTitle = "";
switch (i) {
case 0:
tabTitle = TAB_TITLE_SUBREDDITS;
break;
case 1:
tabTitle = TAB_TITLE_POSTS;
break;
case 2:
tabTitle = TAB_TITLE_USERS;
break;
}
mActionBar.addTab(
mActionBar.newTab()
.setText(tabTitle)
.setTabListener(tabListener));
}
}
Here's my primary MainActivity pre-existing modification's to the toolbar.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NetworkingUtils myClass = new NetworkingUtils(this);
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_action_menu);
When I am working with ListViews I usually use adapters. With adapters you have to call yourAdapter.notifyDataSetChaged() every time you make a new change on your list so the list can update normally. Maybe that is what is happening to you so try to call some method like the one I showed above and it will maybe update and clear the empty space.
I fixed it by using another way to use tabs, the Tablayout as described in this video. https://www.youtube.com/watch?v=zQekzaAgIlQ. I had to make my own Toolbar instead of the default theme one.
I have no idea why Android is suggesting a deprecated and buggy way to implement tabs on their official website instead of this. In fact, the TabLayout examples provided in the Youtube video are all 404 not found.
So i've encountered a small problem today. I was making a bottom navigation view in my app, and after clicking buttons, it replaces the fragment on the screen (and it works perfectly!).
But just after launching the app, and without clicking any button, there is no fragment on the screen.
I've realized that the fragments are shown only after clicking a button, and I'd like to have a default fragment (kalkulatorFragment).
I've been trying my best to somehow set it up, but no success...
public class Main extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
kalkulatorFragment kalkulator_fragment = new kalkulatorFragment();
wzoryFragment wzory_fragment = new wzoryFragment();
definicjeFragment definicje_fragment = new definicjeFragment();
switch (item.getItemId()) {
case R.id.kalkulator:
ft.replace(android.R.id.content, kalkulator_fragment);
ft.commit();
return true;
case R.id.wzory:
ft.replace(android.R.id.content, wzory_fragment);
ft.commit();
return true;
case R.id.definicje:
ft.replace(android.R.id.content, definicje_fragment);
ft.commit();
return true;
}
return false;
}
Ok i just figured it out.
I moved the ft.replace to the onCreate() method, so the kalkulatorFragment is going to be shown just after creating an Activity.
public class Main extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
kalkulatorFragment kalkulator_fragment = new kalkulatorFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(android.R.id.content, kalkulator_fragment);
ft.commit();
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
You need to use this code OUTSIDE of OnCreate Method:
navigation.setSelectedItemId(R.id.IdOFYourItemFromBottomNavigationMenuItems);
I don't know why, but it wont work inside OnCreate method. You can declare and initialize it inside OnCreate method, just can't set the default item in there.
In my case I am using it inside OnCreateOptionsMenu.
In my app, am using a navigation drawer (in Fragment A) to navigate to fragments:
public void displayView(int viewId){
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (viewId) {
case R.id.nav_menu:
fragment = new MenuFragment();
title = getString(R.string.menu_title);
viewIsAtHome = false;
break;
case R.id.nav_reservation:
fragment = new ReservationFragment();
title = getString(R.string.reservation_title);
viewIsAtHome = false;
break;
...
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
// set the toolbar title
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(title);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
}
displayView() is called in onNavigationItemSelected:
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
displayView(item.getItemId());
return true;
}
Now, in ReservationFragment I am displaying a list of reservations and a FloatingActionButton to start Activity B where the user can add a reservation if there are no reservations. When the user is done adding a reservation, I want to display it in the Reservation fragment. This requires me to "go back" to the Fragment.How do I accomplish this since Activity B knows nothing about Activity A?
What I've tried:
I tried creating a method in Activity A like this:
public void navigateToFragment(int viewId) {
displayView(R.id.nav_reservation);
}
and then called this method from Activity B:
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new MainActivity().navigateToFragment(R.id.nav_reservation);
//MainActivity is Activity A
}
});
the app crashes due to a nullPointerException in displayView() from the line:
String title = getString(R.string.app_name);
This isn't surprising since am creating a new MainActivity object that knows nothing about the previous state of the Activity, right?
This question mirrors my problem but is based on Settings so I can't really use the answer in my case.
How do I accomplish this task?
There are three quick methods that come to my mind. First you can start activityB with startActivityForResult and handle the result in activityA after user does what he wants in activityB. Second you can set activityA as singleTop and before finishing activityB you can startActivityA with clearTop an intent flag called clear_top(https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP).
Last but not the least, you can connect two activity by binding service in both activities and communicate via that service that you bound.
I am new to android dev, I created a new project with NavigationDrawer activity, I use Android Studio. The problem is when I add a button and create OnClickListener, the app crushes, but without it, it launches fine. Please look at my code below.
I tried adding setContentView(View) but doesn't help
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //comes by default
setContentView(R.layout.fragment_main); //added by me, but doesnt help
//referencing my button
btnTest = (Button)findViewById(R.id.btnTest);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
//my event listeners
//when i highlight the below code everythin works..these block cause the crash
btnTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
I know others have had these issue and solved it, but I'm not able to solve mine, please help, Thank You
setContentView(R.layout.activity_main); //comes by default
setContentView(R.layout.fragment_main); //added by me, but doesnt help
Don't call setContentView twice. The one that "comes by default" is provided for your convenience when the IDE is generating the Activity class, if you don't need it, delete it.
Secondly, you are setting a fragment layout as View for your Activity. So unless R.id.btnTest is included in that layout, btnTest will be null, hence causing a NullPointerException when calling setOnClickListener.
delete this :
setContentView(R.layout.fragment_main);
and here
setContentView(R.layout.activity_main);
make sure you have a layout file for your Activity that is named activity_main.xml or replace that reference with the name of your layout file.
EDIT : I'm guessing you chose the option in AS to generate a placeholder in your Activity and a Fragment to be added to it. You need to handle the button in the Fragment class, and more importantly, you need to add the Fragment to your Activity.
In your Activity (in OnCreate, after the setup of the nav drawer) :
Fragment newFragment = new ExampleFragment(); // replace ExampleFragment by your Fragment's class name
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(CONTENT_VIEW_ID, newFragment).commit(); // CONTENT_VIEW_ID is the id of the View in your Activity that should contain the Fragment.
And then in your Fragment, move this to onActivityCreated :
btnTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
You will need to inflate R.layout.fragment_main in onCreateView in your Fragment, and get a reference to btnTest from the View you inflate.
Without Logcat I would say that your problem is one of the following:
btnTest is not defined in your ActivityLayout (activity_main). If this is the case check your XML.
Is your btnTest inside your Fragment? If so you should put the OnClickListener in your Fragment class instead of your Activity class.
BTW, the second setContentView doesnt make any sense you should use only one.
Hope it helps
I'm kinda new into android programming, and I couldn't find the answer on the internet.
I have set up a Navigation drawer in Android Studio, and I want to change the content view if that section is selected, for example, if I select a view "Tools" in the navigation drawer, I want that you can see the layout "Tools" but that you also can go back to the navigation drawer. I have tried to put setContentView into the case 1, etc but that gave an error.
I have tried this code for the setContentView:
public class MainActivity extends Activity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
.commit();
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
//This is where I thought I could implement the setContentView
setContentView(R.layout.activity_tools);
mTitle = "Tools";
break;
case 2:
mTitle = "Weapons";
break;
case 3:
mTitle = "Mobs";
break;
case 4:
mTitle = "Food";
break;
case 5:
mTitle = "Blocks";
break;
}
}
ContentView is not what you want to change on drawer item selection. ContentView sets your activity layout (the one which holds NavigationDrawer view). What you really want is to replace fragments.Lets say you have a different fragment with its own layout for every item in the navigation menu. In this case you could implement something like:
public void onNavigationDrawerItemSelected(int position) {
Fragment fragment;
Bundle args = new Bundle();
String tag;
switch (position) {
case 1:
fragment = new FragmentTools();
tag = "Tools";
args.putString(KEY_FRAGMENT_TITLE, tag);
break;
case 2:
fragment = new FragmentWeapons();
tag = "Weapons";
args.putString(KEY_FRAGMENT_TITLE, tag);
break;
case 3:
...
}
FragmentTransaction transaction = getFragmentManager().beginTransaction();
fragment.setArguments(args);
transaction.replace(R.id.fragment_container, fragment, tag);
transaction.commit();
}
The above is just an example. You select which fragment to show and set its arguments. Of cource it could be the same fragment with different layouts. In that case you could pass layout id as an argument, or select appropriate layout in Fragment itself based on its title.