Reset selection in BottomNavigationView when navigate to the fragment - java

Currently in my app i have BottomNavigationView, that consists for example from 3 menu items, that linked with fragments:
Profile Fragment
Reader Fragment
Bookmarks Fragment
Also i have another fragments (Settings, Edit Profile, etc)
So when i navigate to the fragment that doesn't appear in BottomNavigationView menu i want to reset selection of menu item in BottomNavigationView.
In my code i only have NavController and this method
#Override
public void onFragmentNavigation(int id) {
navController.navigate(id);
}

Related

How to switch to the first fragment when selecting an item from the recyclerView of second Fragment? Both fragments are in tablayout with viewPager2

When I click an item from the SongList fragment, it should automatically switch to the NowPlaying fragment. Also how will I send the item position of the selected song from the recyclerView of the SongList fragment to the NowPlaying fragment? Thanks.
Access your NavController in your Fragment with getParentFragmentManager().findFragmentById(R.id.fragment_container_view) and call .navigate(R.id.now_playing).

Acivity1 Fragment2 -> Activity2 (after button click) -> Activity1 Fragment2 navigate from activity to specific fragment

Hello I have an application that implements bottom navigation component.
There is a main activity with 2 fragments (home fragment, note fragment).
I am going from note fragment to another activity called AddNoteActivity.
After I click a button in the AddNoteActivity I want the screen to show the main activity with the note fragment.
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
I understand that this code will launch the main activity but it automatically loads the home fragment because of its set as the app:startDestination in moblie_navigation.xml.
How would I get the main activity to load up the note fragment upon button click in the AddNoteActivity?
Also I have an action bar with up on navigation enabled. When I click the up arrow, I go back to the note fragment like its supposed to.
For go to the fragment just write
on your button click {
finish();
}

I have a Navigation Drawer and want open another isolated fragment

I have a navigation drawer and when i click on one button i want to go to other fragment where i can't access to the navigation drawer and anything else. I just want have a black arrow in the top-left to go back and return to the navigation drawer with all the fragments.
I tried somethings but don't works like i want normally navigator continues accessible and see bot layouts one over the other.
MAIN FRAGMENTS CONTROLLER
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
DrawerLayout drawerLayout=findViewById(R.id.drawer);
NavigationView navigationView=findViewById(R.id.nav_view);
setSupportActionBar(toolbar);
appBarConfiguration= new AppBarConfiguration.Builder(R.id.nav_home,
R.id.nav_user_data,R.id.nav_join_group,
R.id.nav_user_groups,R.id.nav_search_documents)
.setDrawerLayout(drawerLayout).build();
navController = Navigation
.findNavController(this,R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this,navController,appBarConfiguration);
NavigationUI.setupWithNavController(navigationView,navController);
}
#Override
public boolean onSupportNavigateUp() {
return NavigationUI.navigateUp(navController,appBarConfiguration)
|| super.onSupportNavigateUp();
}
I'm trying with a botton on my ActionBar, when i click it should go to my new fragment (with a generic app back but with one back button)
You simply should specify your action in navigation.xml file like others.
It will change hamburger icon by itself because you synchronize your navigation controller with NavDrawer in this two lines of code:
NavigationUI.setupActionBarWithNavController(this,navController,appBarConfiguration);
NavigationUI.setupWithNavController(navigationView,navController);
We need to add one fragment in our navigation.xml (the file where we define our fragments associated with the Fragment and the layout) for the Fragment where we want to go. Add there an action with a unique id and where we want to go in the fragment where we are.
Sorry, i know it's a little bit confused.
NAVIGATION
We go from there
<fragment
android:id="#+id/nav_user_data"
android:name="com.example.androidapplication_reto2.project.activities.navigationfragments.SeeAndModifyUserDataFragment"
android:label="User Data"
tools:layout="#layout/fragment_see_and_modify_user_data"
>
<action
android:id="#+id/action_nav_user_data_to_nav_modify_data"
app:destination="#+id/nav_modify_data" />
</fragment>`
To there
<fragment
android:id="#+id/nav_modify_data"
android:name="com.example.androidapplication_reto2.project.activities.navigationfragments.ModifyUserDataFragment"
android:label="Modify User Data"
tools:layout="#layout/fragment_modify_user_data"
/>
With the action that we defined in the origen fragment.
ORIGEN FRAGMENT
And in the Fragment when you click on one button just happen this.
Navigation.findNavController(getView()).navigate(R.id.action_nav_user_data_to_nav_modify_data);

Android: Refresh current fragment after language change

So my MainActivity has a navigation drawer with a set of fragments.
On top of the nav bar, I have 2 flags which represent a language. If an user clicks on a language, the app changes the language of the whole app.
Everything is working smoothly (activity refreshes, navigation drawer and all fragments get translated) but the fragment that's open doesn't. This means that the user needs to click on the navigation drawer and select the fragment again to see it translated.
How can I refresh the current fragment the user is in?
Add tag to your fragment when you commit it:
fragmentManager.beginTransaction().replace(R.id.your_id, fragment, "Your_Fragment_TAG").commitAllowingStateLoss();
Then You can detach and attach again your fragment for refresh:
// Reload current fragment
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();

Adding a menu to an activity than a fragment

I have a MainActivity along with a fragment. I have a refresh menu option that will refresh the data (Using AsyncTask). Is there any difference in inflating the refresh menu option in the fragment than in the MainActivity?
Note: This is in context with Udacity's Developing Android Apps, Lesson 2.
Yes, if i understand right, you want to inflate your activity toolbar menu from the the fragment. If that is the case you can do it like this
Yout Fragment class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setHasOptionsMenu is important
//it's telling the parent activity that he wants to participate in inflation of the menu
setHasOptionsMenu(true);
}
//Rest of your methods (onCreateView, onPause, onResume etc...)
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//inflate the menu file
inflater.inflate(R.menu.your_menu_xml, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(android.view.MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
//handle click
return true;
}
return super.onOptionsItemSelected(item);
}
You can also use setMenuVisibility(boolean) if you want to hide/show menu in certain childfragments
If you inflate the menu from within the activity, it will be shown in all of the fragments you load inside the activity, but the action will be available to activity only (This is good if you want to do some general stuff with your menu action like starting new activity, displaying information popup etc). If you inflate the menu from within the fragment you will be able to handle menu items from withing the fragment, which will allow you to create more specific menu actions based on which fragment is currently active. For example if you have viewpager with 3 different fragments lets say:
FragmentOne for image browsing
FragmentTwo for video browsing
FragmentThre for Text browsing
Lets say you want to allow users to upload the images only, and you want that upload button to be located in the menu.
If you inflate the the menu from within the activity your upload button will be visible inside all of your fragments, and you would have to create a custom/logic for showing hiding menu items. If you create a menu from within the fragment, you will be able to handle and show the menu for the fragment you need
Long story short i think this depends on the use case of the activity/fragment and what you want to achieve with it

Categories