Call API only once with Bottom Navigatioin Fragments - android

I am having a bottom navigation fragment and want to call API only once, which means when the user jumps to another tab and comes back API should not be getting called. And when the user comebacks from the background state, API should be called once. I tried calling API with the onCreate method, but oncreate method calling every time when I click on the tab. On the other side, for one fragment I am sending data from MainActivity by calling the API.
Please check the code and shed some light on where am doing wrong.
studentBottomNavView.setOnNavigationItemSelectedListener
(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.stdcontainer);
switch (item.getItemId()) {
case R.id.navigation_std_current_course:
if (!(currentFragment instanceof StdCurrentCourse)) {
selectedFragment = StdCurrentCourse.newInstance();
}
isHome = false;
break;
case R.id.navigation_std_points:
if (currentFragment == null) {
if (!(currentFragment instanceof StdPoints)) {
selectedFragment = StdPoints.newInstance();
}
}
isHome = false;
break;
case R.id.navigation_std_home:
if (!(currentFragment instanceof StdHome)) {
selectedFragment = new StdHome(attendanceList, studentAbsenceAndDelays, todayTaskList);
}
isHome = true;
break;
case R.id.navigation_std_task_hw:
if (!(currentFragment instanceof StdTaskAndHw)) {
selectedFragment = StdTaskAndHw.newInstance();
}
isHome = false;
break;
case R.id.navigation_chat:
if (!(currentFragment instanceof StdRealTimeChat)) {
selectedFragment = StdRealTimeChat.newInstance();
}
isHome = false;
break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout1, selectedFragment);
transaction.commit();
return true;
}
});

You can control API Calls using a Boolean, set as false once you have received a successful response and when BaseActivity/Fragent where studentBottomNavView is called onPause set the same Boolean as true as per your requirement.

Related

not re-create fragment in BottomNavigationView

Problem:
Each time the BottomNavigationView is clicked the fragment is re-created.
Currently in each fragment I have Recycler View that runs only once when creating the Fragment.
In this case, the RecyclerView data is loaded every time. And that's not what I'd like.
How could I work so that the Fragment remains active after it is clicked the first time. And when you go back to the Fragment already created, just display it without recreating it every time?
My attempts:
BottomNavigationView bottomNavigationView =(BottomNavigationView) findViewById(R.id.bottomNavView_Bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
final Menu menu = bottomNavigationView.getMenu();
fragmentManager = getSupportFragmentManager();
final FragmentTransaction transaction = fragmentManager.beginTransaction();
/* Fragmento Start */
transaction.replace(R.id.content_home, new MainFragment()).commit();
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Intent in;
Fragment fragment = null;
Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.content_home);
switch (item.getItemId()){
case R.id.ic_home:
if (!(currentFragment instanceof MainFragment)) {
fragment = new MainFragment();
toolbar.setTitle("Main");
item = menu.getItem(0);
item.setChecked(true);
}
break;
case R.id.ic_categorias:
if (!(currentFragment instanceof CategoriasFragment)) {
fragment = new CategoriasFragment();
toolbar.setTitle("Categorias");
item = menu.getItem(1);
item.setChecked(true);
}
break;
case R.id.ic_calendar:
if (!(currentFragment instanceof FeedUserFragment)) {
fragment = new FeedUserFragment();
item = menu.getItem(2);
toolbar.setTitle("Feed");
item.setChecked(true);
}
break;
case R.id.ic_explore:
if (!(currentFragment instanceof ExploreFragment)) {
fragment = new ExploreFragment();
item = menu.getItem(3);
toolbar.setTitle("Explorar");
item.setChecked(true);
}
break;
case R.id.ic_person:
if (!(currentFragment instanceof Opcoes)) {
fragment = new Opcoes();
toolbar.setTitle("Opções");
item = menu.getItem(4);
item.setChecked(true);
}
break;
}
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.content_home, fragment);
transaction.commit();
return false;
}
});
You are replacing your fragment every time ie after your switch block
complete.
Solution: Replace only when condition meets and use addToBackStack which helps to maintain stacks. You can read more about from this site
Try this,
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Intent in;
Fragment fragment = null;
Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.content_home);
switch (item.getItemId()){
case R.id.ic_home:
if (!(currentFragment instanceof MainFragment)) {
fragment = new MainFragment();
toolbar.setTitle("Main");
item = menu.getItem(0);
item.setChecked(true);
loadFragment(fragment,"mainMenu");
}
break;
case R.id.ic_categorias:
if (!(currentFragment instanceof CategoriasFragment)) {
fragment = new CategoriasFragment();
toolbar.setTitle("Categorias");
item = menu.getItem(1);
item.setChecked(true);
loadFragment(fragment,"categories");
}
break;
case R.id.ic_calendar:
if (!(currentFragment instanceof FeedUserFragment)) {
fragment = new FeedUserFragment();
item = menu.getItem(2);
toolbar.setTitle("Feed");
item.setChecked(true);
loadFragment(fragment,"feedUsers");
}
break;
case R.id.ic_explore:
if (!(currentFragment instanceof ExploreFragment)) {
fragment = new ExploreFragment();
item = menu.getItem(3);
toolbar.setTitle("Explorar");
item.setChecked(true);
loadFragment(fragment,"explore");
}
break;
case R.id.ic_person:
if (!(currentFragment instanceof Opcoes)) {
fragment = new Opcoes();
toolbar.setTitle("Opções");
item = menu.getItem(4);
item.setChecked(true);
loadFragment(fragment,"opcoes");
}
break;
}
return false;
}
});
/**
* Create new method which replace your old fragment
*/
public void loadFragment(Fragment fragment, String tag){
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.content_home, fragment);
transaction.addToBackStack(tag);
transaction.commit();
}
I would try transaction.add() and search for fragments in back stack with this method findFragmentByTag. https://developer.android.com/reference/android/support/v4/app/FragmentManager.html#findFragmentByTag(java.lang.String).

How to implement proper back navigation for the Android BottomNavigation

I spent my whole day now looking for an answer and didn't found any solution that suited this question.
I am looking for a way to create a BottomNavigation usage similar to the one of the Instagram or PlayKiosk app. Fragments should be added to the back stack only once. When pressing the back button I'd expect the app to jump back to the last Fragment that was visited and the button of the BottomNavigation to fit that Fragment.
Currently I use the following code:
//BottomNavigationListener
private BottomNavigationView.OnNavigationItemSelectedListener buttonNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
switch (item.getItemId()) {
case R.id.navigation_game:
currentFragment = GameFragment.newInstance();
fragmentManager.beginTransaction()
.addToBackStack(TAG_FRAGMENT_GAME)
.replace(R.id.content_frame, currentFragment, TAG_FRAGMENT_GAME)
.commit();
break;
case R.id.navigation_tournament:
currentFragment = TournamentFragment.newInstance();
fragmentManager.beginTransaction()
.addToBackStack(TAG_FRAGMENT_TOURNAMENT)
.replace(R.id.content_frame, currentFragment, TAG_FRAGMENT_TOURNAMENT)
.commit();
break;
case R.id.navigation_history:
break;
}
return true;
}
};
But this leads to the problem that I could press the button of my BottomNavigation a couple of times and for each of this clicks a new Fragment would be instantiated. Also the BottomNavigation buttons are not set according to the Fragments.
I found this answer but it didn't work out Prevent The Same Fragment From Stacking More Than Once ( addToBackStack)
You can use following structure to have the same working logic of Instagram.
First create a custom limited unique queue class. It only holds last N items, where N is the limit number (or maximum item count) passed to its constructor. Moreover, this type of queue keeps only one instance of a class. If an instance of class A is already in the queue and another instance of class A is to be added to the queue, the former instance is removed first and then the new object is inserted.
public class LimitedUniqueQueue<E> extends LinkedList<E> {
private int limit;
public LimitedUniqueQueue(int limit) {
this.limit = limit;
}
#Override
public boolean add(E o) {
// For uniqueness
for (int i = 0; i < super.size(); i++) {
E item = super.get(i);
if (item.getClass() == o.getClass()) {
super.remove(i);
break;
}
}
boolean added = super.add(o);
// For size limit
while (added && size() > limit) {
super.remove();
}
return added;
}
}
Then update your Activity as follows:
public class MainActivity extends AppCompatActivity {
private BottomNavigationView navigation;
private LimitedUniqueQueue<Fragment> queue;
...
private BottomNavigationView.OnNavigationItemSelectedListener onNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
Fragment gameFragment;
Fragment tournamentFragment;
Fragment profileFragment;
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
switch (item.getItemId()) {
case R.id.navigation_game:
if (gameFragment == null) {
gameFragment = new GameFragment();
}
transaction.replace(R.id.content, gameFragment).commit();
queue.add(gameFragment);
return true;
case R.id.navigation_tournament:
if (tournamentFragment == null) {
tournamentFragment = new TournamentFragment();
}
transaction.replace(R.id.content, tournamentFragment).commit();
queue.add(tournamentFragment);
return true;
case R.id.navigation_profile:
if (profileFragment == null) {
profileFragment = new ProfileFragment();
}
transaction.replace(R.id.content, profileFragment).commit();
queue.add(profileFragment);
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navigation = (BottomNavigationView) findViewById(R.id.navigation);
queue = new LimitedUniqueQueue<>(navigation.getMenu().size());
navigation.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener);
navigation.setSelectedItemId(R.id.navigation_game);
...
}
#Override
public void onBackPressed() {
if (queue.size() > 1) {
queue.removeLast();
Fragment previousFragment = queue.getLast();
if (previousFragment instanceof GameFragment) {
navigation.setSelectedItemId(R.id.navigation_game);
} else if (previousFragment instanceof TournamentFragment) {
navigation.setSelectedItemId(R.id.navigation_tournament);
} else if (previousFragment instanceof ProfileFragment) {
navigation.setSelectedItemId(R.id.navigation_profile);
}
} else {
super.onBackPressed();
}
}
...
}

Webservice call before Fragment replacing with bottomview

I want to call web service before my Fragment1 initialize. But In MainActivity first my Fragment1 Replacing then my webservice call occurs. Now how to handle that ? I am using Retrofit for web service calling.
I can not call Webservice in Fragment 1. Because i dont want to repeat during fragment initialization.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
bottomNavigationView = (BottomNavigationView findViewById(R.id.navigation);
//Here My webservice call which response value sent to Fragment 1
bottomNavigationView.setOnNavigationItemSelectedListener
(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
}
android.app.Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.action_item1:
selectedFragment = Fragment1.newInstance();
toolbar.setTitle("1");
break;
case R.id.action_item2:
selectedFragment = Fragment2.newInstance();
toolbar.setTitle("2");
break;
default:
selectedFragment = Fragment1.newInstance();
toolbar.setTitle("1");
break;
}
android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, selectedFragment);
transaction.commit();
return true;
}
});
android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, Fragment1.newInstance());
transaction.commit();
}
}

how to disable a menu item when fragments are used

i want to disable the menu option when i launch the fragments but when i go back to the home screen i want the option to be visible.i tried to set visibility to true in onresume but it gives an null pointer exception
public boolean onNavigationItemSelected(MenuItem paramMenuItem)
{
FragmentManager localFragmentManager = getSupportFragmentManager();
WebViewFragment localWebViewFragment = new WebViewFragment();
ContactUsFragment contactus = new ContactUsFragment();
Bundle localBundle = new Bundle();
switch (paramMenuItem.getItemId())
{
case R.id.signin:localFragmentManager.beginTransaction().replace(R.id.frame_replace, new SignInFragment()).addToBackStack("signIn").commit();
break;
case R.id.drawer_book_your_car:
if ((PrefUtils.getDefaults("isSignedIn", this.mContext) == null) || (PrefUtils.getDefaults("isSignedIn", this.mContext).equalsIgnoreCase("false")) || (PrefUtils.getDefaults("isSignedIn", this.mContext).equalsIgnoreCase("")))
{
Toast.makeText(this.mContext, "Please Sign In", Toast.LENGTH_LONG).show();
return true;
}
break;
case R.id.drawer_myTrip:localFragmentManager.beginTransaction().replace(R.id.frame_replace, new TripFragment()).addToBackStack("MyTrip").commit();
break;
case R.id.drawer_tariff:startActivity(new Intent(this.mContext, TariffActivity.class));
break;
case R.id.how_to_broom:
localBundle.putString("url","www.abc.com");
localWebViewFragment.setArguments(localBundle);
localFragmentManager.beginTransaction().replace(R.id.frame_replace,localWebViewFragment).addToBackStack("web").commit();
localMenuItem.setVisible(false);
break;
case R.id.drawer_FAQs:
localBundle.putString("url","www.abc.com");
localWebViewFragment.setArguments(localBundle);
localFragmentManager.beginTransaction().replace(R.id.frame_replace, localWebViewFragment).addToBackStack("web").commit();
localMenuItem.setVisible(false);
break;
case R.id.drawer_policies:
localBundle.putString("url","www.abc.com");
localWebViewFragment.setArguments(localBundle);
localFragmentManager.beginTransaction().replace(R.id.frame_replace,localWebViewFragment).addToBackStack("web").commit();
localMenuItem.setVisible(false);
break;
case R.id.contactUs:
localBundle.putString("url", "www.abc.com");
localWebViewFragment.setArguments(localBundle);
localFragmentManager.beginTransaction().replace(R.id.frame_replace,contactus).addToBackStack("web").commit();
break;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.DONUT) {
((DrawerLayout)findViewById(R.id.drawer_layout)).closeDrawer(GravityCompat.START);
}
return true;
}
public void onResume(){
super.onResume();
localMenuItem.setVisible(false);
}

Fragment re-created on bottom navigation view item selected

Following is my code for bottom navigation view item selected
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.action_one:
// Switch to page one
fragment = FragmentA.newInstance();
break;
case R.id.action_two:
// Switch to page two
fragment = FragmentB.newInstance();
break;
case R.id.action_three:
// Switch to page three
fragment = FragmentC.newInstance();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment,"TAG").commit();
return true;
}
});
Now my problem is every time fragment is re-created and don't want fragment to be recreated every time I also tried adding addToBackStack(null) but it this case on back button press keeps popping fragments from stack which I don't want.
Is there any way to display fragments on bottom navigation bar selected without re-creating fragment
With support library v26 you can do this
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
Fragment curFrag = mFragmentManager.getPrimaryNavigationFragment();
if (curFrag != null) {
fragmentTransaction.detach(curFrag);
}
Fragment fragment = mFragmentManager.findFragmentByTag(tag);
if (fragment == null) {
fragment = new YourFragment();
fragmentTransaction.add(container.getId(), fragment, tag);
} else {
fragmentTransaction.attach(fragment);
}
fragmentTransaction.setPrimaryNavigationFragment(fragment);
fragmentTransaction.setReorderingAllowed(true);
fragmentTransaction.commitNowAllowingStateLoss();
I faced the same problem and finally i i found the solution, you can try this code. it's work for me.
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public BottomNavigationView bv;
public home_fragment home;
public account_fragment afrag;
public other_fragment other;
public FrameLayout fr;
android.support.v4.app.Fragment current;
//public FragmentTransaction frt;
public static int temp=0;
final Fragment fragment11 = new account_fragment();
final Fragment fragment22 = new home_fragment();
final Fragment fragment33 = new other_fragment();
final FragmentManager fm = getSupportFragmentManager();
Fragment active = fragment11;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bv=(BottomNavigationView) findViewById(R.id.navigationView);
fm.beginTransaction().add(R.id.main_frame, fragment33, "3").hide(fragment33).commit();
fm.beginTransaction().add(R.id.main_frame, fragment22, "2").hide(fragment22).commit();
fm.beginTransaction().add(R.id.main_frame,fragment11, "1").commit();
bv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.account:
fm.beginTransaction().hide(active).show(fragment11).commit();
active = fragment11;
return true;
case R.id.home1:
fm.beginTransaction().hide(active).show(fragment22).commit();
active = fragment22;
return true;
case R.id.other:
fm.beginTransaction().hide(active).show(fragment33).commit();
active = fragment33;
return true;
}
return false;
}
});
bv.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() {
#Override
public void onNavigationItemReselected(#NonNull MenuItem item) {
Toast.makeText(MainActivity.this, "Reselected", Toast.LENGTH_SHORT).show();
}
});
}
}
This seemed to work well for me. Instead of attaching and detaching, i use show or hide to maintain fragment state.
public void changeFragment(Fragment fragment, String tagFragmentName) {
FragmentManager mFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
Fragment currentFragment = mFragmentManager.getPrimaryNavigationFragment();
if (currentFragment != null) {
fragmentTransaction.hide(currentFragment);
}
Fragment fragmentTemp = mFragmentManager.findFragmentByTag(tagFragmentName);
if (fragmentTemp == null) {
fragmentTemp = fragment;
fragmentTransaction.add(R.id.frame_layout, fragmentTemp, tagFragmentName);
} else {
fragmentTransaction.show(fragmentTemp);
}
fragmentTransaction.setPrimaryNavigationFragment(fragmentTemp);
fragmentTransaction.setReorderingAllowed(true);
fragmentTransaction.commitNowAllowingStateLoss();
}
And this is how i use it
private void initViews() {
BottomNavigationView bottomNavigationView = findViewById(R.id.navigation);
bottomNavigationView.setOnNavigationItemSelectedListener
(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.explore:
changeFragment(new ExploreFragment(), ExploreFragment.class
.getSimpleName());
toggleViews(true, "");
break;
case R.id.favorite:
changeFragment(new FavoriteFragment(), FavoriteFragment.class
.getSimpleName());
toggleViews(false, "Favorites");
break;
case R.id.venue:
changeFragment(new VenueFragment(), VenueFragment.class.getSimpleName());
toggleViews(false, "Venues");
break;
case R.id.profile:
changeFragment(new ProfileFragment(), ProfileFragment.class
.getSimpleName());
toggleViews(false, "Profile");
break;
}
return true;
}
});
//Manually displaying the first fragment - one time only
changeFragment(new ExploreFragment(), ExploreFragment.class
.getSimpleName());
}
This solution stops a fragment from being re-created when the currently selected nav button is clicked again. However, a new fragment will be created every time the user clicks a different nav button.
Just add this line to avoid re-created Fragment from BottomNavigationView
bottomNavigation.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() {
#Override
public void onNavigationItemReselected(#NonNull MenuItem item) {
// do nothing here
}
});
Or with a lambda:
bottomNavigation.setOnNavigationItemReselectedListener(item -> { });
Be careful when using replace. Even if providing a fragment that already exists in memory, replace will restart the fragment's lifecycle. To avoid a restart, the transaction object's methods includes add, show, and hide, which can be used to show the correct fragment without restarting it.
private fun switchFragment(selectedTabIndex: Int) {
val previousTabIndex = this.currentTabIndex
this.currentTabIndex = selectedTabIndex
val transaction = supportFragmentManager.beginTransaction()
val tag = fragments[this.currentTabIndex].tag
// if the fragment has not yet been added to the container, add it first
if (supportFragmentManager.findFragmentByTag(tag) == null) {
transaction.add(R.id.container, fragments[this.currentTabIndex], tag)
}
transaction.hide(fragments[previousTabIndex])
transaction.show(fragments[this.currentTabIndex])
transaction.commit()
}
Try this :
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.container);
switch (item.getItemId()) {
case R.id.action_one:
// Switch to page one
if (!(currentFragment instanceof FragmentA)) {
fragment = FragmentA.newInstance();
}
break;
case R.id.action_two:
// Switch to page two
if (!(currentFragment instanceof FragmentB)) {
fragment = FragmentB.newInstance();
}
break;
case R.id.action_three:
// Switch to page three
if (!(currentFragment instanceof FragmentC)) {
fragment = FragmentC.newInstance();
}
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment, "TAG").commit();
return true;
}
});
That will get the current fragment in your container and if you click again on this fragment that will not re add the fragment.
setOnNavigationItemReselectedListener would be a better solution for that
Use setOnNavigationItemReselectedListener like this:
private BottomNavigationView.OnNavigationItemReselectedListener onNavigationItemReselectedListener
= new BottomNavigationView.OnNavigationItemReselectedListener() {
#Override
public void onNavigationItemReselected(#NonNull MenuItem item) {
Toast.makeText(MainActivity.this, "Reselected", Toast.LENGTH_SHORT).show();
}
};
and call it using:
navigation.setOnNavigationItemReselectedListener(onNavigationItemReselectedListener);
The best way i found to do it.
private void replace_fragment(Fragment fragment) {
String tag = fragment.getClass().getSimpleName();
FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
Fragment curFrag = getSupportFragmentManager().getPrimaryNavigationFragment();
Fragment cacheFrag = getSupportFragmentManager().findFragmentByTag(tag);
if (curFrag != null)
tr.hide(curFrag);
if (cacheFrag == null) {
tr.add(R.id.main_frame, fragment, tag);
} else {
tr.show(cacheFrag);
fragment = cacheFrag;
}
tr.setPrimaryNavigationFragment(fragment);
tr.commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_posts:
replace_fragment(new PostsFragment());
return true;
case R.id.nav_stores:
replace_fragment(new StoresFragment());
return true;
case R.id.nav_chats:
replace_fragment(new DiscussionsFragment());
return true;
case R.id.nav_account:
replace_fragment(new ProfileFragment());
return true;
}
return false;
}
};
I've improved #Viven's answers and written it with Kotlin. My version instantiates fragment only for the first time, hides/shows. I'm new at Kotlin so tell me if I can improve anything.
We need to hold a id to tag map:
private val fragmentTags = hashMapOf(
R.id.action_home to "home_fragment",
R.id.action_profile to "profile_fragment"
)
The listener code:
bottomNavigation.run {
setOnNavigationItemSelectedListener { menuItem ->
supportFragmentManager.beginTransaction()
.let { transaction ->
// hide current fragment
supportFragmentManager.primaryNavigationFragment?.let {
// if selected fragment's tag is same, do nothing.
if (it.tag == fragmentTags[menuItem.itemId]) {
return#setOnNavigationItemSelectedListener true
}
transaction.hide(it)
}
var fragment = supportFragmentManager.findFragmentByTag(fragmentTags[menuItem.itemId])
if (fragment == null) {
// instantiate fragment for the first time
fragment = when(menuItem.itemId) {
R.id.action_home -> HomeFragment()
R.id.action_profile -> ProfileFragment()
else -> null
}?.also {
// and add it
transaction.add(R.id.frame, it, fragmentTags[menuItem.itemId])
}
} else {
// if it's found show it
transaction.show(fragment)
}
transaction
.setPrimaryNavigationFragment(fragment)
.setReorderingAllowed(true)
}.commitNowAllowingStateLoss()
return#setOnNavigationItemSelectedListener true
}
//bottomNavigation things
itemIconTintList = null
selectedItemId = R.id.action_home
}
I solved this problem by adding a ViewPager to which I delegated all my navigation fragments. Its adapter (FragmentPagerAdapter) doesn't recreate the fragments instances when the user navigates through the BotoomNavigationView.
To achieve this, you have to complete 5 easy steps:
add a ViewPager to your layout;
implement its adapter:
class YourNavigationViewPagerAdapter(fm: FragmentManager,
private val param1: Int,
private val param2: Int)
: FragmentPagerAdapter(fm) {
override fun getItem(p0: Int) = when(p0) {
0 -> NavigationFragment1.newInstance(param1, param2)
1 -> NavigationFragment2.newInstance(param1, param2)
2 -> NavigationFragment3.newInstance(param1, param2)
else -> null
}
override fun getCount() = 3
}
don't forget to set the new adapter:
yourViewPager.adapter = YourNavigationViewPagerAdapter(supportFragmentManager, param1, param2)
set a OnNavigationItemSelectedListener to your BottomNavigationView like the following:
yourBottomNavigationView.setOnNavigationItemSelectedListener {
when(it.itemId) {
R.id.yourFirstFragmentMenuItem -> {
yourViewPager.currentItem = 0
true
}
R.id.yourSecondFragmentMenuItem -> {
yourViewPager.currentItem = 1
true
}
R.id.yourThirdFragmentMenuItem -> {
yourViewPager.currentItem = 2
true
}
else -> false
}
}
set a OnPageChangeListener to your ViewPager like the following:
yourViewPager.addOnPageChangeListener(object :
ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(p0: Int) {
}
override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {
}
override fun onPageSelected(p0: Int) {
yourBottomNavigationView.menu.getItem(p0).isChecked = true
}
})
enjoy :)
I wrote an Extension function in Kotlin for the same.
fun FragmentManager.switch(containerId: Int, newFrag: Fragment, tag: String) {
var current = findFragmentByTag(tag)
beginTransaction()
.apply {
//Hide the current fragment
primaryNavigationFragment?.let { hide(it) }
//Check if current fragment exists in fragmentManager
if (current == null) {
current = newFrag
add(containerId, current!!, tag)
} else {
show(current!!)
}
}
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.setPrimaryNavigationFragment(current)
.setReorderingAllowed(true)
.commitNowAllowingStateLoss()
}
There are several test cases involved in proper navigation , I am pasting my code with all test cases checked.
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch (item.getItemId()) {
case R.id.dashboard:
Fragment fragment;
fragment = fragmentManager.findFragmentByTag(DashboardFragment.TAG);
if (fragment == null) {
fragment = new DashboardFragment();
fragmentTransaction.add(R.id.frame, fragment, DashboardFragment.TAG);
} else {
fragmentTransaction.detach(fragmentManager.getPrimaryNavigationFragment());
fragmentTransaction.attach(fragment);
}
fragmentTransaction.setPrimaryNavigationFragment(fragment);
fragmentTransaction.commitNow();
return true;
case R.id.expenses:
fragment = fragmentManager.findFragmentByTag(ExpenseFragment.TAG);
if (fragment == null) {
fragment = new ExpenseFragment();
fragmentTransaction.add(R.id.frame, fragment, ExpenseFragment.TAG);
} else {
fragmentTransaction.detach(fragmentManager.getPrimaryNavigationFragment());
fragmentTransaction.attach(fragment);
}
fragmentTransaction.setPrimaryNavigationFragment(fragment);
fragmentTransaction.commitNow();
return true;
case R.id.vehicle_parts:
Bundle bundle = new Bundle();
bundle.putInt("odometer", vehicle.getOdometer());
bundle.putString("vehicle_id", vehicle.get_id());
fragment = fragmentManager.findFragmentByTag(PartsFragment.TAG);
if (fragment == null) {
fragment = new PartsFragment();
fragment.setArguments(bundle);
fragmentTransaction.add(R.id.frame, fragment, PartsFragment.TAG);
} else {
fragmentTransaction.detach(fragmentManager.getPrimaryNavigationFragment());
fragmentTransaction.attach(fragment);
}
fragmentTransaction.setPrimaryNavigationFragment(fragment);
fragmentTransaction.commitNow();
return true;
case R.id.blog:
fragment = fragmentManager.findFragmentByTag(BlogFragment.TAG);
if (fragment == null) {
fragment = new BlogFragment();
fragmentTransaction.add(R.id.frame, fragment, BlogFragment.TAG);
} else {
fragmentTransaction.detach(fragmentManager.getPrimaryNavigationFragment());
fragmentTransaction.attach(fragment);
}
fragmentTransaction.setPrimaryNavigationFragment(fragment);
fragmentTransaction.commitNow();
return true;
}
return false;
Use Cicerone library for handling navigation easily.
https://github.com/terrakok/Cicerone
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.save: {
router.replaceScreen("your fragment1");
menuItem.setChecked(true);
break;
}
case R.id.search_lessons: {
router.replaceScreen("your fragment2");
menuItem.setChecked(true);
break;
}
case R.id.profile_student: {
router.replaceScreen("your fragment3");
menuItem.setChecked(true);
break;
}
}
return false;
}
How To stop recreating Fragment when it is Already Visible Using BottomNavigationView
Step 1--
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
String valuestring;
**if (item.getItemId() == lastSelectedItemId) { // added this
Log.e( "both id is same","LULL" );
return true;
}**
switch (item.getItemId()) {
case R.id.navigation_category:
SetActioBarText( getString( R.string.label_actionbar_categories ) );
fragment = new CategoryFragment();
valuestring = "category";
break;
case R.id.navigation_favourite:
SetActioBarText( getString( R.string.label_actionbar_favourites ) );
fragment = new FavouriteFragment();
valuestring = "favourites";
break;
default:
throw new IllegalStateException( "Unexpected value: " + menuItem.getItemId() );
}
return loadFragment( fragment, valuestring ,menuItem);
}
Now Step 2---
private boolean loadFragment(Fragment fragment, String argument,MenuItem item) {
if (fragment != null) {
transaction = fragmentManager.beginTransaction();
transaction.addToBackStack( argument );
transaction.setTransition( FragmentTransaction.TRANSIT_FRAGMENT_FADE );
transaction.replace( R.id.frame_container, fragment, "demofragment").commitAllowingStateLoss();
lastSelectedItemId= item.getItemId();
return true;
}
return false;
}
no need to do this answers. actually what you really need is to save view in specific fragmet.
private View view;
if(view == null){
view = inflater.inflate(R.layout.call_fragment_edited, container, false);
}
so any time when you create new fragment you see current state

Categories

Resources