Does every activity need a view? - java

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.

Related

StartActivityForResult to get images from gallery in MVVM architecture?

I am using MVVM architecture for my PhotoEditorActivity, the activity plays the View role. When user clicks "Pick Image" button, I want to start gallery to pick an image and get back its uri to update an ImageView. I am confused in where to call startActivityForResult().
Call startActivityForResult() from View: By this way, the activity processes everything, ViewModel and Model become redundant.
Call startActivityForResult() from ViewModel and View observes Uri from the ViewModel: I can pass Context to ViewModel to start activity, but I can not receive uri because ViewModel does not have onActivityResult().
Call startActivityForResult() from Model: View pass a Context to ViewModel, ViewModel continue passing the Context to Model. Using this way, I face the same issue with the second way.
Could you please help me how to properly apply MVVM for this business. What should each component contain? Thank you for your attention.
All Android related things are best handled in the View. Specifically making your code loosely coupled depends on what you intended to do with the image / file that you have selected.
If for example you are trying to upload it as a bitmap, then you should create the bitmap in the view and pass it to the view model.
Also, onActivityResult is now depreciated. See the official documentation for it here.
You may also want to look at this answer for an example implementation of it.

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

How to reset a fragment's view?

If I have a fragment where I dynamically set a view with onCreateView(), how would I go about calling it again?
I want to implement some kind of "refresh" where the view changes based on the JSON response. I tried making a new function that does midnightSV.removeAllViews(), but how can I call onCreateView() again?
You can't without detaching and re-attaching the fragment.
If you just want to update the data in the view, you can find those views and refresh them from the existing fragment.
If you really want multiple sets of unique layouts, you can look into using a ViewFlipper for your fragment layout and then call setDisplayedChild() to switch to a specific view.

to launch a Dialog from a Service, should I use Activity or View?

Say I'm running a service and need to pop out a dialog. As we know, it's impossible to launch a dialog directly from a service, therefor we need to launch and activity ( or view ) and then have it launch our dialog.
The dialog, as well as the activity launching it, should NOT obstruct whatever is below it, i.e. what's on screen should not turn gray and any buttons, that are outside of the dialog, should still be clickable.
Can this be achieved with using an activity or would the activity block the view under it anyway?
If so, guess I would have to use a view... since I haven't worked with views before, what would be the right way to initialize it, so that it won't obstruct whatever is under it?
Thanks!
You could have it launch as an activity using the Dialog theme:
http://developer.android.com/guide/topics/ui/themes.html (see heading: Apply a theme to an Activity or application)
Although, no matter what you will probably obstruct the user in some way ;-). This method should only show a minimal dialog box instead of taking up the whole screen though
Can this be achieved with using an activity
No.
would the activity block the view under it anyway?
Yes.
If so, guess I would have to use a view
A view normally is hosted by an activity. A service cannot just create some random view and put it on the screen.
You can attempt to use a Toast with a custom View for a modeless "dialog", but I am uncertain if a service-constructed View will work with that.

Avoid Android automatically re-add my fragment on orientation change

I have a ListFragment which depends on the hosting Activity to properly initialize. On first run, it loads up fine. Once I change the orientation, my app crashes. From the stack trace I can see it isn't me trying to add the Fragment prematurely, rather Android is trying to restore the Fragment.
I have setRetainInstance(false) set in the onStart method but can't find any method to disable the restoring of the Fragment once the orientation changes. Any ideas? Do I need to remove the Fragment prior to my app being destroyed?
Edit: I ended up delaying initializing the list until the Activity is ready. Android conveniently shows a 'loading' message until the adapter is set.
I ended up delaying initializing the list until the Activity is ready. Android conveniently shows a 'loading' message until the adapter is set.
I have a ListFragment which depends on the hosting Activity to properly initialize.
That may be your difficulty right there.
From the stack trace I can see it isn't me trying to add the Fragment prematurely, rather Android is trying to restore the Fragment.
Correct.
Any ideas?
I would focus on handling configuration changes properly. Between onSaveInstanceState() in the fragment and the combination of onRetainNonConfigurationInstance(), getLastNonConfigurationInstance() (both on Activity) and onAttach() (on your Fragment), you should be able to pass whatever stuff is in the old fragment to the new one without crashing. For configuration changes, do not rely upon "the hosting Activity to properly initialize".

Categories

Resources