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 add sample code and screenshot to make the problem clear
– abdul rehman
Aug 27 at 7:15





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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế