I have a drawer navigation with:
private void DisplayView (int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
case 2:
...
From an activity I can throw that fragment (fragment2 for example)? With BeginTransaction?
Thanks
A Fragment cannot exist in it's own, eg. without an Activity. It cannot be fired up with an Intent like Activities do. You have to either create a new Activity to hold your new Fragment or replace the Fragment of your current Activity with the new one.
You cannot start fragment as an activity, they need to be added to activities.
Read more in the docs here
Also, see this
Related
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?
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;
}
}
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 .
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;
}
}
in my app i need to switch between fragments when different items on the navigation drawer are clicked. I created a new method DisplayFragment for it. Here is the code:
private void DisplayFragment(int position)
{
Fragment fragment = null;
switch (position){
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
case 2:
fragment = new Fragment3();
break;
case 3:
fragment = new Fragment4();
break;
if(fragment!= null)
this.getFragmentManager().beginTransaction().replace(R.id.frame_container,fragment()).commit();
}
it shows an error for "fragment element in above line as follows " wrong second argument type found android.support.v4.app.Fragment; required android.app.Fragment;"
i tried changing the import from android.support.v4.app.Fragment to android.app.Fragment; but it then shows an error for Fragment fragment = null statement. what am i doing wrong?
You need to get the support fragment manager to use android.support.v4.app.Fragment.
this.getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, fragment).commit();
so, getSupportFragmentManager() instead of getFragmentManager(). Also, you don't need to use this in this case.
I'm developing an android application, I have a problem with the changing of back button function. How i can "replace" the default action of this button ?
I would realize a simple function that allow the user to return to the previous fragment when back button is pressed, for example :
[Main Fragment] ----> [2nd Fragment]
[2nd Fragment] <---- [Main Fragment]
I have read some question like this, but i don't understand how to solve my problem.
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment(getApplicationContext());
break;
case 1:
fragment = new FindPeopleFragment();
break;
case 2:
fragment = new PhotosFragment();
break;
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
fragment = new WhatsHotFragment();
break;
default:
break;
}
This is the part of my code where i call the "new fragment".
When i start up my application a new fragment is created (HomeFragment), in this frag i create a new fragment (Fragment_Detail).
What that i want is return from Fragment_Detail to HomeFragment.
I hope I was explanatory
Just add the addToBackStack(null). It will automatically go to the previous fragment loaded in stack.
HomeFragment fragment = new HomeFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction()
.replace(R.id.content, fragment,TAG)
.commit();
Refer addToBackStack() and refer this answer.
Refer the docs
You should take a look at the example: http://developer.android.com/training/implementing-navigation/temporal.html#back-fragments
I am trying to save the state on the navigation drawer fragments as I toggle between different fragments within the navigation drawer. For Example: I start at Fragment A fire some events, then toggle to Fragment B. Then I want to see the same state of Fragment A when I toggle back to Fragment A from Fragment B.
I tried using the onSavedInstanceState(Bundle savedInstanceState) but it only gets called when the orientation changes in the fragment life cycle. A new fragment gets created whenever I toggle to a new fragment and I can't figure out how to save data from a fragment and reload it on another visit.
I don't want to use backstack() either because it removes all the fragments up to the fragment that I want to restore.
Below is how I call the fragments on the drawer toggle.
private void selectItem(int position) {
Fragment fragment;
String TAG;
switch (position) {
case 0:
fragment = new FragmntA();
TAG = "A";
break;
case 1:
fragment = new FragmentB();
TAG = "B";
break;
case 2:
fragment = new FragmentC();
TAG = "C";
break;
case 3:
fragment = new FragmentD();
TAG = "D";
break;
case 4:
fragment = new FragmentE();
TAG = "E";
break;
default:
fragment = new FragmentA();
TAG = "A";
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.content_frame, fragment, TAG);
ft.commit()
I don't know if there is any point in the fragment life cycle where I can save its state. Any help would be appreciated. Thanks.
To not loose the state of your fragments when switching from one to another you should do "new Fragment()" only once, and keep the instance in a global variable.
But this will not fix the rotation problem.
For the rotation problem, you should read this => http://blog.sqisland.com/2014/06/navigationdrawer-creates-fragment-twice.html
Not easy but I haven't found another way yet.
define fragment object as static in the class and in newInstance method only initialise is fragment is null otherwise just return the fragment.
This will solve your problem.
but for orientation change you will have to use on saveinstancestate method.
I am using ViewPager and i am creating Fragments dynamically inside getItem method of FragmentStatePagerAdapter as follows and i want to avoid recreation of Fragment inside this method so that the application does not crash.
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
Fragment fragment = new Fragment();
switch (position) {
case 0:
fragment = new CourseOverViewFragment();
break;
case 1:
fragment = new CourseSchedueListFragment();
break;
case 2:
fragment = new CourseSchedueListFragment();
break;
case 3:
fragment = new CourseNoteListFragment();
break;
case 4:
fragment = new ProjectListFragment();
break;
default:
Log.i(StudyManagerDataSource.LOG_TAG, "default");
break;
}
return fragment;
}
How ever i fond this stackoverflow answer usefull. But i am not able to assign tag
inside getItem method.
Thanks!