I want to create application which structure can be simplified as this:
Where Events tab has static content in our stackoverflow world, and is not reloading when app is working. So we can simplify it as a ViewPager.
Problem is with reloading Home tab, because when we set option in Drawer, accurate fragment should replaced Home tab.
Question
So here is my question, is it something similiar to ViewPager which replace current window from right side ? Because I replacing fragment in view pager is quit hard, so I want to replace home fragment in traditional way.
I`m not sure what your problem is. You don't want to update home fragment, you just want to replace it, so replace it and notify viewpager adapter for your change, save preview fragment before that and override back button press logic to replace again whit backuped fragment.
Related
I am making an application that has MainActivity that contains BottomNavigationView and FrameLayout above it. There are 3 Fragments say Fragment A, B, and C.
My doubt is, How do I make switching of Fragments as quick as YouTube Android application? By saying quick, I mean that, when I am on "Home" tab of Youtube application and I switch to the "Trending" tab and again go back to the "Home" tab, it simply loads "Home" tab within fraction of seconds, as if it just hided the inflated page in background and showed up when selected from BottomNavigationView. And also, It inflates the page exactly to the same position where I left.
When I am trying to implement the same in my Application, the RecyclerView in Fragment A re-inflates if I come back from Fragment B.
I am expecting the idea how they do it and in which method they do it (For eg. onStart or onDestroy or onViewCreated)...
If you are using viewpager then Increase the viewpager offset limit
viewpager.OffscreenPageLimit = 2;
Tt's limit is one by default. I hope this may fix your issue.
Thanks
I have in my app a drawer navigation, which contain few fragments to go to. when my app starts it opens by default the first fragment - which contains a view pager that has 2 fragments in it as well. in those 2 fragments I have a text view and a button as well.
the problem is that if I click on another fragment in the drawer, and then go back to the first fragment, the button and the text view are gone. in the first time this view pager fragment is creating the two other fragments. I think that when I go back to this fragment again by replacing it, it wont load the other two fragments and their text and button view. I'm going crazy because of this and I cant continue my work..what is wrong here?
I don't know how to upload code snippets..sorry
Thank you for your help :)
I am currently using a ViewPager with a TabLayout, the ViewPager as of now consists of three separate fragments.
I would like to be able to switch to a fourth fragment using a button on one of the already present fragments.
However, I do not want the fourth fragment to be accessible through the usual ways (aka scrolling) without using the button to reach the fourth fragment. Similarly, I would like scrolling to be disabled when I am currently displaying my fourth fragment.
What would be the best way to do so?
The current hack that I can think of would be to create a hidden fragment within my main layout. I can then display it when the button is pressed while hiding the ViewPager at the same time.
Are there any better ways to do this?
Include your ViewPager in fragment and onClick make replace for the next fragment, and you can return to ViewPager`s fragment onBackPress.
I've implemented a NavigationDrawer within the MainActivity which containts two Fragment. The second Fragment contains a ListView that opens a new Activity (onItemClick) that shows the according detailed data. (I guess that's pretty much the master/detail flow).
When I use the up button to navigate back I got shown the first fragment and not the second fragment with the ListView.
Any ideas how to solve that problem?
Make method in MainActivity for example setFragment(int whichFragment);
and set fragment you want in it, you should already have code that do that and than call that method in onBackPressed() method.
For your question about another fragment, well it depends on how your master/detail flow is suppose to work, it is not a problem to use another activity if you dont need left menu any more, but if you do need left menu then use another fragment.
Best regards
I am developing an app that uses ActionBar navigation, I have 3 tabs (I dont use tabhost) as image below:
In the first tab, a fragment named FragmentListItem will be shown, if I click on Item1, FragmentListItem will be replaced by another fragment named FragmentItemDetails, that shows details of the selected item.
My problem is, when I select Tab2 or Tab 3, and then I reselect Tab1 again, what I get is not FragmentItemDetail anymore, it is FragmentListItem instead.
So, why is that? If I want FragmentItemDetail still be there instead of FragmentListItem, how to do that?
Moreover, when I press Back button (when Tab1 is selected), I want FragmentListItem will be shown again. I realise that Fragment class has no onBackPressed method to implement.
There are different solutions to this issue.
One possible solution is to use an own FragmentManager for this tab. This is possible with the getChildFragmentManager() method of Fragments.
Another solution could be a flag to identify which Fragment was shown as last on Tab1. If you know which Fragment was shown as last you could show this one by search this Fragment in your BackStack with the findFragmentByTag(tag) method of FragmentManager.
But I recommend you to try the first Option. If each tab has his own stack you have less work with your BackButton.