How to display a progressbar when loading a new android fragment? - java

I am using fragments to display the information to the user.
Question is. Four of the fragments need to load a lot of information before they are displayed. How can i show a spinning progress bar (not locking the UI-thread) while i fetch the data for the fragment?
Should i create a fragment called ProgressBar that only contains a progress bar. Then show this fragment while i load the new fragment in another thread?
Seems a bit silly to have the progress bar in each of the fragments or is this the normal approach?

I think it would suffice to display indeterminate progress similar to how ListFragment does it out of the box. Don't think of it as loading the fragment though. You're waiting for the fragment's data to load. The Fragment loads immediately, displays an indeterminate progress bar, then registers a callback that will later fire and populate its UI with the retrieved data.

you should put in your fragment life cycle callback while you doing lost of stuff
I think onCreateView just create progress bar use asyn task for long running task then stop progrss bar this is general way i think people used.
thanks

I think you can have a seperate <view> in each of the fragment to show progress in each fragment. This way it will not freeze the other fragment and also it will not freeze the user. Fragments are useful only if the users are not halted.

Related

How to structure a particular activity (Android)

I am developing an activity that contains a kind of mix between a menu and a progress bar. Below this, there are views that are "visible" or "gone" depending on the progress in which the user is. I have programmed everything in the same activity, but I have doubts that this is efficient because we are talking about more than 1200 lines of code and most of it is only used in part of the progress.
Do you think creating a structure with fragments would be better? If so, how would you go about changing from one fragment to the next from a button within the fragment?

Does every activity need a view?

I‘m just starting with android and i‘m wondering whether every activity need its own view or you can manage the ui in the activity itself ? Also it isn‘t pretty clear to me why the xml file belongs to the activity and not to the view
Any activity that has a UI needs a View. Activities handle a UI by displaying the View you set as the content view via setContentView. Activities have no direct drawing functions.
There are a few situations in which you may have an Activity with no View that just does some processing, but its generally a hack to get somewhere else- like an Activity that acts as a router.

Application skipping frames

I have an navigation drawer Activity in which I have made 3 fragments using ViewPager. Each of the fragment has an edit Text, Image button and RecyclerView. The RecyclerView is getting data from a local SQLite database.
On opening the app, I get a Log trace that application skipped XX frames.
(Usually 30-50 frames). Is it normal ? If not, then what should I do? Can fragments be made using background thread like AsyncTask? Or should I populate RecyclerView in background thread?
Is it normal?
Nope. Check out Choreographer (start at line 619) if you are eager to find out what's happening behind the scenes.
what should I do? Can fragments be made using background thread like AsyncTask?
You can't do anything with Views on a background Thread since that would cause a CalledFromWrongThreadException, but what you definitely can and should do is moving all performance-heavy operations like DB read/write to worker Threads. Check out the AsyncTask and AsyncTaskLoader chapter from "Android Developer Fundamentals Course", it will give you a good start on background processing.
Also note that the LoaderManager from the support library is not deprecated.

Architecture for a drawer layout app

I am building an app with a drawer layout similar to the Android Facebook app. I am wondering what the best method for architecture is. Should I have a main activity which is responsible for the action bar, and then have it use fragments to display the content of each menu item, or should I be using one activity to manage the action bar, and then have each menu item kick off entirely separate activities?
I could also imagine building multiple activities, which each have to manage the action bar. This option seems the worst.
You have two architecture options here
MainActivity with Fragments
ParentActivity that handles drawer and lots of Activities that extends this Activity.
I have tried both in different projects and found some things worth sharing.
For me The MainActivity that handles drawer and then using Fragments to fill the display is the best.
You will need to handle callbacks from specific Fragments in your MainActivity and redirect them to the specific Fragment they came from. This is mainly if you use Interfaces in objects lower in the Arcitecture chain since you sometimes need to pass down Activity to certain objects. This generates more code that are not as generic as one might want in top level architecture node.
If you are using a ParentActivity and extending it for each ChildActivity, you can write all specific code in the child, meaning that the toplevel ParentActivity will almost only have generic code.
If you are using the ParentActivity with ChildActivities and you are switching between Activities, you fill get the graphic when an Activity closes and the next opens every time a user switches between navigation objects. If you use Fragments this wont happen as the Fragment will be switched in the background. The user will also experience that the navigation drawer will be closed and recreated each time he clicks on an item there.
Its also unnessecary to recreate the navigation drawer with each click on an item. This is a minus for the ParentActivity approach.
With the ParentActivity approach you will also have to keep track of how the backbutton should function, this will be autoaticly handled for you with Fragments. Also when starting new Activities you have to choose if a new Activity should be created or if the old should be killed etc.
Just my 5c, hopes it helps :)
The best way is to use one Activity with one Fragment per section/view.
Take a look at the design documentation.
Also see the Tutorial and Sample Application. It's fairly straight-forward.
You will have one activity which manages ActionBar, Drawer (ListView!) and Fragment.
Every time it clicks an item in the ListView it updates the fragment with the new view.
If you use different Activities then you should use intents with a very bad effect, use a different activity only if needed (if it's totally unrelated to the current activity maybe?)
Official documentation: http://developer.android.com/training/implementing-navigation/nav-drawer.html
If you got any problem in creating this, online you can found more tutorials but the official is very great.
You should have the activity holding the actionbar & drawer
When using a drawer you should not start new activities from within the drawer but fragment instead
Good post & video about it: https://plus.google.com/u/0/+RomanNurik/posts/3nMVVQzUTjG
another good read: http://www.androiduipatterns.com/2013/05/the-new-navigation-drawer-pattern.html
And finally this is a must see also (check the slides or the video): https://plus.google.com/u/0/+NickButcher/posts/1jeyV2n1ZpM

Working with DIalogs in Fragments

In my application, I have the Activity and several Fragments (Activity works as Controller and Fragments - as views)
In some of Fragments I need to show AlertDialogs and ProgressDialogs, Activity can change current Fragment.
My problem is: activity can receive broadcasts and C2DM notifications, and when I created AlertDialog, Activity can change fragment, but Dialog stays. So when user clicks on some buttons, app crashes.
DIalogFragments works like a simple Dialog.
Have I dismiss dialog manually or check if fragment is active? Is there any built-in tools?
First of all I might be missing something, no code etc to go by but...
Secondly: maybe you shouldn't be using dialoges? Seems like a cumbersome user interface. Just use fragments for those as well? Though you say you are using DialogFragments so maybe you've already thought about that and use them as "regular" fragments already.
Thirdly: Dismiss the dialogs when the fragment that showed is removed/hidden then? Use the onStop() callback for example in the fragment or a more central place where you are perhaps saving the currently showing fragment and deciding to display a new one.
Dismiss the dialog by calling ´dismiss´ on the Dialog object or Fragment or dismissDialog in the Activity.
See dismissing dialog: http://developer.android.com/guide/topics/ui/dialogs.html#DismissingADialog
You can still call dismiss on those DialogFragments.
http://developer.android.com/reference/android/app/DialogFragment.htm

Categories

Resources