Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid recreation of fragment #44

Closed
rajam1215 opened this issue Jul 7, 2020 · 7 comments
Closed

avoid recreation of fragment #44

rajam1215 opened this issue Jul 7, 2020 · 7 comments
Assignees
Labels
wontfix Proposed changes won't be applied

Comments

@rajam1215
Copy link

rajam1215 commented Jul 7, 2020

hello @st235

sins the MVVM support it is awesome, but we need one more enhancement in the library, so Fragment not create every time when any bottom menu item clicked with MVVM

Thanks

@st235
Copy link
Owner

st235 commented Jul 10, 2020

Hi @rajam1215,

sorry for a bit late replay. Did you check the default BottomBarNavigation view? Does it behave in an expected way? Also, did I understand you correctly - double-clicking on the same item causes Fragment recreation? If you don't mind, it would be really cool, if you can record a small video, which reflects wrong behaviour.

Looking forward to hearing from you

@st235 st235 added the help wanted Extra attention is needed label Jul 10, 2020
@rajam1215
Copy link
Author

rajam1215 commented Jul 11, 2020

Hi @rajam1215,

sorry for a bit late replay. Did you check the default BottomBarNavigation view? Does it behave in an expected way? Also, did I understand you correctly - double-clicking on the same item causes Fragment recreation? If you don't mind, it would be really cool, if you can record a small video, which reflects wrong behaviour.

Looking forward to hearing from you

Hello @st235

I am very glade to see you again, i know the default behavior of bottom navigation, this is used only for show activities or fragments, but in this library we move forward already, it have MVVM support, but default doesn't, we need to avoid the recreation of fragment, now please check what is happen right now.

your qus - double-clicking on the same item causes Fragment recreation?
no it's not, it recreate fragment again when click on other menu item,
it will recreate again and again, i want when all fragments create once, then no need to create again, just visible and gone.

i make one little video,it will clear your query

ezgif com-video-to-gif

Please check once.

@st235
Copy link
Owner

st235 commented Jul 15, 2020

Hi @rajam1215,
sorry, it was a really crazy week. I'll be able to take a look at the problem only at the end of this week.

PS: However, it's always welcomed any kind of contribution here. So, if you have any ideas on how to solve this problem, do not hesitate, please, to create a pull request.

Kind regards,
Alexander Dadukin

@rajam1215
Copy link
Author

rajam1215 commented Jul 16, 2020

Hello @st235

not an issue, please see on the issue end of the week, but i have one solution for that.

public class MainActivity extends AppCompatActivity {

final Fragment fragment1 = new HomeFragment();
final Fragment fragment2 = new DashboardFragment();
final Fragment fragment3 = new NotificationsFragment();
final FragmentManager fm = getSupportFragmentManager();
Fragment active = fragment1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);


BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

fm.beginTransaction().add(R.id.main_container, fragment3, "3").hide(fragment3).commit();
fm.beginTransaction().add(R.id.main_container, fragment2, "2").hide(fragment2).commit();
fm.beginTransaction().add(R.id.main_container,fragment1, "1").commit();

}


private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
   = new BottomNavigationView.OnNavigationItemSelectedListener() {

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.navigation_home:
            fm.beginTransaction().hide(active).show(fragment1).commit();
            active = fragment1;
            return true;

        case R.id.navigation_dashboard:
            fm.beginTransaction().hide(active).show(fragment2).commit();
            active = fragment2;
            return true;

        case R.id.navigation_notifications:
            fm.beginTransaction().hide(active).show(fragment3).commit();
            active = fragment3;
            return true;
    }
    return false;
 }
 };`
}

But this solution work with onItemselectedLinstner of bottom navigation.

but not with MVVM Technique

Refer to this for the official solution.
https://github.com/android/architecture-components-samples/tree/master/NavigationAdvancedSample

Please check the official solution for MVVM support, the solution code work with kotlin, and expandable bottom bar project work with kotlin, so you can handle this

Please look in to it.

Regards
Bishal Malick

@st235
Copy link
Owner

st235 commented Jul 19, 2020

@rajam1215, hi 👋

Hopefully, you are well. I took a look at the problem. I tried to reproduce similar behaviour with default android components, so I've changed ExpandableBottomBar to

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:backgroundTint="#fff"
        app:menu="@menu/navigation_menu"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

and also used NavigationUI (an alternative for native android view).

 NavigationUI.setupWithNavController(bottomNavigation, navigationController)

Unfortunately, the behaviour you mentioned below is not followed by material design components. As it's not a native behaviour, I cannot implement it in this library. The users will not expect such behaviour at this component.

I've made a small video of BottomNavigationView. This video contains the default behaviour for default android widget:

device-2020-07-19-223556

PS: also, I looked at the provided sample. The main highlight here: they don't use NavigationUI, but do use a custom extension (BottomNavigationView#setupWithNavController) to provide required behaviour.

Kind regards,
Alexander Dadukin

@st235 st235 added wontfix Proposed changes won't be applied and removed help wanted Extra attention is needed labels Jul 19, 2020
@st235 st235 self-assigned this Jul 19, 2020
@st235 st235 closed this as completed Jul 19, 2020
@rajam1215
Copy link
Author

rajam1215 commented Jul 20, 2020

@st235 hello

I understand what you want to say, it is not necessary to add this functionality in the library, but in my suggestion this feature you can see in maximum apps, you can check from your side also, those who use bottom navigation in the app. coz some time user don't want to see the app screen again and again from start, user always want to use it from where he/she left (App User), i hope you can understand,

you can add this functionality in your lib, if user want to use this functionality in app they can use, if not they can not use (Coder)

Thanks and Regards
Bishal Malick

@st235
Copy link
Owner

st235 commented Jul 20, 2020

Good morning, @rajam1215 👋

Thank you for provided information, I really appreciate it. However, as I wrote below - it's not a default behavior for alternative native android widget. Moreover, it looks like as an application layer responsibility to cache fragments. I can recommend you to look at fragmentFactory - probably, it will cover you case.

Best wishes,
Alex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix Proposed changes won't be applied
Projects
None yet
Development

No branches or pull requests

2 participants