fragment bottom navigation bar clickable one time selected item - java

When I click again to any item, same fragment view reloading every time. But I wanna do bottom navigation bar item one time clickable. how can i do this?
This is MainActivity.java codes
navigationMenu=findViewById(R.id.navigation);
navigationMenu.setOnNavigationItemSelectedListener(navListener);
//navigationMenu.getMenu().getItem(2).setIcon(R.drawable.arrow_down);
//I added this if statement to keep the selected fragment when rotating the device
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new UserHomeFragment()).commit();
}
This is click listener codes
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
selectedFragment = new UserHomeFragment();
break;
case R.id.navigation_works:
selectedFragment = new UserWorksFragment();
break;
case R.id.navigation_completed_works:
selectedFragment = new UserCompletedFragment();
break;
case R.id.navigation_profile:
selectedFragment = new UserAccountFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
};

Related

How to save the data on a fragment while switching between them

I have a app with a main activity and fragments. My fragments work but when I switch between them, they refresh and create a new fragment when I switch back. I want the data to save when I switch back and forth, but I can't figure out how to use a bundle to save the values.
This is the code that controls switching between fragments:
`private NavigationBarView.OnItemSelectedListener navListener =
new BottomNavigationView.OnItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;//used to default to home
if(item.getItemId() == R.id.nav_home){
selectedFragment = new HomeFragment();
}else if(item.getItemId() == R.id.nav_input){
//selectedFragment = new InputFragment();
if((InputFragment) getSupportFragmentManager().findFragmentByTag("input_tag") != null){
selectedFragment = (InputFragment) getSupportFragmentManager().findFragmentByTag("input_tag");
}else{
selectedFragment = new InputFragment();
}
}else if(item.getItemId() == R.id.nav_balance){
selectedFragment = new BalanceFragment();
}else if(item.getItemId() == R.id.nav_calendar) {
selectedFragment = new CalendarFragment();
}
if(selectedFragment == null) selectedFragment = new HomeFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment, "input_tag").commit();
return true;
}
};
}`
I tried reformatting it, and looked through Android Studio forums, but I can't seem to find anything helpful to resolving this problem. I unsuccessfully tried to use tags to save a fragment, but it didn't retain the data. I also tried to use savedInstanceState but it did not work. Any help or directions to start in would be greatly appreciated.

How can I add back to previous page on webview fragments?

I have an android app created with android studio that has a main activity and fragments with webview. Each fragment contains webview code with a link to a website. The problem that I have is that when I press back it closes the app. I want to use back to go to the previous page and the previous activity.
Main Activity code:
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.nav_facebook:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new FacebookFragment()).commit();
break;
case R.id.nav_twitter:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new TwitterFragment()).commit();
break;
case R.id.nav_instagram:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new InstagramFragment()).commit();
break;
case R.id.nav_linkedin:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new LinkedinFragment()).commit();
break;
case R.id.nav_snapchat:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new SnapchatFragment()).commit();
break;
case R.id.nav_pinterest:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new PinterestFragment()).commit();
break;
case R.id.nav_youtube:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new YoutubeFragment()).commit();
break;
case R.id.nav_about:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new AboutFragment()).commit();
break;
case R.id.nav_privacy:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new PrivacyFragment()).commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
Here is the code of one of the fragment's code (the rest of the fragments has similar code):
public class FacebookFragment extends Fragment {
public FacebookFragment() {
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_facebook, container, false);
WebView webView = (WebView)v.findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://www.facebook.com/");
return v;
}
}
After replacing the fragment just make sure to add it to back stack using:
replace(R.id.fragment_container,
new YoutubeFragment()).addToBackStack("tag").commit()
and override the onBackPressed function to pop up stack while it's not empty.
All these fragments have same function is go back fragment. Why don't add the base fragment for them and the code will be easier.

How to fix bottom navigation bar not showing correctly

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.

How to get last Fragment used when pressing back button

I have a simple fragment with this code:
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
Fragment selectedFragment= null;
switch (menuItem.getItemId()){
case R.id.nav_home:
selectedFragment= new HomeFragment();
setTitle("Beranda");
break;
case R.id.nav_message:
selectedFragment= new MessageFragment();
setTitle("Pesan");
break;
case R.id.nav_transaction:
selectedFragment= new TransactionFragment();
setTitle("Transaksi");
break;
case R.id.nav_profile:
selectedFragment= new ProfileFragment();
setTitle("Profil");
if(sessionLevel.equals("admin")){
setTitle("Admin");
}
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();
return true;
}
};
Most of the fragment are just some kind of holder for Intent Activity. And the Activity itself doesnt have some fancy code.
The problem is that when i do Intent on Profile menu and then press back, the fragment shown is HomeActivity but the selected button is Profile.
I dont know about the other 2 fragment since im not there yet, but probably they do the same thing.
You can do this like the following:
#Override
public void onBackPressed() {
super.onBackPressed();
Fragment frag = YourActivity.this.getSupportFragmentManager().findFragmentById(R.id.fragment_container);
if(frag!=null && frag instanceof yourFragment)
{}
}
The following will give you count
int count = getSupportFragmentManager().getBackStackEntryCount();
You can make condition, If count is great than 1 then have more than 1 fragments else you have atleast 1 fragment and that will be last fragment. If count 0 then no more fragment available on stack as all have been poped out of stack.
Create an interface:
public interface IOnBackPressed {
boolean onBackPressed();
}
In Fragment implement IOnBackPressed like:
public class FAQFragment extends Fragment implements IOnBackPressed {
public boolean onBackPressed() {
return false
}
}
On Main Activity Backpress use
#Override
public void onBackPressed(){
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
}
}

NullPointerException when using BottomNavigationView

// I learned android studio about BottomNavigationView, but I have a problem like this ... please help me :(
// Caused by: java.lang.NullPointerException: Attempt to invoke
virtual method 'void
android.support.design.widget.BottomNavigationView.setOnNavigationItemSelectedListener(android.support.design.widget.BottomNavigationView$OnNavigationItemSelectedListener)'
on a null object reference// eror logcat
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()) {
case R.id.nav_home:
selectedFragment = new HomeFragment();
break;
case R.id.nav_search:
selectedFragment = new SearchFragment();
break;
case R.id.nav_filter:
selectedFragment = new PesananFragment();
break;
case R.id.nav_notifikasi:
selectedFragment = new NotificationFragment();
break;
case R.id.nav_akun:
selectedFragment = new AccountFragment();
break;
}
assert selectedFragment != null;
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();
return true;
}
};
}
It looks like a problem with the initial bottomNav stationing.
Maybe try to put explicit casting like:
BottomNavigationView bottomNav=(BottomNavigationView)findViewById(R.id.bottom_navigation);
If that does not work check the BottomNavigationView implementation requirements.
You can use android studio to create a activity that already has a Bottom navigation.

Categories