What is the best way to manage BackStack in Fragment [duplicate] - android

This question already has answers here:
Fragment with multiple backstack
(4 answers)
Closed last year.
Hello i want to manage BackStack in fragment i have implemented a method but im not happy with it becuase if i switch to multiple fragments again and again ,then i want that backstack dont switch to the same fragment again and again and just goes to each fragments one time backward and in the last to the Home_Fragment and then exit
For Eg : if i switch from notification to profile and make it like a loop by pressing it again and again ,and when i start pressing back it also follows the same loop that i dont want ,i want that even user do a thing like this the back stack only goes to each fragment only once and at the last goes to the Home Fragment and exit
Just like a big app like instagram or pintrest
here is a code
private final BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener =
item -> {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.nav_home:
selectedFragment = new Home_Fragment();
break;
case R.id.nav_following:
selectedFragment = new Following_Fragment();
break;
case R.id.nav_upload:
selectedFragment = new Upload_Fragment();
break;
case R.id.nav_notification:
selectedFragment = new Notification_Fragment();
break;
case R.id.nav_profile:
selectedFragment = new Profile_Fragment();
break;
}
assert selectedFragment != null;
getSupportFragmentManager().beginTransaction().addToBackStack(String.valueOf(selectedFragment)).replace(R.id.fragment_container,
selectedFragment).commit();
return true;
};

I'm having difficulty understanding the question/problem, but if you want to avoid re-adding a fragment that is already added then you could check the stack before adding a new fragment is it is already there. This article might be helpful.

use popBackStackImmediate("your-tag",0)
or findFragmentByTag in fragmentManager and replace it(be careful about adding to back stack again and again).

Related

Layouts sometimes overlap in android fragment

My code works very well
But sometimes layouts overlap
The reason for this code is that the first fragment should not be reloaded.
And the second and third fragments should always be reloaded
Have you ever had problems with layout overlapping code?
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem)
{
Fragment selectedFragment = null;
switch (menuItem.getItemId())
{
case R.id.timer:
fm.beginTransaction().hide(active).show(fragment1).commit();
active = fragment1;
break;
case R.id.favorite:
fm.beginTransaction().hide(active).commit();
fm.beginTransaction().detach(fragment2).attach(fragment2).commit();
fm.beginTransaction().show(fragment2).commit();
active = fragment2;
break;
case R.id.calendar:
fm.beginTransaction().hide(active).commit();
fm.beginTransaction().detach(fragment3).attach(fragment3).commit();
fm.beginTransaction().show(fragment3).commit();
active = fragment3;
break;
}
return true;
}
As per my understanding you have to show one screen based on menu click so why not you're using FrameLayout in xml and based on click or any event just add or replace the fragment. Make 3 different Fragment class and use interface or any latest arch component provide by Google.

Do fragments need to be always recreated when exchanging the fragment?

Is it really necessary to always recreate fragments when navigating the bottom nav menu like in this code?
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.nav_home:
selectedFragment = new HomeFragment();
break;
case R.id.nav_favorites:
selectedFragment = new FavoritesFragment();
break;
case R.id.nav_search:
selectedFragment = new SearchFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
};
I would rather like to create all my Fragments once in onCreate of the surrounding MainActivity and store them there as member variables. Then i could in the onNavigationItemSelected just use the references to my fragments instead of creating new fragments.
Is it okay to do it as described and not use the code above? Or could doing it as described cause complications somewhere?
Actually, creating them on create would be the best way. Also, sometimes people tend to add a lot of initializations on their on create and in the long run ur app might use too much memory when initializing the fragments every time you need them.
You would have to be responsible for saving your fragments state, so on recreation, your data would be readily available. You could try something like this
https://proandroiddev.com/fragments-swapping-with-bottom-bar-ffbd265bd742
Although there are many approaches you could attempt.
You could create all the fragments at once and swap them with your bottom navigation. But I wouldn't recommend that for obvious navigation and performance reasons.

Correct Activity/Fragment Architecture for Android Bottom Navigation View

I would like to get some input on the best way to structure my app's architecture when using Android's Bottom Navigation View.
Currently I define my BottomNavigationView in my MainActivity. It looks something like this.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()){
case R.id.action_home:
selectedFragment = HomeFragment.newInstance();
break;
case R.id.action_search:
selectedFragment = SearchFragment.newInstance();
break;
case R.id.action_message:
selectedFragment = MessageFragment.newInstance();
break;
case R.id.action_profile:
selectedFragment = ProfileFragment.newInstance();
break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, selectedFragment);
transaction.commit();
return true;
}
});
//Manually displaying the first fragment - one time only
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, HomeFragment.newInstance());
transaction.commit();
}
The problem is that once I click on one tab, it opens up a fragment, and I would like to have those Fragments open up other Fragments/Activities (i.e:
I open the profile tab (`ProfileFragment` loads)
I click on a button from `ProfileFragment`, and from this the `SignUpFragment` or `SignUpActivity` loads
After running into many bugs, I've researched on how to architect my app, but i've found mixed results. Would anyone know the correct way of using a BottomNavigationView with Fragments, and in those fragments I can load more Activities/fragments. A huge thanks in advance.
Every approach depends on the project and what you pretend to achieve. I had to code a Bottom Navigation app that works with over 20 Bottom Navigation layouts, meaning one single Activity. The process you wish to achieve is pretty much the same of setting the desired fragment in the desired tab on tab selected, the difference is that, instead of taping on a tab, you will tap on a button inside a fragment, which you will replace with the new desired fragment.
tap tab -> replace fragment -> button click inside fragment -> replace fragment -> and so on.
Since you are using replace, you will have to carefully handle your onBackPress event, since I'm assuming that on every back press you wish to go back to the previous fragment. Myself, I've implemented an interface in the Main Activity that listens to the visible fragment onBackPress.

FrameLayout setOffscreenPageLimit?

is this possible ? I use framelayout to display the contents of my fragment with the use of navigation drawer. What i want to happen is to keep the state of the fragment even if i go to another fragment.
I have a vague idea on whats causing the reload which is this.
private Fragment getProfileFragment() {
switch (navItemIndex) {
case 0:
// home
ProfileLO profileLO = new ProfileLO();
return profileLO;
case 1:
LocationLO locationLO = new LocationLO();
return locationLO;
case 2:
FarmGoodsLO farmGoodsLO = new FarmGoodsLO();
return farmGoodsLO;
case 3:
RentEquipLO rentEquipLO = new RentEquipLO();
return rentEquipLO;
default:
return new ProfileLO();
}
}
Is there any hack out there?

Replace Activity to Fragment

I have a drawer navigation with:
private void DisplayView (int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
case 2:
       ...  
From an activity I can throw that fragment (fragment2 for example)? With BeginTransaction?
Thanks
A Fragment cannot exist in it's own, eg. without an Activity. It cannot be fired up with an Intent like Activities do. You have to either create a new Activity to hold your new Fragment or replace the Fragment of your current Activity with the new one.
You cannot start fragment as an activity, they need to be added to activities.
Read more in the docs here
Also, see this

Categories

Resources