How to open a nth fragment hosted by an activity with the help of intent?
How to open a nth fragment hosted by an activity with the help of intent?
I have an activity with 12 fragments. And another activity with 12 buttons. I want to open the 1st fragment when I clicked the 1st button, 2nd fragment by clicking the 2nd button and so on. But I can open only the activity and always the first fragment opens with it. For the nth fragment, I have to scroll every time.
My app is showing 12 teams names and while clicking on one team it opens full squad list. All the squad fragments (12 fragments) are hosted by an activity. I want to open the squad fragment as per the team.
code
Intent i = new Intent(getActivity(),SquadActivity.class);
startActivity(i);
Team Name
Squad list
please check again
– Badal Kumar
Aug 27 at 7:28
1 Answer
1
You can replace the fragment in the activity container. On button click pass the fragment no. and use it to replace the fragment.
on button click
Intent i = new Intent(getActivity(),SquadActivity.class);
i.putExtra("frag_no",Fragment_no)
startActivity(i);
inside activity onResume()
Intent i=getIntent();
int frag_no=i.getIntExtra("frag_no",0);
Switch(frag_no)
case 1:
getSupportFragmentManager().beginTransaction().replace(R.id.container, new fragment1(), "Frag1");
break;
case 2:
getSupportFragmentManager().beginTransaction().replace(R.id.container, new fragment2(), "Frag2");
break;
case 3:
getSupportFragmentManager().beginTransaction().replace(R.id.container, new fragment3(), "Frag3");
break;
case 4:
getSupportFragmentManager().beginTransaction().replace(R.id.container, new fragment4(), "Frag4");
break;
case 5:
getSupportFragmentManager().beginTransaction().replace(R.id.container, new fragment5(), "Frag5");
break;
case 6:
getSupportFragmentManager().beginTransaction().replace(R.id.container, new fragment6(), "Frag6");
break;
case 7:
getSupportFragmentManager().beginTransaction().replace(R.id.container, new fragment7(), "Frag7");
break;
you can use different condition as per your requirement.
Thank you............
– Badal Kumar
Aug 27 at 11:05
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
please add sample code and screenshot to make the problem clear
– abdul rehman
Aug 27 at 7:15