I need to replace fragment in view pager in an activity on a nework call success in another fragment. View pager is inside my dashboard activity. Here is get item method
#Override
public Fragment getItem(int pos) {
Fragment fragment = null;
switch (pos) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new SponsorshipOffersFragment();
break;
case 2:
if (mPreferenceManager.getConnectProfileStatus()) {
fragment = new CollaborationFragment();
} else {
fragment = new ConnectSettingFragment();
}
break;
case 3:
fragment = new ProtfolioFragment();
break;
case 4:
fragment = new SocialSummaryFragment();
break;
}
return fragment;
}
For case 2, first time getConnectProfileStatus will return false and ConnectSettingFragment will get call. After submitting profile from ConnectSettingFragment, on success I need to call CollaborationFragment.
Can someone help me to do ? I am calling below method from fragment
public void replaceFrament() {
mViewPager.setCurrentItem(2);
}
that is inside activity but nothing happening.
Related
I create a custom view pager like this:
public class TaskPagerAdapter extends FragmentStatePagerAdapter {
public TaskPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new NotificationFragment();
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
return fragment;
}
#Override
public int getCount() {
return 5;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
and used on main fragment :
mPagerAdapter = new TaskPagerAdapter(getFragmentManager());
mViewPager.setAdapter(mPagerAdapter);
mViewPager.setCurrentItem(-2);
in the setCurrentItem I have to which number to use until that i do not get an nullpoiner error?I want NotificationFragment() class is first start on view pager
Edit
whit mViewPager.setCurrentItem(-1); i got blow error:
Process: safarayaneh.com.anotify.debug, PID: 21484
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setMenuVisibility(boolean)' on a null object reference
at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:120)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1034)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1248)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
at android.view.View.measure(View.java:18788)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1028)...
Read setCurrentItem
Set the currently selected page. If the ViewPager has already been
through its first layout with its current adapter there will be a
smooth animated transition between the current item and the specified
item.
Problem
mViewPager.setCurrentItem(-1);
Don't set -1 or -2 . index starts from 0 .
EDIT
switch (position) {
case 0:
fragment = new NotificationFragment();
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
return fragment;
WHY 1,2,3,4 BLANK . You should try with
switch (position) {
case 0:
fragment = new NotificationFragment();
break;
}
return fragment;
mViewPager.setCurrentItem(0);
Since NotificationFragment has the first position.
It's index is 0.
Therefore, it should be something like mViewPager.setCurrentItem(0);
I have built an Android application that uses tabs to navigate. I am using fragments for the majority of the application, however I need to display one activity. How do i display an activity in case 2. Below is the code i have at the moment, i know it returns a fragment but how do i enable the use of both?
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
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).
System.out.println("current position = " + position);
Fragment fragment = null;
switch(position){
case 0:
return new diaryFragment();
case 1:
return new newEntryFragment();
case 2:
return new Calendarnew();
}
return null;
}
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new diaryFragment();
break;
case 1:
fragment = new newEntryFragment();
case 2:
fragment = new Calendarnew();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
Hi I dont actually get what you are up to but try reading this, maybe it could somehow solve your issue. https://stackoverflow.com/a/2958586/4470313
I am using navigation drawer in my android app and when i am reselecting fragment it loads twice.
Here is my code
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
// if(fraghome!=0)
// {
// fraghome=0;
fragment = new HomeFragment();
// }
break;
case 1:
fragment = new BlogsFragment();
break;
case 2:
fragment = new NewsFragment();
break;
case 3:
fragment = new TransferFragment();
break;
case 4:
fragment = new FixturesFragment();
break;
// case 5:
// fragment = new BestXIFragment();
// break;
case 5:
fragment = new FeedFragment();
break;
case 6:
fragment = new TwitterFragment();
break;
case 7:
fragment = new FacebookFragment();
break;
case 8:
fragment = new BookmarkFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
In each of fragment i am using async task and when i am selecting fragment the async task started again and again
please help me
Normally in fragment, fragmens are destroyed, if it is not the current top fragment or the fragment just right or left to the current top fragment. So the asynctask is getting called again and again.
I assume you are getting some data using the asynctask to show in the fragment. So try to make the data source variable static and check null for the data source variable before executing asynctask. if the data source is not null then show the data. I have added some abstruct code for your understanding. For better understanding you can see this link. Hope it helps. Thanks.....
In each fragment.....
public class TabFragment extends Fragment{
private static DataSource ds = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/*
.
other view initializing work
.
*/
if(ds == null){
//execute asynctask and set data to ds in asynctask
} else {
// set data to view.
}
return view;
}
}
I'm using the native Android drawer for my menu, here in MainActivity :
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
//Home
fragment = new EntryFragment();
break;
case 1:
//Journal
fragment = new JournalFragment();
break;
case 2:
// Medications
fragment = new RxFragment();
break;
case 3:
// Reminders
fragment = new ReminderFragment();
break;
case 4:
// Counselor Locator
fragment = new LocatorFragment();
break;
case 5:
// Library
fragment = new LibraryFragment();
break;
case 6:
// Settings
break;
case 7:
// About
break;
default:
break;
}
if (fragment != null) {
fm.beginTransaction()
.replace(R.id.container, fragment).addToBackStack(String.valueOf(position)).commit();
String fragmentTitles[] = getResources().getStringArray(R.array.menu_array);
if(mActionBar != null) {
mActionBar.setTitle(fragmentTitles[position]);
}
} else {
Log.e("MainActivity", "Error in creating fragment");
}
But I'm repurposing EntryFragment for another activity (EntryDetailActivity), so in EntryFragment I defined a couple of methods to modify the view based on which activity is hosting it:
// Runs on the home page, shows/hides appropriate views
public void prepareForHomeView() {
mTxtDate.setVisibility(View.GONE);
mBtnDiffDay.setVisibility(View.GONE);
mEdtNotes.setVisibility(View.GONE);
mBtnSave.setVisibility(View.GONE);
mBtnDelete.setVisibility(View.GONE);
}
// Runs on the detail page, shows/hides appropriate views
public void prepareForDetailView() {
mBtnLog.setVisibility(View.GONE);
}
In EntryDetailActivity, I can easily call prepareForDetailView() in onResumeFragments(), but I'm having an issue of where to call prepareForHome() in MainActivity, since I am swapping fragments in and out based on the menu. Any suggestions?
/** Update **/
I assigned the fragments a tag that is the position of the menu through addToBackstack(). I also added the following method to my MainActivity:
#Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
FragmentManager fm = getSupportFragmentManager();
EntryFragment entryFrag = (EntryFragment)fm.findFragmentByTag("0");
if(entryFrag != null) {
Log.v("rx", "Home frag is not null");
} else {
Log.v("rx", "Home frag is null");
}
}
Activity (or FragmentActivity) has onAttachFragment(Fragment fragment) - you could do an instanceof on fragment to determine if it is the correct type of fragment (or use the id, tag, etc) and call the appropriate method.
Alternatively, you could have the Fragment check the getActivity() (in onCreateView / onViewCreated).
I'm using ViewPager in a fragment. It has 3 tabs
Here's the code of my fragmentOagerAdapter:
#Override
public Fragment getItem(final int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = Fragment.instantiate(this.context, ClassA.class.getName());
break;
case 1:
fragment = Fragment.instantiate(this.context, ClassB.class.getName());
break;
case 2:
fragment = Fragment.instantiate(this.context, ClassC.class.getName());
break;
}
return fragment;
}
#Override
public CharSequence getPageTitle(final int position) {
String res = "";
switch (position) {
case 0:
res = "nameA";
break;
case 1:
res = "nameB";
break;
case 2:
res = "nameC";
break;
}
return res;
}
#Override
public int getCount() {
return 3;
}
I'm adding a a button in my main fragment that will allow me to switch between a list display and a grid display.
So what I want is to have a dynamic fragment depending on some enum stored within the Preferences
I want to do this for the 1st tab (position 0)
So I do the following:
#Override
public Fragment getItem(final int position) {
Fragment fragment = null;
switch (position) {
case 0:
switch (Settings.getInstance().getProductDisplayMode()) {
case LARGE_GRID:
fragment = Fragment.instantiate(this.context, ClassALargeGrid.class.getName());
break;
case SMALL_GRID:
fragment = Fragment.instantiate(this.context, ClassASmallGrid.class.getName());
break;
case LIST:
default:
fragment = Fragment.instantiate(this.context, ClassA.class.getName());
break;
}
break;
case 1:
fragment = Fragment.instantiate(this.context, ClassB.class.getName());
break;
case 2:
fragment = Fragment.instantiate(this.context, ClassC.class.getName());
break;
}
return fragment;
}
...
So it works fine when first opening the activity, but I don't know how to switch the fragment displayed in the first tab dynamically.
I tried calling destroyItem() on the FragmentPagerAdapter but it doesn't help...
Any idea ?
I actually had this problem a while back. I wound up having to take the fragment out of the viewpager and then use the FragmentManager to swap out the fragments dynamically.
Then, when I wanted to switch to another "page," I wrote some custom animations to "slide in" a fragment that hosted a ViewPager
Not a pretty solution, but it worked.