I have a fragment initially inside my activity, this fragment contains a button that's when it's clicked it open the second fragment, and the second fragment contains a button that's when it's clicked it open the third fragment, and so on ...
Now I want to test the launch of the third fragment after passing by the 2 old fragment (not testing the fragment in isolation), I tried to preform a click in the button inside each fragment but it seems espresso doesn't wait for the 2 fragment to launch, before clicking the button inside it, is there any solution for this ?
Without seeing the code its hard to tell what the best way to handle this would be.
However this seems like an issue with the IdlingResource. Espresso should wait until 2nd fragment has finished launching before moving forward.
You can handle it by adding a SystemClock.sleep(int milliseconds) . That is not an ideal solution however and sleeps should be avoided at all costs.
No it's not a timing issue I have an Activity with 3 fragments all of which are visible concurrently. Android Espresso will only do tests in the top fragment and refuses to acknowledge the presence of all the rest of the fragments.
Related
I am currently writing a drawer layout as my main layout, with an embedded FrameLayout that I will be using to hold each “page” when an item on the drawer is clicked on. When the app first starts, an initial fragment will be show. Other fragments may be added/replaced later which is fine, however, my problem is that when the user clicks the back button on the very first “initial fragment”, I don’t want that particular fragment to be removed from the layout. Currently, it is being removed and it’s just showing a drawer layout with no other content (which makes sense). I want the app to automatically exit if the initial fragment was the last one showing and the back button is pressed, instead of removing that initial fragment and then after another back press, then it exits.
Things I have thought of doing:
Not adding the first fragment to the backstack. (If I do this, I can compare it with the classname of the fragment which is a somewhat longer string, or I can use a boolean value for once the first fragment has been placed (and not added to backstack), the boolean is set which allows the fragments to now be added.
Overriding the onBackPressed function of the activity
Does anyone have a suggested way of doing this or can think of a better way? Thanks
The first bullet point sounds the cleanest. You have no other need to handle conditions when back is hit, correct? If that's the case, it's less lines of code (removing one as opposed to adding several) and you get to keep default Activity methods as is.
I know that's not exactly what you asked, but I think the first bullet point is so clean, that I just wouldn't try something else.
I have implemented same in one of the app using my own Stack of fragment. and also implemented onBackPressed method.
Every time when user clicks on item in drawer i add fragment in stack and in back press once its length is 1 I finish the activity with message.
On item click -- Add/replace fragment in container.
OnBackPressed -- Pop fragments from stack and once its last one i finish activity.
Hope this can give you another option to consider.
I have an android app with a MainActivity which implements a menu with 5 buttons. Each button activates a different fragment.
Without using the "setOffscreenPageLimit(int limit)", every time I go from fragment to fragment, each fragment loads every single time.
Using:
setOffscreenPageLimit(5)
I understand that loads all of my 5 main fragments when starting my application. But I don't want this, because it is too heavy.
I want to implement this:
"Load every fragment only when the user activates it, through choosing the menu button.Then keep it on memory , so when user goes back to this fragment, you don't have to load it again."
From the scratchy details you have provided, I understand that, you want to show fragment when it's displayed on the screen, you don't want it to load when fragment is in background. For this you can try this:
https://stackoverflow.com/a/25001920/6383029
I am new to Android Coding so this might seems to be simple, but please help me solve my confusion about Fragment:
Can a fragment "do stuff" like Activities (like call some methods, run some
schedule task. Or it is more like a view to display the info given by the parent Activity
Can 2 fragments run at the same time, for example, if I have 1 fragment runs a scheduled task every 10 ms and 1 fragment runs another scheduled task every 20 ms, can they run together?
I use FragmentPagerAdapter to create different tabs, each of the tabs holds 1 fragment, will switching between tabs pause or stop my fragment? Can I switch to tab 2 and make the fragment on tab 1 still running?
How can a Fragment communicate with other Fragment (with same parent Activity), for example fragment 2 get sensor information and if it is larger than some threshold it flags fragment 1 to do some thing?
Please enlighten me, thank you very much
You can think of Fragments like sub activities. They can almost do everything that activities can. They just extend Fragment, so the code might be a little different. They can have all the views that an activity has.
For your scheduling part, you will have to switch the fragments (so this cant happen in the background), but if you want to switch while the app is on, it is possible.
For the third point, look at the image I have attached(fragment lifecycle)
And this is about communicating with other fragments.
I have this Android architecture:
Activity that has an action bar with two tabs.
each tab is a fragment
my problem is, when i start the activity, the two tabs start working.
I don't want the second tab to start working unless i click on it.
How please?
note that i am using onCreateView on my second fragment
note
i tried to make my functions on onResume() but still fires itself
This comes from caching of the ViewPager : it will initiate the visible fragment and one on each side (left and right). There is no thing you can do against that. The method setOffscreenPageLimit will refuse a value below 1.
So the solution is to fire your own even to actually "start" your fragment when it becomes visible. Don't use the fragment lifecycle per say, but add a custom method : startBeingVisible().
To fire this custom event, you can just use an OnPageChangeListener.
Scenario:
I have a fragment which has a ViewPager which contains 5 instances(different) of a single Fragment with different values, each having a listivew that contains some sort of items.
Problem:
The flow goes smooth when I click an item of listView(say on page1) but the moment I come back from there on pressing back (overridden OnBackPressed in the main_activity (will discuss later)), the viewPager fails to load the subFragments.
But when I switch to the 2nd or 3rd page, it gets displayed, and on going back to 1st page, it now gets displayed.
OnBackPressed():
I am maintaining a manual stack here. When I click on an item of ListView, the current Fragment Instance (of the parent Fragment ofcourse) goes into stack. And when user comes back , that instance gets popped off the stack and I replaces it on the activities FrameLayout container.
References:
Fragments not getting recreated by FragmentStatePagerAdapter after coming back to the main Fragment instance
Guys, I am really pissed off here, please help me out.
Frankly, I haven't worked much with fragments.
But it seems like the page is being thrown off the memory every time you switch to other pages. I am not sure but if there is a way for android to manage this and keep the page in its memory persistently, it might solve the problem
Finally I am able to get a workaround for this issue, which included two things:
while replacing the fragment make a call to
ft.replace(R.id.content_frame, frag).addToBackStack(null);
while initiating the ViewPager, the fragment manager to be used is :
mAdapter = new myAdapter(getChildFragmentManager());
Actually the problem is that, android fails to recognise and load the older instance of the fragment unless you explicitly add that instance to the backstack. So after adding it to BackStack, there is practically no need to maintain your own manual Stack, but you still can do that.