first fragment not restoring edit box value after swapping 3 fragment in viewPager... i have 3 fragment inside viewpager and after clicking on 3rd fragment tab and swap back to 1st fragment .. edit text filling blank.. plz give some solutions.. code is here..
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_item_master, container, false);
viewPager = (ViewPager)v. findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout)v. findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
return v;
}
private void setupViewPager(ViewPager viewPager) {
// ViewPagerAdapter adapter = new ViewPagerAdapter(mActivity.getSupportFragmentManager());
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new ItemInfoFragment(), "Item Info");
adapter.addFragment(new ItemExtraInfoFragment(), "Extra Info");
adapter.addFragment(new OrderHistoryFragment(), "Order History");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
This is recommended only if you have less number of fragments. You can use yourViewPager.setOffscreenPageLimit(numberOfFragmentsInViewPager);What this does is it wont create a new fragment every time you swipe, thus data is persisted. the default value will be 1 if you won't set this property.
Related
I have to refresh the fragment when I swipe to another fragment from the Tab.
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
mBinding = DataBindingUtil.inflate(inflater, R.layout.manage_work_shop_act, container, false);
mBinding.layout.title.setText(getString(R.string.d_manage_my_staff));
mBinding.tabs.setupWithViewPager(mBinding.viewpager);
setupViewPager(mBinding.viewpager);
mBinding.layout.nav.setVisibility(View.GONE);
mBinding.layout.navMenu.setVisibility(View.VISIBLE);
mBinding.layout.navMenu.setOnClickListener(view -> openDrawer());
return mBinding.getRoot();
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
int limit = (adapter.getCount() > 1 ? adapter.getCount() - 1 : 1);
adapter.addFragment(new MechanicListFragment(), getString(R.string.d_manage_my_staff));
adapter.addFragment(new MechanicNewFragment(), getString(R.string.new_frag));
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(limit);
adapter.notifyDataSetChanged();
}
My Adapter is like this
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
I tried all the ways to refresh my fragment.
If I swipe the tab or click the another tab then I need to refresh the visible tab again.
In my current fragment did not refresh.
please help me to override from this issue.
Try using Fragment state pager adapter than fragment adapter when you have to refresh tabs in your adapdter.
FragmentPagerAdapter stores the previous data which is fetched from the adapter while FragmentStatePagerAdapter takes the new value from the adapter everytime it is executed.
There's onHiddenChanged method in the fragment.
Called when the hidden state (as returned by isHidden() of the
fragment has changed. Fragments start out not hidden; this will be
called whenever the fragment changes state from that.
override the onHiddenChanged method in each of your tab fragment and if is not hidden do the refreshing. For example:
class YourFragment extends Fragment {
#Override public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if (!hidden) {
//you can refresh here now
}
}
}
I'm using tablayout with ViewPager, I've 3 tabs and I'm using fragment for my tabs . The problem is ,when I go to tab 3 , the content of tab 1 is fully lost and it need to reload again ,I don't want something like this , I want to make them to save the data and it doesn't need to reload again .
this is my code :
private void setupViewPager(ViewPager viewPager) {
Bundle bl = getIntent().getExtras();
Bundle bundle = new Bundle();
bundle.putString("for", bl.getString("for"));
if (bl.getString("for").equals("catid"))
bundle.putString("catid", bl.getString("catid"));
Bundle bundle2 = new Bundle();
bundle2.putString("for", bl.getString("for"));
if (bl.getString("for").equals("catid"))
bundle2.putString("catid", bl.getString("catid"));
Bundle bundle3 = new Bundle();
bundle3.putString("for", bl.getString("for"));
if (bl.getString("for").equals("catid"))
bundle3.putString("catid", bl.getString("catid"));
FragmentList latest = new FragmentList();
bundle.putString("forw","latest");
latest.setArguments(bundle);
FragmentList topvisit = new FragmentList();
bundle2.putString("forw","topvisit");
topvisit.setArguments(bundle2);
FragmentList por_mabhas = new FragmentList();
bundle3.putString("forw","por_mabhas");
por_mabhas.setArguments(bundle3);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(por_mabhas, "پرمبحث ترین خبرها");
adapter.addFragment(topvisit, "پربازدیدترین اخبار");
adapter.addFragment(latest, "تازه ترین خبرها");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
private void declare() {
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
final int finalPos = 2;
tabLayout.post(new Runnable() {
#Override
public void run() {
int right = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(finalPos).getRight();
tabLayout.scrollTo(right,0);
tabLayout.getTabAt(finalPos).select();
}
});
}
How can I do so ?
use setOffscreenPageLimit(TOTAL_PAGE); If you have N pages, you can use this, so that it will keep all pages in memory.
viewpager.setOffscreenPageLimit(TOTAL_PAGE);
i want to know that how can i add some different data like an array to each fragments that are in TabLayout?
i mean that fragment1 show array1, fragment2 show array2 and fragment3 show array3
the below is my code for Tablayout:
public class PoshtibaniActivity extends AppCompatActivity {
private Toolbar toolbar;
private ViewPager mPager;
private SlidingTabLayout mTabs;
private TabLayout tabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_poshtibani);
toolbar = (Toolbar) findViewById(R.id.app_bar_empty);
//setSupportActionBar(toolbar);
toolbar.setTitle("پشتیبانی");
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mPager = (ViewPager) findViewById(R.id.pager);
setupViewPager(mPager);
mPager.setCurrentItem(2);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FragmentOne(), "پیامهای دریافتی");
adapter.addFragment(new FragmentTwo(), "پیامهای ارسالی");
adapter.addFragment(new FragmentThree(), "ارسال پیام");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
you should use SetArguement for each of your fragments.
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FragmentOne(), "پیامهای دریافتی",MY_ARRAY_1,MY_SEC_ARRAY_1);
adapter.addFragment(new FragmentTwo(), "پیامهای ارسالی",MY_ARRAY_2,MY_SEC_ARRAY_2);
adapter.addFragment(new FragmentThree(), "ارسال پیام",MY_ARRAY_3,MY_SEC_ARRAY_3);
viewPager.setAdapter(adapter);
}
then in your adapter :
public void addFragment(Fragment fragment, String title,Object data,ArrayList<MY_DATA_MODEL> array2) {
Bundle mBundle=new Bundle();
mBundle.put("dataArray",data);
mBundle.put("dataArray2",array2);
fragment.setArgument(mBundle);
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
Now in each of your fragments, you should override setArguments and get your data from bundle there.
Don't forget to make your data model parcelable if you want to pass them in bundle and intents.
view pager file
public class SamplePagerAdapter extends FragmentPagerAdapter {
String[] title = {
"stringA",
"stringB",
"stringC",
}
public SamplePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
/** Show a Fragment based on the position of the current screen */
if (position == 0) {
return FragmentA.newInstance(title);
}
else if(position==1)
{
return new FragmentB.newInstance(title);
}
else { return FragmentC.newInstance(title);}
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
Fragment file
public class FragmentA extends Fragment {
public static FragmentA newInstance(String [] s) {
FragmentA A = new FragmentA();
Bundle args = new Bundle();
args.putStringArrayList("elist",s);
A.setArgumentas(args);
return A;
}
}
public View onCreateView(LayoutInflater inflater,
ViewGroup containerObject,
Bundle savedInstanceState){
//here is your arguments
Bundle bundle=getArguments();
//here is your list array
String[] myStrings=bundle.getStringArray("elist");
}
public class FragmentB extends Fragment {
public static FragmentB newInstance(String [] s) {
FragmentB B = new FragmentB();
Bundle args = new Bundle();
args.putStringArrayList("elist",s);
B.setArgumentas(args);
return B;
}
}
public View onCreateView(LayoutInflater inflater,
ViewGroup containerObject,
Bundle savedInstanceState){
//here is your arguments
Bundle bundle=getArguments();
//here is your list array
String[] myStrings=bundle.getStringArray("elist");
}
public class FragmentC extends Fragment {
public static FragmentC newInstance(String [] s) {
FragmentC C = new FragmentC();
Bundle args = new Bundle();
args.putStringArrayList("elist",s);
C.setArgumentas(args);
return C;
}
}
public View onCreateView(LayoutInflater inflater,
ViewGroup containerObject,
Bundle savedInstanceState){
//here is your arguments
Bundle bundle=getArguments();
//here is your list array
String[] myStrings=bundle.getStringArray("elist");
}
Following the tutorial from here
I have implemented the same in activity It is working perfectly but when I tried adding tabs inside a fragment,swiping tabs is not showing fragment's view.
public class DetailFragment extends android.support.v4.app.Fragment implements LoaderManager.LoaderCallbacks<Cursor>,View.OnClickListener {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_detail_start, container, false);
viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "ONE");
adapter.addFragment(new TwoFragment(), "TWO");
adapter.addFragment(new ThreeFragment(), "THREE");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
} // Update - code formatting
Please suggest.
To implement the View pager within a fragment use getChildFragmentManager() instead of getFragmentManager().
So while Initializing the ViewPagerAdapter for ViewPager use "getChildFragmentManager()".
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
I have one activity named rootActivity , in which i have a rootContainer in which i replace the fragments.
Now, the first fragment that i replace is DashboardFragment.
I now click on the cross to open a new fragment like this...
Now when i press the back button the content of dashboard fragment is lost.
Can someone help me
Here's a video to show what's really happening.
https://youtu.be/NobqYUCEZKY
Code for the DashboardFragment(contains the fragments inside tabs)
public class DashboardFragment extends Fragment {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private View rootView;
/**
*
* #param config : For setting up stuff by the given config
* #return
*/
public static DashboardFragment newInstance(Bundle config) {
DashboardFragment fragment = new DashboardFragment();
fragment.setArguments(config);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.activity_dashboard, container, false);
toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
((RootActivity)getActivity()).setSupportActionBar(toolbar);
viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
return rootView;
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(android.R.drawable.btn_star_big_off);
tabLayout.getTabAt(1).setIcon(android.R.drawable.btn_star_big_on);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());
adapter.addFragment(new TrackeesFragment(), "Trackee");
adapter.addFragment(new TrackersFragment(), "Trackers");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
return super.instantiateItem(container, position);
}
#Override
public CharSequence getPageTitle(int position) {
return null;
}
}
}
try by changing the following line in
setupViewPager(ViewPager viewPager) {}
Change
ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());
To
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());