1

I was thinking to build Android app which is based on image of questions and there answer options. There would be few exercise and in each exercise there would be approximately 50-60 questions and answers.

I was thinking that to build, for each question there would be each Activity. But I know it's wrong approach. I couldn't be able to find answer.

If I will use fragments, than may be there would be many numbers of fragments file. Totally, I couldn't be able to make decisions.

What to do ? If there is any tutorial or videos, that would be helpful and give your suggestions please.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Joshi Yogesh
  • 142
  • 1
  • 12
  • 1
    Use single activity with few fragments and update the data from your API calls. – Pushpendra Jan 03 '17 at 10:05
  • 1
    You will only have few Activities. As for each question the components on your screen will be the same, you won't need to create one activity for each question. – Wilder Pereira Jan 03 '17 at 10:07
  • 1
    Use Pager Adapter :) – Malik Abu Qaoud Jan 03 '17 at 10:11
  • Viewpager is good – ugur Jan 03 '17 at 10:16
  • You need to create only 3 files. Your MainActivity. FragmentClass and FragmentPagerAdapter. And then create a list of Fragment. `List` in your MainActivity then set each questions as argument to the fragment and add this fragment to the list. And then send the whole fragment list to FragmentPagerAdapter. Thats it – Vishal Chhodwani Jan 03 '17 at 10:17
  • @Vishal Chhodwani is there any tutorial or videos link can you provide , so that I could clear more concept – Joshi Yogesh Jan 03 '17 at 14:31
  • You can try this example. [ViewPager with FragmentPagerAdapter](https://guides.codepath.com/android/ViewPager-with-FragmentPagerAdapter). But You do not need to create more than one fragment. create a for loop and in loop create new Instance of that fragment, and then set your question as argument of fragment. it will solve your problem. – Vishal Chhodwani Jan 04 '17 at 05:49

3 Answers3

0

You can create one activity and one fragment.Fragment will have a question and the user chosen answer along with the rest of the options.You can simply create a fragment with the new data ie Questions and answers and add it to backstack(only if the user can edit the previously answered question).You can refer this One Activity and all other Fragments for one activity and many fragments Also, http://www.vogella.com/tutorials/AndroidFragments/article.html would help you create/understand how to use fragments in android

Community
  • 1
  • 1
  • What I got ,It means , you are suggesting to make several fragments for each question , if i have approximately 150 questions then there would be 150 different fragments layout ? It would be justified ? Please elaborate . – Joshi Yogesh Jan 03 '17 at 11:42
  • No. Just create one Fragment that takes data ie Question id. In fragment, fetch the question, options, the right answer etc using the Question id. Basically, only the Question id keeps changing and thereby reuse the same fragment. You dont have to create soo many fragments or layouts. – Gautami Bhandary Jan 03 '17 at 11:47
0

Pass question index in an intent extra:

Call

 Intent intent = new Intent(this, QuestionActivity.class);
 intent.putExtra("QuestionIndex", questionIndex);
 startActivity(intent);

In QuestionActivity:

 @Override
 protected void OnCreate(Bundle savedInstanceState) {
     ....
     Intent intent = getIntent();
     int questionIndex = intent.getIntExtra("QuestionIndex", 0);

 }     

Information related to questions (image resource Ids, text resource Ids, etc) you can keep in QuestionActivity (accessible only in activity) or in Application class (accessible everywhere).

cyanide
  • 3,885
  • 3
  • 25
  • 33
  • What about answers , should I just put in another Activity file or should I use sqLite database . And what should I use to link each question to its corresponding answer . – Joshi Yogesh Jan 03 '17 at 11:34
0

You will just need 2 activities and 1 fragment. 1 activity to show list of exercises. 1 activity to show fragment. Now fragment will have question and answer when the user clicks on next button create new instance of same fragment and pass data of next question and answer. :)

Kshitij Jain
  • 549
  • 6
  • 12