Android 30 Activity, how to make them into one - android

Lets say, I want to make a quiz application. And I have 90 question - 3 question on each activity which is 30 activity. The way I know how to make all those Activities is to make a class for each of them. 30 class is a lot.
I'm using this method:
startActivity(new Intent(this, Myclass.class));
What is the best way to make all those activities? Or can it all happen in one?
Thanks, comment if something is not explained clearly.

I would try to separate the data (the questions/answers) from the view (the activities) and use some Intent.putExtra() to open one quiz activity with a parameter to let it know which questions to load/show.
I am not sure, but I think that to share the questions data between the activity instances you could put them in a static variable in that activity.
If the plan is to go like start quiz -> good answer -> start another quiz -> ... then you might want to use android:noHistory to avoid leaving all old question activities in memory.

One thing that you could do is use a viewflipper within a single activity, to show a few different views there. Perhaps you could group your questions and use a single activity for each group, using the viewflipper for each group...

bigstones is correct. You need to separate the data from the Activity, and only create a single Activity. Then, you could have a button that they click when they've answered the set of questions they're currently viewing. Clicking that button would show a progress spinner, and then load the next 3 questions in the background. This should all be done with a single Activity.

Mauzam.
I have a better solution .
You could also use one activity ,and use an Expandable List view instead.
this activity will have all the question in it and when someone clicks on a particular question, this list view would expand to show the options and when answer is selected, u can store it somewhere.
here is an example:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html
I hope this helps!!

Related

Need Advice to Use Fragment on Android Studio

here i want to develop an app that can run a simple quiz for psychology character test, but here i need some advice on how to show the question on the activity. Because i want to showing each question without moving to other activity.
This is my temporary layout (it's still ugly XD), but i want the question can appear on the inside of that red box. And when user press Next button, the question inside that box can changed but before it change the app has been stored the answer of previous question.
I really don't know how to use fragment or something like that.
NB. This is my mainActivity look :
For your app I would recommend not to use fragments at all, it will make your app much slower than it can be.
What I would do in your case, is only load the new question in the textbox on pressing next, and call an asynchronous function (asynctask) to perform the storage of the answer. This way the user sees no delays whatshowever.
If getting the next question required a server connection, use a progress dialog in between to show the user something happens.
Agree with Bas van Stein, using different fragments will slow down the app. What you could do is separate frequently changing part of your screen (e.g. question with answer options) into a fragment and reload it's data on Next/Answer button click. This will separate the code and make it easier to maintain and change it later.
On the other hand you could do it in the activity itself if you don't want to use fragment.

Android Questonnaire layout approach

I want to create a questionnaire but I'm not sure what is the best way to approach this. I'm pretty new to Android but at the moment I have a working app which has a main activity screen which has various options. One of them is to launch the questionnaire. I have created a single database table which contains 4 columns Question Number, Question, Answer, Explanation. The explanation is displayed when the user selects the incorrect answer.
I am envisaging that the screen will be simple enough, just a textview containing the question and 3 buttons, 'YES' 'NO' 'BACK'. I plan to have 15 questions. What I'm wondering is do I have to create an activity for every question and every explanation even though the layout is going to be the same from one question to the next?
Definitely not!
Do this the way you would in any other application. Put your questions in some kind of list and iterate through them. Set the content "question" view, in your layout with the successive values.
... and remember to save the position in the list across Pause and Resume!
Ultimately to get this to work I had to use a counter which I incremented after each successive question. No list was required.

Design advice: multiple choice quiz

I'm interested in hearing what you guys think is the most efficient/solid object-oriented design for a simple multiple choice quiz.
Basically, the app presents a series of questions with 4 choices each. If the choice you selected is correct, then a new activity will display some congrats and full details behind the correct answer and if you're wrong it'll say you got it wrong, along with the details, etc.
After that, the user goes to the next question. On each new question, you'll see your score so far. So maybe you get 50 points for each correct answer, something like that.
Here's how I thought about implementing this: Create a custom view with some radiobuttons or some other widget that could be used for choosing an answer. The custom view takes a Question object, which is just a regular old java object, with fields for choices 1-4. Then, in the custom view, I can set the text of the radiobuttons to the choices from my Question object.
So in my activity it would look something like Question myQuestion = new Question("string for choice 1", "string for choice 2"...)
And then..
Use the question with my custom view. QuestionView myView = new QuestionView(this, myQuestion);
Ok so that's all great. I'm just wondering if I'm setting myself up for trouble. For one, I've got to keep track of the score across all the question activities. Does it make sense to have all of my questions subclass some, mostly empty, activity that I create which can have a counter in it that gets incremented anytime the user gets an answer right (i.e in my superclass activity int scoreKeeper; and in each activity that subclasses this activity: if(choiceIsCorrect) scoreKeeper++). This will allow the score to persist across the activities. I realize I could hold a score between activities by passing and extra to each new activity and then simply adding to it, but that doesn't really seem to make sense to me.
Sorry for all of the blabbering..but I guess my questions are: what do you think is the simplest design for this? Also, in general, I always assumed that you should always use a new activity whenever the user is doing a "single, focused thing"...as the Android paradigm states. But sometimes it feels weird to create so many activities. I know the fact that "it feels weird" is no reason not to do it, but when does it make sense to simply reuse on activity (e.g. in this case, just swapping in a new question in my activity and updating the UI accordingly) as opposed to starting a new activity?
Also, a more detailed question - what would be a smooth way to set which choice (e.g. which radiobutton) contains the right answer, so that when the user presses submit, I can check if they have the right answer and yes/no then react accordingly?
To summarize:
What is a straightforward, object-oriented way to create a succession of multiple choice question activities?
What's the most sensible way to keep track of the score?
Does each question necessarily need to be a separate activity? (And, in general, how do you guys approach the question of whether something ought to be a separate activity?)
Also, kind of a particular: what's the easiest way for me to flag which choice is the correct one so I can check to see if the user got right? I know that with a radiobutton, for example, you can use onCheckedChangeListener and retrieve which radiobutton is currently checked, but I'm not sure how to use that in my design to check if the user got it right...
Thanks!
I would not make the "correct/incorrect information activity" its own activity. Instead, when the user selects a radio button choice, and then clicks an "OK" button, a Dialog should pop up. When they close the dialog, there is a button there to advance to the next question.
I would say you should do this all in one activity.
I'm going to use one activity with a ViewFlipper for cycling through questions.

How to call an activiy itself in android

Basically I am doing a quiz app, I need to dedicate one question to one page, user should be able to flip through the pages to finish one quiz. My proposed idea is to hold a singleton class for all the questions, and have an activity call itself each time for a question, and generate different content according to that question dynamically.But how would one achieve such thing? Can I use intent to call an activity itself? or should there be some other better solutions? thanks
Depending on how many questions you're talking about, the easiest way to do what you're trying to accomplish would be to load up all of your questions into Views and add them to a ViewFlipper. You'd only need one activity, then, and you can animate the transitions between the questions with the ViewFlipper.
You would use startActivity(); or startActivityForResult(); depending on what exactly you wanted to do . Read Here

Best way to restart activity on Android?

I'm building a game which works as some type of a quiz. I ask user the question and when he submits the answer(click or touch correct answer) I need to refresh the page with some other question.
How should i implement this? How to wait for users answer and continue when onClick or OnTouch listener finishes?
Should i use Handler class, intents or something else?
Thx in advance for help.
edit: I want next scenario:
On the screen I have a question and 3-4 clickable ImageButtons. I'm building some of the layout dinamically from custom showQA() function. User choose the answer and if he clicked the correct answer i should start some type of animation on the screen. I've done that from the onClickListener. Now, i need to build layout again(show new question and answers) from the showQA() function which must be called after that animation showed to the user. How can i know when the onClickListener() finished its work?
I think in your case one activity would be sufficient and you just place two views (question, answer) in this activity on which you switch the visibilities accordingly and update the question/answer content.
For the click/touch:
depends what kind of answer the user needs to provider (enter text, click a button, use a slider, etc). But a listener would be the right thing, i.e. onClickListener for a button.
For a quiz, you probably want to implement your special logic in custom widgets that you extend from the basic ones, i.e. a button group for i.e. A) - E) answer buttons (i.e. flexible in number), etc. and abstract the game logic a bit.
But that really depends no your game details, cannot say much withough knowing the game play in detail.
Typically, the easiest way to organize your app is to start a new instance of an activity for each new view.
In your case, you might have one base activity that has a list of all of the questions.
Your ShowQuestionActivity takes in a question and list of answer choices via intent extras.
BaseActivity starts ShowQuestionActivity for result with the first question. When the user clicks on an answer, ShowQuestionActivity finishes. It can pass the answer back with an extra.
BaseActivity immediately starts ShowQuestionActivity for result with the second question. To the user, this will appear as if you just moved from one question to the next smoothly.
Alternatively, you can have each ShowQuestionActivity start the next ShowQuestionActivity, but then you need to keep track of which question to show, and all of the answers via intents.
I've solved the problem using AnimationListener.
I started the animation after user clicks the answer, and called showQA() function for creating user interface from onAnimationEnd method.

Categories

Resources