I'm having the problem that when I trying to switch between the menu item. The menu item will not pointing to the correct icon.
here is the flow when i found this problem
starting at the (Home)fragment,then press on the second menu item (Features )
case R.id.nav_home:
//home fragment transaction
actionBar.setTitle("Home");
HomeFragment fragment1 = new HomeFragment();
FragmentTransaction fragmentTransaction1 = getSupportFragmentManager().beginTransaction();
fragmentTransaction1.replace(R.id.content, fragment1, "");
fragmentTransaction1.commit();
break;
second menu item (features) will go to features activity
case R.id.nav_features:
//features fragment transaction
startActivity(new Intent(DashboardActivity.this, FeaturesActivity.class));
break;
close features activity and back to (Home) fragment
onBackPressed();
bottom navigation still pointing to second menu item (features).
how can I make the system point to the correct menu item ?
You should return a boolean for this method:
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
//...
}
do this :
case R.id.nav_home:
//home fragment transaction
actionBar.setTitle("Home");
HomeFragment fragment1 = new HomeFragment();
FragmentTransaction fragmentTransaction1 =
getSupportFragmentManager().beginTransaction();
fragmentTransaction1.replace(R.id.content, fragment1, "");
fragmentTransaction1.commit();
return true; // add this line and remove break;
if you don't want to select icon after click, you can return false.
Related
This question already has an answer here:
Navigation View Global Action Back State
(1 answer)
Closed 1 year ago.
I have a search button, with an actionViewClass. When this button is clicked a new fragment opens, and I want when the back button is clicked to go back to the previous fragment.
PS: I am using navigation graph
My toolbar_menu.xml
<item
android:id="#+id/search"
android:title="Pesquisar"
android:orderInCategory="103"
android:icon="#drawable/ic_search"
android:iconTint="#color/colorPrimary"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="androidx.appcompat.widget.SearchView"/>
MainActivity.java
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.search:
getSupportFragmentManager().beginTransaction()
.replace(R.id.navHostFragment, new ContaFragment())
.commit();
break;
}
return false;
}
});
try adding the fragment to backstack
private void replaceFragment (Fragment fragment){
String backStateName = fragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
boolean fragmentPopped = manager.popBackStackImmediate (backStateName, 0);
if (!fragmentPopped){ //fragment not in back stack, create it.
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.navHostFragment, fragment);
ft.addToBackStack(backStateName);
ft.commit();
}
}
this question has a good answer and explanation here How to resume Fragment from BackStack if exists
So I have no idea how to do this and even where to start, so if someone would be patient enough to explain how to do this I would be appreciated.
I already looked up on google but what I found was too difficult to understand ( and I am pretty sure that it can be done way more easily.
I have 4 fragments (4 tabs on a bottom navigation bar) and I want to have only 1 running at the time.
Now, I am pretty sure that I should use FragmentManager but I don't really know how to start to implement in my program.
Do I have to write all the FragmentTransaction code in the mainActivity? and after that how to keep goin? Is there a example code by any chance?
Disclaimer: I am a total noob in the android studio environment and I am also a student and I almost do everything as a school project.
You Can try this in your code. On the onNavigationItemSelected
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = new HomeFragment();
break;
case R.id.navigation_dashboard:
fragment = new DashboardFragment();
break;
case R.id.navigation_notifications:
fragment = new NotificationsFragment();
break;
case R.id.navigation_profile:
fragment = new ProfileFragment();
break;
}
return loadFragment(fragment);
}
private boolean loadFragment(Fragment fragment) {
//switching fragment
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
return true;
}
return false;
}
I hope this can help You!
Thank You.
I'm making an app that has launches with a Swiping Tabs Fragment, but I also want to have a Preferences menu using Fragment Transaction. When I press the Settings button in my app, the preferences menu does appear and is interactive, but the layout seems to be transparent showing the Tabs below. Is there anything that can be done to fix this, or is there a better method?
Code that calls the Preferences Menu from the Options:
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
getFragmentManager().popBackStackImmediate();
break;
case R.id.logout:
FirebaseAuth.getInstance().signOut();
finish();
startActivity(new Intent(this, Activity_Main.class));
break;
case R.id.action_settings:
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new Fragment_Settings()).addToBackStack(null)
.commit();
break;
}
return true;
}
Code that calls Tabs Fragments:
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public android.support.v4.app.Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new Fragment_Flower();
case 1:
// Games fragment activity
return new Fragment_Overview();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 2;
}
}
Initialising Transaction in onCreate:
startService(new Intent(this, SensorListener.class));
if (b == null) {
// Create new fragment and transaction
Fragment newFragment = new Fragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
//Replace whatever is in the fragment_container view with this
// fragment,
// and add the transaction to the back stack
transaction.replace(android.R.id.content, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
Any help will be greatly appreciated!
Just set background color in your layout file. It may avoid transparent backgound.
I have looked at many of the similar question But still I'm unable to replace container layout (FrameLayout) of My Main Activity from a fragment that is visible in that container.
Here is what I'm trying to do I have a main activity with navigation drawer and On navigation drawer item click I'm changing fragment accordingly, I'm successful till this. These fragments are Login, Register, Categories, Contact Us etc. I'm changing/replacing fragments with FragmentManager, FragmentTransaction its method replace. Check the code below
public void changeFragment(int position) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Fragment fragment = new Fragment();
BlankFragment blankFragment = new BlankFragment();
switch (position) {
case 0:
fragment = new FragMain();
break;
case 1:
fragment = new FragPrivacyPolicy();
break;
case 2:
fragment = new SettingsFragment();
break;
case 101:
fragment = new FragLogin();
break;
case 102:
fragment = new FragRegister();
break;
case 103:
fragment = new FragEditAccount();
break;
case 104:
fragment = new FragOrderHistory();
break;
case 105:
fragment = new FragForgotPass();
break;
case 106:
fragment = new FragOrderDetail();
break;
case 108:
fragment = new FragAboutUs();
break;
case 109:
fragment = new FragContactUs();
break;
case 110:
fragment = new FragMain();
break;
}
transaction.replace(R.id.flFragments, fragment);
transaction.commit();
}
Which is working fine as all my fragments extends Fragment. The problem comes when I try to call this method in one of the fragments visible in this mainActivity's container. Suppose I have a textView in FragLogin saying "Don't have an account Register" so on click this textView I have to open FragRegister in the same container of mainActivity. When I do it like this in my FragLogin App Crashes pointing to the transaction.commit();
MainActivity activity = new MainActivity();
activity.changeFragment(102);
And when I try like this
FragRegister fragRegister = new FragRegister();
// Fragment fragRegister = new FragRegister();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.Id.flFragments, fragRegister).commit();
It does not recognize the R.Id.flFragments as its inside the mainActivity not in FragLogin.
So how can I replace that FragRegister in mainActivity's container from FragLogin?
Dont use getChildFragmentManager() because it belongs to the fragment manager and the container that is holding current fragment is the activity container.Instead use getSupportFragmentManager().
Also see this stament
MainActivity activity = new MainActivity();
activity.changeFragment(102);
You are creating a new object of mainactivity which is already created.You have to call the activity method from fragments using getActivity().Call the changeFragment method like this.
((MainActivity)getActivity()).changeFragment(102);
I have a app with BottomNavigation (3 Items). The Item 1 load Fragment 1, Item 2 load fragment 2 and Item 3 load fragment 3. When one item is selected in the BottomNavigation, the view of this item keep blue and the text is more big. I have implemented onBackPressed, for it back the fragment history (BackStack). But, when i'm backing, the views in BottomNavigation are stoppe. So, if i'm in fragment 3, and i press back button and it back to fragment 2, the BottomNavigationView show that i'm in fragment 3, and if i press back button again, i go to fragment1, but the BottomNavigationView don't update, it keep in Fragment 3. The screenshorts will show what i'm talking about.
Sorry my bad english and my bad explanation.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
selectedFragment = Fragment1.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).addToBackStack(null).commit();
return true;
case R.id.navigation_dashboard:
selectedFragment = Frament2.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).addToBackStack(null).commit();
return true;
case R.id.navigation_notifications:
selectedFragment = Fragment3.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).addToBackStack(null).commit();
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
From the Material Design guidelines for bottom navigation:
On Android, the Back button does not navigate between bottom navigation bar views.
And from the Developer Training for Back Navigation:
Note: You should not add transactions to the back stack when the transaction is for horizontal navigation (such as when switching tabs)
So you should not use the back stack when replacing fragments based on user interaction with your BottomNavigationView.
If you choose to ignore these guidelines, probably the answer is to look into FragmentManager.OnBackStackChangedListener and activate the appropriate navigation item when you pop the back stack.