Issue
My Activity contains two buttons (Next and Back) and a fragment. When the Activity starts, it gets an array of resources. Such as {"Fish", "Bird", "Animal"}; Or this {"Fish", "Bird", "Animal", "Bird", "Bird"}; etc. Depending on the contents of the array, the Next button should switch the fragments accordingly: So: Fragment_Fish.java >Fragment_Bird.java>Fragment_Animal.java. So: Fragment_Fish.java >Fragment_Bird.java>Fragment_Animal.java. >Fragment_Bird.java>Fragment_Bird.java>
How is it easier to implement in Java, please tell me?
Solution
- You need to make an array of the fragments like this:
Fragment[] fragments = new Fragment[]]{new Fragment_Fish(), and other fragments separated with commas};
- Make a int variable in the class something like this:
int counter = 0;
- Increase the counter on button click and update the fragment. Like this:
buttonNext.setOnClickListener(v -> {
counter ++;
if(!counter >= fragments.size()){
//all the fragments are opened do what you need
return;
}
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.details, fragments[counter]);
ft.commit();
//came to another fragment
});
Answered By - Sambhav. K
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.