I am a newbie in Android. I want to ask that do I need to create an activity class for each page in my app? If not what can I do to bring new pages on screen? Is layout inflater a good choice for this?
Thank you.
Lets say, I have a list view which contains questions. When you click one of them you will see the answer o another page. This means lots of activity class. Then I am looking for another solution.
No, you don't need different activities for this. You can make your "Answer activity" dynamic. It can change data according to which ListView item was clicked. Here's a simple solution to your problem. Create a new int variable and change it accordingly to which ListView item was clicked. If item at index 0 was clicked, then this int should be 0. Pass it with extras to your answer activity and in your answer activity, implement an IF statement.
if (intVariable == 0)
{
//show answer 0
}
else if (intVariable == 1)
{
//show answer 1
}
Do you get the concept?
So, if you want to change the layout accordingly to which ListView item was clicked, this might help you.
Are you changing only image sources or actual layout positions? If you're changing positions, you might aswell create another activity because changing layout positions programatically would take too much time and it's too complicated.
Do you only want image sources to change but layout positions to stay the same? Then this is easier. This code might help you:
int position = //get int extras here
switch (position)
case 0:
imageView1.setImageResources(r.id.yourPicture);
case 1:
imageView1.setImageResources(r.id.yourPic2);
So as you can see, your image resource changed accordingly to ListView item clicked.
Yes, to use activities is the most common way to display content. You can bring new content, that is new activities on the screen by starting new activities via Intent. Therefore, make sure to check out the activity lifecycle
http://developer.android.com/training/basics/activity-lifecycle/index.html.
On the other hand, it sometimes is useful to use fragments to display content on the screen. Fragments are basically "multiple screens" that are hosted by one parent activity. By using fragments you can achieve views like the one in the Skype android app.
Here is more on fragments: http://developer.android.com/training/basics/fragments/index.html
All in all: Use activities to display static content and fragments to make your views more dynamic.
Not necessary, it depends on your design. A typical example is Android Gallery, which is implemented in OpenGL. All pages of Gallery are in one Activity. The advantage is the switching speed is fast, and UI seems much slick. Starting a new Activity may cost much and cause delay. So how to implement it this? You need to create a stack for storing all pages, and manually control(add or remove) them while switching different pages. Anyway, it depends on how you want to design your app(some pages could be displayed in new Activity, some pages not). You have to consider cost and performance.
Related
I want to create the look shown in below image. Basically the user should go through all screens until he reaches the final screen.
I want to know if it is better to create a slider for this which can have all screens in it or to have a new activity for every page. I want to add all the inputs from these fields in the database in one single row (entry) instead of each item as one entry. I am using Firebase database to do so. Please let me know what would be a good option to do so.
You can use activity option but in this case you have choose one two options: passing all previous values to the next activity and keeping data in a holder whose scope is longer than activity scope. The former option is better actually for this purpose but it is not recommended because it causes a key mess in your activities. I think the best approach is to use fragments for each screen and keeping the data in the parent activity.
In my application, I have multiple dynamic lists in which the user can navigate between them.
For simplification, the app has a main screen which shows a list of items.
When the user clicks on any of the items, a new list should be shown, and again if he/she clicks on an item again, a new list should be shown.
Moreover, the user should have the ability to navigate back to the previous screen.
I considered two options for implementing this:
Using the same recyclerview and change the content when needed.
Using fragment with recyclerview and replace the fragment with a new fragment that shown a recyclerview with the new content.
My question is about the implications of each of these implementations, especially in term of efficiency and user experience, or if someone can suggest a better approach to this situation.
I know a little about fragments and recyclerview, and I think that using fragment might be preferable since it is suitable if I ever plan to make the application fit into tablets and devices with a bigger screen.
I planned to test both to understand them better. However, I still hoped to hear more opinions.
I would like to thanks in advance to anyone who will share his opinion with me.
I think Using fragment with recyclerview and replace the fragment with a new fragment that shown a recyclerview with the new content is pref rebel because if you need some ui changes also fragment will be good choice.
I will suggest you use fragments for this implementation. So that you don't write huge lines file in one class.
Also it will reduce future problems if app will upgrade and UI or functionality changes, then you will have separate code to change.
Keeping adapters and fragments separate will lead more efficient way to manage user interactions.
Also you don't need to maintain views stack, android will take care of backpress itself.
I currently have a TabActivity which has 4 tabs, within one of the tab's I want to be able to move forward and back between 4 different Activities.
However if I try to start a new Activity now it removes the TabActivty and starts a whole new Activity with no tab bars.
I have read about using view groups but that this is not best practice and also about using a view flipper but this doesn't seem to let me switch between different Activities only change the views within the Activity. I can't implement back functionality for exa,ple.
Can anyone point me in the right direction as to what I should be looking for as a solution to this?
EDIT:
Some more information:
Within the TabActivity my first screen will be a ListView that contains 4 rows, then selecting one of these will in turn load another ListView with 2 rows again within the TabActivity and then the 3rd screen will just contain some text depending on which option the user chose again within the Tab Activity.
Is a ViewFlipper the best solution here? It seems to me that it will require a lot of coding within one Activity if I use the ViewFlipper?
I have done something similar. I used the ViewFlipper to achieve this. You can override onBackPressed in your Activity so you can deal with moving back through your views.
There's a couple of ways of doing this but a simple way would be to just increment a counter in your Activity as you move to the next views, then in your onBackPressed method if counter != 0 just show the previous view, if counter == 0 call super.onBackPressed.
You can see in my video showing what the result could look like (ignore the bug being shown in the video).
I have a group of tabs. In one of the tabs, I want to display two different types of functionality.
So my question is it better to implement a activity group with child activities or just create a view flipper and switch between views.
ActivityGroup is used to manage one or more activity example. if two different activity have different implementation but the user want to see on sharing screen basis means in first half user want to see some thing and in second have some other thing. In that case ActivityGroup comes to play
ViewFlipper can be used if you want to periodically change the views. Say like an automated flipping book of some sort. Though a custom-adapter gallery is much better at this.
Both have different purpose. It depends on your requirement you will choose any of them.
i would go with a view flipper. you probably dont need the whole overhead of creating another activity. but that way will make your single activity that will hold them a bit larger. so also maybe try to implement some of the functionality inside the views you will be adding.
hope this helps.
I've recently decided to update my app to support the new fragments feature in honeycomb 3.0.
My Application currently works on a list view that opens different activities depending on which list item is clicked.
Using an adaptation of the code in this tutorial I have created an app that consists of only two activities, but depending on which list item is clicked the second "viewer" activity launches using a different layout xml.
Unfortunately I haven't been able to figure out how to call the old methods that had all the functionality. Should I Import all of my old activities and then call the methods into the viewer activity (I may need some advice on how exactly to do this) or should I just put all the methods directly into the same viewer activity (please consider the size of these methods(which is very large by the way)).
Once everything is working with two activities upfront then it will be a pretty simple task of "fragmenting" the app as demonstrated here
Although I haven't considered that there might be a way to allow multiple fragments to occupy the same space in an activity(If this is the case then please let me know how it's done)
Thanks
As James has pointed out you will have to move the business logic from your Activities to your Fragments.
To handle events you can create a listener Interface. The CONTAINER activity/ies will implement this interface. As fragments has access to the container activity you will be able to delegate to the container Activity the "logic" for the desired events. For this events the activity will decide whether to launch a new activity, show/hide new fragments or whatever.
I had a similar question, take a look to the question and answer: here
Although I haven't considered that there might be a way to allow multiple fragments to occupy the same space in an activity(If this is the case then please let me know how it's done)
I think its possible to allow multiple fragments to occupy the same space in an activity. Again, take a look to the answer here ... I think the concept/scope of Activity has change a bit and now an Activity can contain different Fragments which every one will allow user to do a single focused thing.
I'm not sure what you mean by "call the old methods that had all the functionality". You'll want to rewrite all of your activity classes as fragments. Check out this tutorial here (it's very concise). Basically, you'll want an activity that consists of a ListFragment and a FrameLayout. Your ListFragment will update the FrameLayout by changing to the appropriate Fragment based on which row was selected.