Performance issue when calling setRequestedOrientation - android

I have vertical android app with many fragments inside just one activity. At some point I'm having user the sign. Thus I rotate device using following code at onCreateView and onDestroyViewof signature fragment.
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
((AppCompatActivity) getActivity()).getSupportActionBar().show();
However, it takes too much time to replace fragment, sometimes more than 2 seconds, and even I put waiting dialog on nextPage button click, It's not showing the dialog. (Even there is no function inside of nextPage button but replace fragment function)
I think It's about CPU, but didn't understand which column (Inclusive Time, Exclusive Time etc) is related to it. Here is screenshot;
Any idea about solution or what is causing this?
Thanks.

Related

Prevent last Fragment from being removed from backstack

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.

Android espresso test multiple fragments consecutively, inside the same activity

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.

Android fragments refresh every time after returning back to them

This has to be really simple but I've never worked with fragments before and I'm lost here.
I searched and found tons of stuff on fragmets but I can't find an answer to my problem.
On my main activity I have a submenu with 5 buttons, basically a LinearLayout.
I have a List with 5 fragments in it.
When I press a button, I want to have one of the five fragments to be seen.
If I add the fragments with FragmentTransaction.replace(), the fragments are recreated every time. Everything refreshes and this is not what I want. Views are also refreshed if I return to a fragment by pressing the back button.
I couldn't figure out how "not to refresh" fragments. I tried using hide/show but I lose the track of the backstack at some point hence I couldn't iplement the back button behaviour.
Any help would be much appreciated. Thanks.
I think you mean FragmentTransaction.replace(). Consider editing your question.
Anyway, the view will always be recreated when you change a fragment. It is your job to keep the information needed in a Bundle by implementing onSavedInstanceState() and then retrieving the info in onCreateView().

How to use EditText from a single fragment across tabs?

I'm having difficulty understanding the lifecycle of my fragment when the same fragment is created across three tabs. The main widget in each fragment is an EditText object that that is tracked in a class I created, one per tab, created when my app first executes. When the app executes the input (which is provided by buttons on the screen, not a keyboard) sets text into the second tab's EditText.
The input appears to be following getItem() inside my FragmentPagerAdapter. I understand that getItem() is called twice when my tabs are created and that this is perfectly normal behavior but what I'm not understanding is if this is a focus issue or a logic issue with my code design (I'm hoping the former). Could it have to do with instantiate() being returned by getItem()? Once instantiate() is called, does focus always follow it since I have this line of code in the onCreateView() of my fragment?
active.editLine = (EditText) v.findViewById(R.id.display);
// active is the currently active object of my app's class.
The reason I suspect instantiate() is causing my error is because when I increased the rendering of pages via setOffscreenPageLimit(2) the text was being sent to the third tab.
At this point I tried to track what was happening where and it was a nice educational exercise. What I found in my struggles was this is probably a focus issue-- however my attempts at clearing focus and setting focus did not appear to be applied, nor am I even sure if that's the underlying issue. I ended up using active.editLine.debug(0) to see if focus was being lost or gained and it wasn't. I read some of this fairly popular post and took my focus out of my XML but that didn't help either.
Just to make sure, where should I be setting my EditText objects? In OnCreateView() in my fragment? That is where the above code currently lives. I create three objects of my custom class in my activity as global variables. I have getters and setters in what I feel are appropriate areas of my code. Should the above code instead live in onCreate() of my fragment? If you think this is a focus issue, what is the accepted method of turning off focus and only using it when I need it? More importantly, when my app first runs, where do I turn on focus? OnTabSelected() is called before getItem() runs twice to render the first two tabs. So where would I gain initial focus if I'm globally turning it off? Can set focus with a type of listener?
Not exactly sure what you're going for here, but here are some suggestions:
have your global active object live in the main activity and call getActivity().someObject from your fragments
findViewById() will search down the view hierarchy until it finds the first instance of R.id.display
your fragment views won't be available until you commit them with a fragment transaction, this means that their views also won't be available until that time
you are "scoping" findViewById() to some view v by calling v.findViewById()
if you are paging through fragments, perhaps try setting your active view in the fragment's onResume() method
the fragment's onCreateView() method is only called once. This will be called between onCreate(Bundle) and onActivityCreated(Bundle).

Android animate to singleTask

I am making an app with multiple tabs (without the tabview). One takes a long time to load so I made a singletask of it. Now it only has to load once what saves a lot of time.
Only in the onCreate I define the transition I want:
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
Only the onCreate is never called again, i tried to put it on top of the onResume and onStart but that didn't helped either.
The problem now is that the animation becomes the default animation which differs a lot on different devices.
Does anyone know a way to change this? I tried calling it after the startActivity method with no succes either.
If anyone has a solution to stop the loadtime then it is also okey.
The load time goes to making multiple listview in a swipeview (jason fry). This listview contains around 90 imageviews each, most of them are the same image.
Thanks for any thoughts, ideas or solutions in advance.
You should override the finish() method of your activity.
In the overridden method, after calling super.finish() - call overridePendingTransition() with your favorite transition (if you want no transition - define a transition that does nothing, with duration 0).
You could try calling that in the onNewIntent() method. That should be called when you try to launch the Activity a second time.

Categories

Resources