I have four tabs in navigation tab, and each tab has a fragment:
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch (position){
case 0:
return new PostJob();
case 1:{
return new PostedJobHistory();
}
case 2:{
return new SearchCandidates();
}
case 3:{
return new EmployerSettings();
}
default:
break;
}
return PlaceholderFragment.newInstance(position + 1);
}
I enter job details in first tab - PostJob fragment. and want to get it updated in the second tab when I go to Posted Job history fragment.
How can I achieve this.
In tab 1 I enter details in database
and in tab 2 i read database to show posted job history, but posted job history is not updated.
use this
List<Fragment> fragmentsList = getSupportFragmentManager().getFragments();
if (fragmentsList == null || fragmentsList.size() == 0) {
return;
}
for (Fragment fragment : fragmentsList) {
if (fragment instanceof PostedJobHistory) {
((PostedJobHistory) fragment).yourUpdateMethod();
}
}
Related
So for some reason i'm getting a wrong position when i switch tabs on my tabbed activity.
At the bottom of my post you can see my logcat when i start my app, tab 1 and tab 2 their oncreateview is called.
And position 0 switches to 1 even though i'm on tab 1 which must be position 0.
So my problem is that when i change a tab, the wrong data is shown and wrong logcat info.
for example i change to tab 2, logcat shows oncreateview i called from tab3 ?
How can i fix this?
Thanks,
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private int numOfTabs;
SectionsPagerAdapter(FragmentManager fm, int numOfTabs) {
super(fm);
this.numOfTabs = numOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
Log.e(TAG, "Pos: "+ position);
return new Tab1();
case 1:
Log.e(TAG, "Pos: "+ position);
return new Tab2();
case 2:
Log.e(TAG, "Pos: "+ position);
return new Tab3();
case 3:
Log.e(TAG, "Pos: "+ position);
return new Tab4();
case 4:
Log.e(TAG, "Pos: "+ position);
return new Tab5();
default:
return null;
}
}
#Override
public int getCount() {
// Get total pages
return numOfTabs;
}
}
Logcat
.activities.MainActivity: Pos: 0
.activities.MainActivity: Pos: 1
.fragments.Tab1: onCreateView called!
.fragments.Tab2: onCreateView called!
ViewPager by default loads 2 tabs. So getItem gets called twice when you first open the app. When you go to tab2, ViewPager loads tab3 in advance so that the scroll will be smooth. See offscreenPageLimit.
i have an activity with a navigation drawer built using the library https://github.com/mikepenz/MaterialDrawer
I want to load 4 different pages from selecting 4 different items from the navigation drawer.
I decided to use fragments for each item in the navigation drawer, so when any item is pressed, a new fragment for that option is created and shown to the user.
What i want is that, regardless of which fragment the user is currently in, or which fragment he came from, whenever he presses the back button, he is returned to the original activity.
This is the code i have so far, but apparently the Fragment Backstack is not being updated.
result = new DrawerBuilder().withSelectedItem(-1)
.withActivity(this)
.withToolbar(toolbar)
.withAccountHeader(headerResult)
.addDrawerItems(
item1,item2,item3,item4
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
result.setSelection(position,false);
clearStack();
switch (position) {
case 1:
fragment = new admin_fragment_data_entry();
break;
case 2:
fragment = new admin_fragment_data_edit();
break;
case 3:
break;
case 4:
break;
}
initiateFragment();
return false;
}
}
)
.build();
#Override
public void onBackPressed() {
int count = getFragmentManager().getBackStackEntryCount();
//handle the back press :D close the drawer first and if the drawer is closed close the activity
if (result != null && result.isDrawerOpen()) {
result.closeDrawer();
}
else if (count == 0) {
super.onBackPressed();
//additional code
}
else {
getFragmentManager().popBackStack();
}
}
private void initiateFragment(){
if (fragment != null) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.addToBackStack(null);
ft.replace(R.id.container_body, fragment);
ft.commit();
}
}
private void clearStack(){
int count = getFragmentManager().getBackStackEntryCount();
while(count > 0){
getFragmentManager().popBackStack();
count--;
}
}
Any help would be highly appreciated.
Also, should i have used fragments here? Or i should have just started 4 different activities for 4 different options from navigation drawer?
Are you using the correct fragment manager to get your backstack count?
I notice in other parts of your class you're using getSupportFragmentManager instead.
I am trying to show multiple fragments in a FrameLayout container. The basic idea is to use a spinner to select a different fragment to be displayed in the container. I am trying to add them programmatically and for this I have created one xml file for each day of the week which is what I am trying to show.
// Set up the spinner to select days of the week.
Spinner spinner = (Spinner) weekView.findViewById(R.id.day_selecter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// For each day of the week, create a new instance of Switches
// get an instance of FragmentTransaction from your Activity
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
Fragment frag = fm.findFragmentByTag(Integer.toString(pos));
if(frag == null) {
switch(pos) {
case 0: frag = new mondayFragment();
break;
case 1: frag = new tuesdayFragment();
break;
case 2: frag = new wednesdayFragment();
break;
case 3: frag = new thursdayFragment();
break;
case 4: frag = new fridayFragment();
break;
case 5: frag = new saturdayFragment();
break;
case 6: frag = new sundayFragment();
break;
}
fragmentTransaction.add(R.id.week_program_switches, frag, Integer.toString(pos));
}
else
fragmentTransaction.replace(R.id.week_program_switches, frag, Integer.toString(pos));
fragmentTransaction.commit();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
The program manages to load the fragment corresponding to the first day of the week, i.e. the mondayFragment into the container. Each fragment contains textboxes and checkboxes so i need to save the view of each fragment loaded. At times the app would allow me to select a different day but the content from the previous fragment gets carried over and it eventually crashes the app with an IllegalStateException error: Day has already been added to fragment transaction. other times, simply selecting a different day in the spinner immediately crashes the app with the same error. I really need your help in this as I'm all out of ideas.
Few Notes:
1. I have not added any fragment statically into any layout. I have seen this to be the cause of many related issues.
2. I originally intended to use a single fragment and create multiple instances in a frameLayout container but i had similar issues of saving the content in each instance.
I have a problem with the FragmentPagerAdapter .
I can not save the state of the Fragment and then there is the view that within the Fragment . Whenever I use the swipe left and right , the Fragment is recreated by overriding the method getItem ( int position ) in the static class that extends the FragmentPagerAdapter .
public static class GraphicsCollectionPagerAdapter extends FragmentPagerAdapter {
final int NUM_ITEMS = 3; // number of tabs
public GraphicsCollectionPagerAdapter(FragmentManager fm) {
super(fm);
fragmentList = new AnalyzeFragmentPageListWithDate();
fragment1 = new AnalyzeFragmentPage1();
fragment2 = new AnalyzeFragmentPage2();
}
#Override
public Fragment getItem(int position) {
//Log.i(TAG, "getItem() -> New fragment at position " + position);
switch (position) {
case 0:
return fragmentList;
case 1:
return fragment1;
case 2:
return fragment2;
}
return null;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Fragm1";
case 1:
return "Fragm2";
case 2:
return "Fragm3";
}
return "OBJECT " + (position + 1);
}
}
Within the method OnCreateView of each instance of the Fragment there are several steps to the SQLite database and this causes a saturation of the Database Connection Pool.
The warning found whenever change dynamically fragment is: "W / SQLiteConnectionPool (1111 ) : A SQLiteConnection object for database ' / data / data / com.myapp / databases / mydb ' was leaked ! Please fix your application to end transactions in progress properly and to close the database When it is no longer needed . "
I already tried to use the FragmentStatePagerAdapter without success.
Could you kindly tell me how to proceed ? I do not want the Fragment is regenerated each time, causing problems to the database. Have you got an example for save Fragment/View sate?
I have not found any suggestion for now .
thank you very much
use setOffscreenPageLimit property for your ViewPager object.
ViewPager pager = (ViewPager) findViewById(R.id.viewPagerId);
pager.setOffscreenPageLimit(2);
First of all create a new instance of Fragment every time the getItem() is called
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentList();
case 1:
return new AnalyzeFragmentPage1();
case 2:
return new AnalyzeFragmentPage2();
}
return null;
}
Note that FragmentPagerAdapter does not work exactly like normal Adapter meaning it does NOT use an empty vessel Fragment and then refills it with different data.
Instead it creates a new Fragment whenever it is needed. So you should pass data (if any) when the Fragment is created. Please study the example in the Android docs.
In a real time projects instead of passing current position to a new Fragment you could pass the actual ID of the entry that a Fragment should refer to.
I am using android DrawerLayout from Support Library to show slide menu.I am using a single activity and 5-6 fragments to show them upon selection in DrawerLayout menu.But I have a small problem which is "How do I check which fragment is currently visible so if user selected the menu item which corresponds to already opened fragment. Currently it creating the Fragment again and displaying it which is not good.The function that triggers when clicked on menu item is:
private void selectItem(int position) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Locate Position
switch (position) {
case 0:
ft.replace(R.id.content_frame, fragment1);
break;
case 1:
ft.replace(R.id.content_frame, fragment2);
break;
case 2:
ft.replace(R.id.content_frame, fragment3);
break;
}
ft.commit();
mDrawerList.setItemChecked(position, true);
// Close drawer
mDrawerLayout.closeDrawer(mDrawerList);
}
How do I check if the requested fragment is already opened so not to create it again?Is their any method to check this through FragmentManager?
I would add to #Plato's answer.
Check get currently displayed fragment question if you haven't already.
The answer says that when you add the fragment in your transaction, you can use a tag to represent a particular fragment. Something like:
ft.replace(android.R.id.content, fragment, "MY_FRAGMENT");
Later if you want to check if that fragment is visible, you can do something like:
RequestedFragment fragment = (RequestedFragment)getFragmentManager().findFragmentByTag("MY_FRAGMENT"); //"My_FRAGMENT" is its tag
if (fragment.isVisible()) {
// add your code here
}
Hope this helps.
the simplest approach would be to store last used position and compare next time. Just make your selectItem method like:
int lastPosition = -1;
private void selectItem(int position) {
if( position != lastPosition ) {
[ old method body here ]
lastPostion = position;
}
}
Assign different tags to each one of your fragments and call findFragmentByTag() of the FragmentManager to check if each one is already present.
You can also use the findFragmentById() of the FragmentManager for the same purpose and use the ids of your fragments.
If you are absolutely sure that each of fragment1, fragment2, fragment3 cannot be visible at the same time and if you reuse the same fragments (i.e. do not create a new fragment each time you change position) then the method isVisible() from the class Fragment should tell you if that fragment is currently visible.
Your switch should look like:
switch (position) {
case 0:
if(!fragment1.isVisible()) {
ft.replace(R.id.content_frame, fragment1);
}
break;
case 1:
if(!fragment2.isVisible()) {
ft.replace(R.id.content_frame, fragment2);
}
break;
case 2:
if(!fragment3.isVisible()) {
ft.replace(R.id.content_frame, fragment3);
}
break;
}