How can I change one page to another without using Intent? - android

I want to implement quiz module in my application which consists 14 questions so how do i go from one question to another. If i use intent then this is not smart work because i have need to create 14 activities. is there any other ways to move to next page without using intent.?

You can use the Fragment inside your activity which load your view with data. For this you need to ID to get the data for example Question 1 have ID 1 which will be fetching from database and display in your view, this will be implementing in your fragment, firstly get the ID and query in your database for fetch that record and then display in your view and this fragment object will initiate and set in your layout. Now after question complete pass the next question ID to your fragment with new initialization and replace with current fragment.

You can achieve this in two ways
1) Using Fragments:
Have 15 fragments
All are mounted on a single container
Start one fragment to another dynamically
2) Using a Single activity
Have 1 Textview for question
Say if you have objectives answers have radio buttons for them
Then u have to write a logic to dynamically change the data one after
another keeping the same activity
Best Approach
Using the Fragments Would be best and simple way to achieve your objective because second option unnecessarily complicates the things
Online Sources to learn fragments
If you have not used fragments before click here to check developer
docs
Simple tutorial to learn fragments

I assume your modules are in different layouts. Then you can use above syntax to change to another one:
setContentView(R.layout.second);

Related

Multiple screens In one slider view for android and firebase

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.

How to divide activity to two page/layouts as in viewpager?

I have an application with a single page. But I want to create another page for an information from the file that I fill up using background service. I want it look like as swipable tabs. What is the best option to do it ?
I have only 1 Activity and 1 service. Service is just for getting information from web and saving it to file. First page is for controls, second, which I want to add, is for ListView that will read from that file service is filling up (swipable tabs will be the best). I tried to use Fragments, but there is too much to rewrite in order to transfer logic to Fragment.
Your Best option is to use ViewPager and Fragments as the UI codes will be clean and will easily be obtained from internet.
The other option if you are not willing to rewrite the code is to use a Tabbed View and clicking on the tab will hide and show elements in the mainlayout( Will not have animations though)

Clarification on the structure of my app

Some background info first. I require my app to show a feed of posts, implemented as cards. The data within the posts/cards is retrieved from my site's REST API. My app has 3 different tabs, each containing a different feed of posts/cards (but still the same layout).
Currently, I have an activity_main and the 3 fragments made for each tab (fragment_1, fragment_2, fragment_3). To my understanding, a Fragment is a layout that can be re-used on different Activities. Is this correct?
Additionally, where should I implement the layout/design for the cards? Should it be directly within the separate Fragments (even though the layout of the cards will be the same across all 3 tabs)?
After implementing the card layout, how would I populate the cards given the requested REST API json data? In other words, how would I loop through each result and insert the data into the card and display it on the screen?
Sorry for the noob questions.
Currently, I have an activity_main and the 3 fragments made for each
tab (fragment_1, fragment_2, fragment_3). To my understanding, a
Fragment is a layout that can be re-used on different Activities. Is
this correct?
Yes.
Additionally, where should I implement the layout/design for the
cards? Should it be directly within the separate Fragments (even
though the layout of the cards will be the same across all 3 tabs)?
Have a single fragment. Create 3 instances of this fragment and inflate the same layout/design if the layout of the cards will be the same across all 3 tabs.
After implementing the card layout, how would I populate the cards
given the requested REST API json data? In other words, how would I
loop through each result and insert the data into the card and display
it on the screen?
Pass a unique identifier that identifies the feeds (for this fragment) to the fragment as a bundle when you create a new instance and set them as the fragments argument. You can then retrieve this information to retrieve data relevant for this fragment.
To display the data, use a ListView/RecyclerView along with a suitable adapter to connect your data to your view.
I hope this answers your question.
OK. Lets try this. Yes, you are right, Fragments are basically layouts that can be used in different Activities. As it says on Android Developers site:
A Fragment represents a behavior or a portion of user interface in an
Activity. You can combine multiple fragments in a single activity to
build a multi-pane UI and reuse a fragment in multiple activities.
Having that said, since it is the same layout for all three tabs, you can use only one Fragment and populate it with different data. So if you want to change anything, you would do it only within that one fragment.
For the cards I am guessing that you will be using a custom ListView adapter. And that you should (must) implement outside the Fragments.
And regarding the last question, there are many libraries that make consuming ReST webservices much easier. For example Retrofit.

how to update a fragment from another fragment

I have fragemnt in which several buttons layaut and picture. In creating this fragment on top of the loaded data. in the lower part of the site for other fragments. One of the fragments contains a list. When I click on an item list, I need to update data on a fragment that contains this fragment. This hour I just create a new fragment the parent with the new parameters derived from the list. But I think it's not right. tell me how to update the fragment.ie I update the data and set to the desired form elements, but how to update the GUI itself
example may be easier
Using Interfaces for InterFragment communication should do the job for you.
https://www.youtube.com/watch?v=VyyGP_d0Ia8&index=8&list=PLonJJ3BVjZW4lMlpHgL7UNQSGMERcDzHo
Look into the below video, It might help you.
You should add interfaces to the fragment and implement them in your activity. Interfaces are good way to communicate between fragments. Take a look at here. In the implemented interface write code appropriately to change other fragments.

Converting Multiple Activites into a Single Fragment

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.

Categories

Resources