Reputation: 10891
I have a MainActivity that has several fragments which are added and then shown/hidden. This is because the MainActivity uses a NavigationDrawer
. Clicking on items in the drawer causes different fragments to be added (if they do not exist), or shown/hidden if they do.
My question is, how can I launch my MainActivity via an intent from a different activity, and at the same time show a specific fragment?
Would I have to pass some extra to my MainActivity and then based on that data, add/show/hide the relevant fragment? Is there another way?
Upvotes: 6
Views: 16176
Reputation: 71
In Kotlin,
val i = Intent(context, MyActivity::class.java)
i.putExtra("fragmentToLoad", "fragment2")
startActivity(i)
Inside your activity check the extra and load the intended Fragment:
val intentFragment = intent?.extras?.getString("fragmentToLoad")
when (intentFragment) {
"fragment2" -> {
val fragment = Fragment2()
supportFragmentManager.beginTransaction()
.replace(R.id.main_content, fragment)
.commit()
}
}
Upvotes: 1
Reputation: 211
When you create your Intent, you can give it an extra that determines the fragment to load.
Intent i = new Intent(this, ActivityClass.class);
i.putExtra("frgToLoad", FRAGMENT_A);
// Now start your activity
startActivity(i);
Now, inside your activity check the extra and load the right Fragment:
OnCreate(){
...
int intentFragment = getIntent().getExtras().getInt("frgToLoad");
switch (intentFragment){
case FRAGMENT_A:
// Load corresponding fragment
break;
case FRAGMENT_B:
// Load corresponding fragment
break;
case FRAGMENT_C:
// Load corresponding fragment
break;
}
}
Upvotes: 21
Reputation: 154
You can use sharedPreference to achieve the goals. Save the index after you open you TAB/Fragment.
//get current tab
sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
String position = sharedPreferences.getString("tab_opened", null);
if(position==null){
viewPager.setCurrentItem(1,true);
}else if(position=="0"){
viewPager.setCurrentItem(0,true);
}else if(position=="1"){
viewPager.setCurrentItem(1,true);
}else if(position=="2"){
viewPager.setCurrentItem(2,true);
}
}// <<<~~~ THAT IS END OF onCreate() method.
Please refer to this tutorial : OPEN THIS .
Upvotes: 1
Reputation: 240
In case you are looking for something like this, hope so!
@Override
public void onNavigationDrawerItemSelected(int position) {
Fragment fragment;
FragmentManager fManager = getFragmentManager();
Bundle args = new Bundle();
args.putInt(MCConstants.ARG_SECTION_NUMBER, position);
clearAllBackStack();
FragmentTransaction fTransaction = fManager.beginTransaction();
switch (position) {
case 0:
fragment = new FirstFragment();
fragment.setArguments(args);
fTransaction.replace(R.id.container, fragment,
"FTAG").commit();
break;
case 1:
fragment = new SecFragment();
fragment.setArguments(args);
fTransaction.replace(R.id.container, fragment,
"STAG").commit();
break;
case 2:
fragment = new ThirdFragment();
fragment.setArguments(args);
fTransaction.replace(R.id.container, fragment,
"TTAG").commit();
break;
}
}
Upvotes: 0