How to avoid recreation of Fragment inside getItem method of FragmentStatePagerAdapter - android

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!

Related

replace current fragment with new one

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.

How to change Back Button function in Android (Fragment)

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

FragmentPagerAdapter getItem() method to start at a different position

I'm making a very simple app using the ViewPager. I would like to be able to start at a custom position when the getItem method is called. Below is my current code.
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Fragment fragment1 = new Fragment1();
return fragment1;
case 1:
Fragment fragment2 = new Fragment2();
return fragment2;
case 2:
Fragment fragment3 = new Fragment3();
return fragment3;
default:
return null;
}
I was wondering how it would be possible to start at position 1 instead of 0 when the getItem method is called and then to be able to swipe to position 0 or 2 from position 1.
Why don't you simply set selected item to 1 after setting adapter?
mPager.setCurrentItem(1);

Replace Activity to Fragment

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

Adding fragments to my main activity

Ok so,I'm new to Android programing and I have a question. I have a swipeview similar to the one in the Play Store,and I want each tab to contain a different listview fragment. I have 2 fragments ,one called LP and other PL ,lets say. How to I edit this part of code so it uses my fragments ?
#Override
public Fragment getItem(int position) {
Fragment fragment = new Fragment();
switch (position) {
case 0:
return fragment = new Fragment1();
case 1:
return fragment = new Fragment2();
case 2:
return fragment = new Fragment3();
default:
break;
}
return fragment;
EDIT : So after listening to the advice given,it still doesent work. :-?? I have 2 separate java files for my fragments and it doesent seem to work unless i copy the classes and put them inside the main activity...I may be missing something ,so please feel free to point out any posibility
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Fragment fragment = new Fragment1();
return fragment;
case 1:
Fragment fragment = new Fragment2();
return fragment;
case 2:
ListFragment fragment = new Fragment3();//for list fragments
return fragment;
default:
break;
}
return fragment;
You could use something like this:
#Override
public Fragment getItem(int position) {
Fragment result;
switch (position) {
case 0:
result = new LPFragment();
case 1:
result = new PLFragment();
default:
result = new DefaultFragment();
}
return result
}
You just need to return an instance of whatever fragment you want for each case in the switch.
If the PL and LP classes extends from Fragment then it's fine (though you may have some need to cast them elsewhere in the code as the function returns Fragment and not your own class types)

Categories

Resources