Sorry for my amatuer question, but I'm really new to Android, and even Java.
I have a listview layout in my appwidget. The listview items are handled in widgetservice.java, where i declared an array to store the listview items in.
There is a configuration button for every listview item. When you click on them a new activity starts, and i really need to acces the listview items in this activities (so actually an element of the before mentioned array).
I did some research how to do that, and I came accross with the Parcelalbe class, so that i can attach a Parcel to my Intents. That is great, however the parcels seem to be just mere copies, and i need my original array elements (so i can change them in the activities, etc).
I hope you could understand my problem, and would able to help me.
Cheers.
Parcelable is one way to go but unless you are just dealing with basic, single dimensional, String only Lists/Arrays than I wouldn't go down that route as it can get complicated quite quickly and is not hugely flexible.
The preferred way to go about this is using Fragments. Such as the ListFragment in this case.
Your ListFragment contains all the bits and pieces it needs to display a ListView. It should generate (or be able to access) it's own collection of items and know how to display and control them. The benefit of putting it all into a Fragment is that you can then easily reuse it in any Activity or layout.xml you like and make use of the same functionality again without having to duplicate all that code.
If you need your ListFragment to be dynamic and not just display the same list all the time you can pass it some parameters through a newinstance(...) method. That method will itself wrap all the info and arguments you want into a Bundle object that you will use to configure or populate your Fragment. Here's how to go about that.
What that ensures is that if your Fragment needs to be recreated (screen rotate, Activity change, Application focus etc etc) it will be able to recreate itself properly using the same parameters again without you having to intercede.
It may seem like more work now to try to get your head around the concept and use of Fragments but in the long run it will save you time, spare you headaches and let you write more maintainable and appealing code.
Related
I want to make a recipes app, the basic layout is the same for all the recipes the only thing that changes are the images, times and ingredients.
The problem is, I could make 40 activities, one for each recipe and performance wouldn't be a problem because the user is only interacting with one activity at the time. However, writing the same code and going on a copy paste spree feels wrong.
I would have to repeat the same code over 40 activities and it would work (I guess), but it would be much easier to create one activity with the functionalities I want like a timer and the layout and in some way make smaller files that insert the data for the selected recipe in that "pre-made template".
There's must be a way of doing it, although I'm not experienced enough
Here is an example layout
It is usually good practice to have a base activity that implements all the code common to several activities then those activities can simply inherit from the base activity like this public class ChildActivity extends BaseActivity.
This will allow you to call methods that are in the BaseActivity from any of the child activities. You can have a read up on Java Inheritance here and here is a blog post with some examples of using a base activity.
You can create only one activity that will receive the Receipt data as extra using Intent. The layout for this activity should contain an image view(or a recycler view to hold all your images), a recyclerview to show your steps/ingredients and a textview for the time.
Receiving these data from the activity(that one that the user selected which receipt he wants too check) that created this new activity, all you need to do is to setup your layout with this data.
Check this question to get how to pass data between activities
Click here to see how to create recycler views.
I am following the Google tutorial for building your first android application. I got to the point where I needed to implement the actionbar actions with the functions openSearch() and openSettings().
I implemented all of this in the MainActivity.java file.
My question is this:
In the example app you can type a message and then send it and it displays it in a second activity. In the second activity, the top action bar changes and does not display my Search icon or perform the action when the settings button is clicked. In order to have these icons displayed in the action bar for this activity as well, do I need to add those methods and update onOptionsItemSelected method in DisplayMessageActivity.java as well as in MainActivity.java? Is this the only way to carry the action bar icons/actions over? To retype the same methods in each activity that you want them in? Or is there a better way to do it?
My other somewhat related curiosity is this. The method openSettings() is called when I click the 3 vertical dots and then settings. These 3 vertical dots show up on every activity, and settings is always in the list. However clicking settings obviously doesn't perform the call to openSettings() when in the DisplayMessageActivity and not MainActivity. How is it that settings and the vertical dots are carried over?
Second to last, how can I add other selections to the drop down list from the options/vertical dots in the action bar? Settings is always there although it responds differently in each activity which was my first question. But I would like to add certain things to the options menu that are on all activities, and some things that are unique to some activities. I assume there must be a better way than repeating switch statements and methods in every Activity.java file.
And finally, what is the best practice to implement an action bar over multiple activities?
Obviously different activities will often have different icons/actions in the action bar, however some things like the 3 vertical dots(options) and settings within that would obviously be acceptable to have in every Activity, while it would be nice to add other things to the options list I don't see why settings should ever change across activities. Yet as I stated before the method is not called in DisplayMessageActivity unless I repeat the code in DisplayMessageActivity.java that I had added to MainActivity.java. I'm confused as to where I can add these so that they are displayed on all activities without repeating code. And I'm confused as to how the actionbar's options/vertical dots are carried over to all activities while others require the repeating of code in each activities' java file that I want them to show up in.
I know this was a bit of a long winded quesiton, I will clarify if necessary. I'm just a bit confused. I was able to make it through the tutorial fine as I have a decent understanding of java. However google's guide isn't written that well and the Android environment is very confusing to a beginner.
I do understand how things work to a degree, I just want to ensure that I'm actually doing it in a way that when my app grows in complexity it won't be a mess of unnecessarily repeated statements and methods.
Thanks in advance for any assistance and tips.
In order to have these icons displayed in the action bar for this activity as well, do I need to add those methods and update onOptionsItemSelected method in DisplayMessageActivity.java as well as in MainActivity.java? Is this the only way to carry the action bar icons/actions over? To retype the same methods in each activity that you want them in? Or is there a better way to do it?
That is certainly one solution, but as you obviously know, it's not a very good one. There are at least two alternative solutions:
Create a MenuActivity class which implements all the logic for common menu items and then extend this class from all of your activities, rather than extending the standard Activity class.
Use fragments to implement your UI. Fragments are similar to activities in that they create UI elements from an XML layout. One difference is that they live inside a "host activity". In this particular case, the host activity will provide the common menu functionality and each fragment can customize it further depending on your needs.
How is it that settings and the vertical dots are carried over?
Most likely your DisplayMessageActivity overrides onCreateOptionsMenu() and inflates a menu XML layout which was created by Android Studio (or Eclipse?) when you created the activity class.
My app has an album style feature that users can pick/take photos from device and load it in every page in this album.
Now the number of this album pages is up to user and should be unlimited. On the Other hand the codes that is being used in activities as you could imagine are exactly same. I have wrote code once and I just want to reuse this activity for each page of album. how should I achieve this?
Since there are thousands of apps that are provide this kind of functions I know there is a good way but i'm unable to find that. Just a trick or a link to a tutorial or even a small explanation is good enough.
Thanks in Advance.
You can create a next() method, and change every layout view in the page with this function, so that you are in same activity but only views are being changed.
One solution is to add the data to be displayed to the "extras" of the Intent which you use to start the activity.
Alternatively, you might want to look at using a Fragment instead of an Activity. You might also want to look at using ViewPager to be able to swipe through the fragments which display the album data.
As a C# developer I'm trying to get familiar with Java and more specifically, the Android framework.
I've created some very basic intro Activities, but I'm now trying trying to get a ListView up and running with 'databinding'. I found this sample code: http://developer.android.com/guide/topics/ui/layout/listview.html
I don't think I've fully grasped the concept of these views yet, because initially I figured that the ListActivity class that I created would be built as some sort of a user-control that I could simply replace my ListView with. When I look at the code though, it appears as though it's somehow trying to hook into an existing view which confuses me. (How do I call this ListActivity from my current activity, and how do I use it with my ListView?).
getListView().setEmptyView(progressBar);
Could someone please clarify what's going on? Perhaps I've become too .NET paradigm orientated and I'm not understanding the picture properly.
A ListActivity is an activity that assumes that you will set a layout with a list view in it with a certain id. It will then store that view in a variable so getListVew() will return it. Basically its a tiny bit of syntactic sugar to do what you can do yourself with 3 lines of code.
Really I don't think its worth using. You can use listviews in a regular activity, it takes 1-3 lines of code to do everything the ListActivity does, and you don't have problems if you name your listview with a different id or if you even decide to not have a listview down the road.
I'm having a bit of an argument with myself and I wanted to check with the rest of you which implementation is "best" in terms of better practices.
Just like a basic Navigation Drawer app, mine has one main NavDrawerActivity that contains the NavDrawer itself, and a Frame Layout that I later replace. Then, I also have one Fragment called ListsFragment, that basically contains a ListView of data from a simple String ArrayList. Therefore, this ListsFragment always contains a basic list of Strings (meaning, I only ever need one fragment.java file).
Now, in Google's example of the Navigation Drawer, they have something similar, where they have one Fragment that they simply replace with new content (replace as in, literally, FragmentManager.replace(etc)). This means that a new Fragment is reinstantiated each time we select an item from the NavDrawer. This is what I'm doing right now as well.
But I've been thinking whether or not doing this is "messy". Won't a cleaner way of doing this be:
Create a method in the Fragment called loadList(ArrayList<String> list), which basically replaces the contents of the ListView in the Fragment with the consumed list.
After all, isn't this a "cheaper" operation cost than having to instantiate a new Fragment everytime? When I could just be "refreshing" the data through a single Fragment variable I'll maintain?
So, my question is: Which is better? Or if "better" is too subjective: Which is more, "the Java/Android way". It'd be great to see what you think. Thanks!
TLDR: Which is better: Instantiating a new Fragment every time I select a new item from the NavDrawer or simply load new data through a public loadData method.
EDIT: For those who want to see Google's NavDrawer example, the Example is downloadable here http://developer.android.com/training/implementing-navigation/nav-drawer.html