send data from Activity to Tab view fragment - android

How can i send data from Activity to tab fragment when FloatingActionButton is clicked OR where do get it wrong and how can i fix it,I know it has been asked number of times and i tries all solution include this this and this which gave clear explanation but still work for more that 20hr of working i got no error and nothing works.
//float button in main activity
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String MyValue=" Hi fragment";
Bundle bundle = new Bundle();
bundle.putString("value", MyValue);
// set Fragmentclass Arguments
TabFragment1 fragobj = new TabFragment1();
fragobj.setArguments(bundle);
}
});
//In fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.tab_fragment_1, container, false);
TextView textView= (TextView)view.findViewById(R.id.textView) ;
if (getArguments() != null) {
String MyValue = getArguments().getString("value");
textView.setText(MyValue);
}
return view;
}
//// here is the adapter
public class PagerAdapter extends FragmentStatePagerAdapter {
private final SparseArray<WeakReference<Fragment>>
instantiatedFragments = new SparseArray<>();
private ArrayList<String> mTabHeader;
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabFragment1 tab1 = new TabFragment1();
return tab1;
case 1:
TabFragment2 tab2 = new TabFragment2();
return tab2;
case 2:
TabFragment3 tab3 = new TabFragment3();
return tab3;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
#Override
public Object instantiateItem(final ViewGroup container, final int position) {
final Fragment fragment = (Fragment) super.instantiateItem(container, position);
instantiatedFragments.put(position, new WeakReference<>(fragment));
return fragment;
}
#Override
public void destroyItem(final ViewGroup container, final int position, final Object object) {
instantiatedFragments.remove(position);
super.destroyItem(container, position, object);
}
#Nullable
public Fragment getFragment(final int position) {
final WeakReference<Fragment> wr = instantiatedFragments.get(position);
if (wr != null) {
return wr.get();
} else {
return null;
}
}
#Override
public CharSequence getPageTitle(int position) {
return mTabHeader.get(position);
}
}

First, i assume that you want to update the fragment whenever you press the fab, even if your fragment is already on screen. your adapter is returning a new fragment each time, you should instead hold the fragments instance and return the fragment for the given position stored in your array.
Check this generic FragmentStatePagerAdapter:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private List<Fragment> mFragmentList = new ArrayList<>();
private List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#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);
}
public void addFragment(Fragment fragment) {
mFragmentList.add(fragment);
}
#Override
public CharSequence getPageTitle(int position) {
if (!mFragmentTitleList.isEmpty()) {
return mFragmentTitleList.get(position);
}
return "";
}
}
Then on your fragment create a method to update your info:
public void updateData(String string){
miTextView.setText(string);
}
And finally on your activity do something like this:
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
YourFragment fragment = (YourFragment) adapter.getItem(desiredPosition);
fragment.updateData("your string");
}
});

Related

Interface in fragment in ViewPager.OnPageChangeListener

In my viewpager i set listener from which i call methods from my interface in tab fragments.
But variable in this tab fragments in my interfaces methods always return null.
Tell me why ?
p.s. sorry for my english)
Parent fragment:
public class MyFragment
extends Fragment {
protected Context mContext;
#Override
public void onAttach(Context context) {
super.onAttach(context);
mContext = context;
}
#Override
public void onDetach() {
super.onDetach();
mContext = null;
} }
Container fragment with PagerAdapter:
public class FragmentContainer
extends MyFragment {
private ViewPager viewPager;
private ViewPager.OnPageChangeListener onPageChangeListener;
public interface IPageSelected {
void onPageSelected();
void onPageUnselected();
}
public static FragmentContainer newInstance() {
return new FragmentContainer();
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(
R.layout.fragment_container, container, false);
viewPager = view.findViewById(R.id.pager);
PagerAdapter pagerAdapter = new PagerAdapter(getFragmentManager());
viewPager.setAdapter(pagerAdapter);
TabLayout tabLayout = view.findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
onPageChangeListener = new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
for(int x = 0; x < pagerAdapter.getCount(); x++) {
((IPageSelected) pagerAdapter.getItem(x)).onPageUnselected();
}
((IPageSelected) pagerAdapter.getItem(position)).onPageSelected();
}
#Override
public void onPageScrollStateChanged(int state) {
}
};
viewPager.addOnPageChangeListener(onPageChangeListener);
return view;
}
#Override
public void onResume() {
super.onResume();
setCurrentItem();
}
private void setCurrentItem() {
viewPager.post(() -> {
viewPager.setCurrentItem(0);
onPageChangeListener.onPageSelected(0);
});
}
class PagerAdapter
extends FragmentStatePagerAdapter {
PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return TestFragment1.newInstance();
case 1:
return TestFragment2.newInstance();
}
return null;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Tab1";
case 1:
return "Tab2";
default:
return null;
}
}
} }
Tab fragments.
Question: why variable mContext in interface methods always return null ?
public class TestFragment1
extends MyFragment
implements FragmentContainer.IPageSelected {
public static TestFragment1 newInstance() {
return new TestFragment1();
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
#Override
public void onPageSelected() {
Log.d("myLog", "TestFragment1 onPageSelected: " + mContext); // WHY NULL ???
}
#Override
public void onPageUnselected() {
Log.d("myLog", "TestFragment1 onPageUnselected: " + mContext); // WHY NULL ???
} }
public class TestFragment2
extends MyFragment
implements FragmentContainer.IPageSelected {
public static TestFragment2 newInstance() {
return new TestFragment2();
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, container, false);
}
#Override
public void onPageSelected() {
Log.d("myLog", "TestFragment2 onPageSelected: " + mContext); // WHY NULL ???
}
#Override
public void onPageUnselected() {
Log.d("myLog", "TestFragment2 onPageUnselected: " + mContext); // WHY NULL ???
} }
In your fragment container :
PagerAdapter pagerAdapter = new PagerAdapter(getFragmentManager()); here is the fault.
In order to attach your child fragments to your container fragment, consider passing child fragment manager getChildFragmentManager() to your viewpager adapter. (Right now your fragments are attached to same level as your container fragment).
I found solution.
Adapter class.
Old:
class PagerAdapter
extends FragmentStatePagerAdapter {
PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return TestFragment1.newInstance();
case 1:
return TestFragment2.newInstance();
}
return null;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Tab1";
case 1:
return "Tab2";
default:
return null;
}
}
}
New:
class PagerAdapter
extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
Adapter initialization.
Old:
PagerAdapter pagerAdapter = new PagerAdapter(getFragmentManager());
viewPager.setAdapter(pagerAdapter);
New:
PagerAdapter pagerAdapter = new PagerAdapter(getFragmentManager());
pagerAdapter.addFragment(TestFragment1.newInstance(), "tab1");
pagerAdapter.addFragment(TestFragment2.newInstance(), "tab2");
viewPager.setAdapter(pagerAdapter);
And now new question: what's the differense with the first option ?
Now i initializing adapter in container fragment, it's a cause ?
Why didn't work when initializing was in the adapter ?

Back doesn't work in inner fragment

There is an activity in which there is a fragment, let's call it FirstFragment. There is a framelayout in this fragment containing another fragment, SecondFragment. By clicking on SecondFragment, ThirdFragment is fired. Here, I need to be able to go back to the SecondFragment when pressing back button. However, by clicking on back button, the activity is closed. Here's my code :
MainActivity.java
public class MainActivity extends AppCompatActivity{
ViewPager viewPager;
String[] titles;
SecondFragment secondFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentPagerAdapter fragment = new FragmentPagerAdapter(getSupportFragmentManager());
viewPager = findViewById(R.id.viewPager);
viewPager.setAdapter(fragment);
firstFragment = new FirstFragment();
}
public class FragmentPagerAdapter extends android.support.v4.app.FragmentPagerAdapter {
public FragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0 : return FirstFragment.newInstance("hi");
default: return new OtherFragment();
}
}
#Override
public int getCount() {
return 3;
}
}
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
}
FirstFragment.java
public class FirstFragment extends Fragment implements RecyclerViewListener {
public FirstFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Fragment secondFragment =SecondFragment.newInstance("hi");
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.frameLayout, secondFragment).commit();
return inflater.inflate(R.layout.fragment_first, container, false);
}
public static FirstFragment newInstance(String text) {
FirstFragment f = new FirstFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
#Override
public void clicked(int position) {
Fragment productDetailFragment = ThirdFragment.newInstance(position);
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.frameLayout, productDetailFragment).addToBackStack(null).commit();
Toast.makeText(getContext(), "pos : " + position, Toast.LENGTH_SHORT).show();
}
#Override
public boolean longClicked(int position) {
return false;
}
}
SecondFragment.java
public class SecondFragment extends Fragment {
RecyclerView recyclerView;
static ArrayList<Item> items;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_second, container, false);
recyclerView = v.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
items = new ArrayList<>();
for (int i = 0; i < 100; i++) {
Item item = new Item(R.drawable.accept_state, "esm" + i, Integer.toString(i * 100));
items.add(item);
}
recyclerView.setAdapter(new RecylcerViewAdapter(getContext(), (RecyclerViewListener) getParentFragment(), items));
return v;
}
public static SecondFragment newInstance(String text) {
SecondFragment f = new SecondFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
}
ThirdFragment.java
public class ThirdFragment extends android.support.v4.app.Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_third, container, false);
return v;
}
public static ThirdFragment newInstance(int position) {
ThirdFragment f = new ThirdFragment();
Bundle b = new Bundle();
f.setArguments(b);
return f;
}
}
RecyclerViewListener.java
public interface RecyclerViewListener {
void clicked(int position);
boolean longClicked(int position);
}
RecyclerViewAdapter.java
public class RecylcerViewAdapter extends RecyclerView.Adapter<RecylcerViewAdapter.RecyclerViewHolder>{
private Context context;
private RecyclerViewListener recyclerViewListener;
private ArrayList<Item> items;
public RecylcerViewAdapter(Context context, RecyclerViewListener recyclerViewListener, ArrayList<Item> items) {
this.context = context;
this.recyclerViewListener = recyclerViewListener;
this.items = items;
}
#Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.recycler_item, parent, false);
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
return recyclerViewHolder;
}
#Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
holder.bind(position);
}
#Override
public int getItemCount() {
return items.size();
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView name;
TextView cost;
public RecyclerViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
recyclerViewListener.clicked(getAdapterPosition());
}
});
itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
return recyclerViewListener.longClicked(getAdapterPosition());
}
});
imageView = itemView.findViewById(R.id.image);
name = itemView.findViewById(R.id.name);
cost = itemView.findViewById(R.id.cost);
}
public void bind(int position) {
Item item = items.get(position);
imageView.setImageResource(item.getImageID());
name.setText(item.getName());
cost.setText(item.getCost());
}
}
}
First of all, override onBackPressed() and then use use getChildFragmentManager() as shown below:
public void onBackPressed() {
// If the fragment exists and has some back-stack entry
if (myFragment != null && mytFragment.getChildFragmentManager().getBackStackEntryCount() > 0){
// Get the fragment fragment manager - and pop the backstack
myFragment.getChildFragmentManager().popBackStack();
}
// Else, nothing in the direct fragment back stack
else{
// Let super handle the back press
super.onBackPressed();
}
}

FragmentPagerAdapter crash

I try to build viewPager with several fragments.
But when I launch app I get crash with text
Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
my code is following:
public static class MyPagerAdapter extends FragmentPagerAdapter {
private static int NUM_ITEMS = 4;
MainFragment main;
CartFragment cart;
public MyPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
main = new MainFragment();
cart = new CartFragment();
}
// Returns total number of pages
#Override
public int getCount() {
return NUM_ITEMS;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return main;
case 1:
return null;
case 2:
return null;
case 3:
return cart;
default:
return null;
}
}
}
Use below code for calling Fragments...
#Override
public android.support.v4.app.Fragment getItem(int position) {
Fragment f = new Fragment();
switch (position) {
case 0:
f = new LogoFragment();
break;
case 1:
f = new AboutUsFragment();
break;
case 2:
f = new ServicesFragment();
break;
}
return f;
}
this is my way to use it :
public class ViewPagerAdapter extends FragmentPagerAdapter {
ArrayList<PagerFragments> mFragments;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
this.mFragments = new ArrayList<>();
}
public void addFragment(Fragment fragment,String title) {
PagerFragments t = new PagerFragments();
t.setTitle(title);
t.setFragment(fragment);
mFragments.add(t);
notifyDataSetChanged();
}
public void addFragment(Fragment fragment) {
PagerFragments t = new PagerFragments();
t.setFragment(fragment);
mFragments.add(t);
notifyDataSetChanged();
}
//This method return the fragment for the every position in the View Pager
#Override
public Fragment getItem(int position) {
return mFragments.get(position).getFragment();
}
// This method return the titles for the Tabs in the Tab Strip
#Override
public CharSequence getPageTitle(int position) {
return mFragments.get(position).getTitle();
}
// This method return the Number of tabs for the tabs Strip
#Override
public int getCount() {
return mFragments.size();
}
}
PagerFragment.java :
import android.support.v4.app.Fragment;
public class PagerFragments {
Fragment fragment;
String title;
public Fragment getFragment() {
return fragment;
}
public void setFragment(Fragment fragment) {
this.fragment = fragment;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
Then in fragment or activity :
private ViewPagerAdapter viewPagerAdapter;
viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
viewPagerAdapter.addFragment(new RTopGainLossFragment(oneDay), "1D");
viewPagerAdapter.addFragment(new RTopGainLossFragment(oneWeek), "1W");
viewPagerAdapter.addFragment(new RTopGainLossFragment(oneMonth), "1M");
viewPagerAdapter.addFragment(new RTopGainLossFragment(oneYear), "YTD");
viewPagerAdapter.addFragment(new RTopGainLossFragment(ytd), "1Y");
viewPagerAdapter.addFragment(new RTopGainLossFragment(max), "MAX");
viewpager.setAdapter(viewPagerAdapter);
tab.setupWithViewPager(viewpager);

Catch the changing of slide in a ViewPager

I have implemented a ViewPager in order to swipe between two Fragments like such:
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.statistics_general_vp, container, false);
mViewPager = (ViewPager) view.findViewById(R.id.pager);
mStatisticsPagerAdapter = new StatisticsPagerAdapter(getActivity().getSupportFragmentManager());
mViewPager.setAdapter(mStatisticsPagerAdapter);
return view;
}
public class StatisticsPagerAdapter extends FragmentStatePagerAdapter {
public StatisticsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
return new GeneralStatistics();
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
But for now my two slides display the same Fragment.
How may I have slide 1 display Fragment 1 and slide 2 Fragment 2?
have u created fragment1 and fragment 2 seperately?? if yes then do this - in activity holding these two fragments.
private void createViewPager(ViewPager viewPager) {
Log.d("activity", "viewpager");
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new Fragment1(), "");
adapter.addFrag(new Fragment2(), "");
viewPager.setAdapter(adapter);
}
create fragment like this -
public class Fragment1 extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.transaction_layout, container, false);
return v;
}
}
create viewpager like this -
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 addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
or if you want to make tab with icons use this method -
private void createTabIcons() {
Log.d("activity", "createtabicons");
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.tab_item2, null);
tabOne.setText("QR Scan");
tabOne.setTypeface(MRR);
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.image1, 0, 0);
tabOne.setCompoundDrawablePadding(5);
tabLayout.getTabAt(0).setCustomView(tabOne);
tabLayout.getTabAt(0).getCustomView().setSelected(true);
TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.tab_item2, null);
tabTwo.setText("Mobile No.");
tabTwo.setTypeface(MRR);
tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.image3, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);
tabTwo.setCompoundDrawablePadding(5);
}
Hello i show you what i doing, i got 2 fragments in mainActivity
private void addFragments() {
List<Fragment> fragments = new ArrayList<>();
chatFragment = new ChatFragment();
remoteFragment = new RemoteFragment();
fragments.add(remoteFragment);
fragments.add(chatFragment);
MyPageAdapter adapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(TAB_REMOTE_POSITION)
.setIcon(getDrawable(R.drawable.ic_settings_remote_white_24dp));
tabLayout.getTabAt(TAB_SOCIAL_POSITION)
.setIcon(getDrawable(R.drawable.ic_group_white_24dp));
}
Here is my adapter:
class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
#Override
public int getCount() {
return this.fragments.size();
}
}
and on OnCreate u should
addFragments();
also i bind the views with ButterKnife
#Bind(R.id.view_pager)
ViewPager viewPager;
#Bind(R.id.tab_layout)
TabLayout tabLayout;
but u can static implement this with android
And remember that u have listeners:
private void pageChangeListener() {
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
View view = getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getSystemService
(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
});
}

Viewpager inside a fragment is blank when a listiview item in it is clicked and redirected to another fragment

I am using a Viewpager inside a fragment and the viewpager is having a listview within its fragment.So when a list item is clicked it is redirected to another fragment in the same activity. So when i come back to my previous fragment(viewpager) the viewpager is blank! why is it so
I am desperate for the solution. Any suggestion would be appreciated.
//tabs inside the fragment
public class TabsPagerAdapter extends FragmentStatePagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
/* #Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
return PageFragment.newInstance(position + 1);
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
return "TAB " + (position + 1);
}*/
#Override
public Fragment getItem(int position) {
Fragment frag = null;
switch (position) {
case 0:
frag = new Full_Roaster_Fragment();
break;
case 1:
frag = new By_Sport_Fragment();
break;
case 2:
frag = new By_Team_Fragment();
break;
case 3:
frag = new Video_Critique_Fragment();
break;
}
return frag;
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
String title=" ";
switch (position){
case 0:
title="Full Roaster";
break;
case 1:
title="By Sport";
break;
case 2:
title="By Team";
break;
case 3:
title="For Video Critique";
break;
}
return title;
}
}
//Child Fragment of Viewpager
public class Full_Roaster_Fragment extends Fragment implements AdapterView.OnItemClickListener {
private View rootView;
public static ListView Analystslist;
public static FullRoasterAdapter full_Roaster_adapter;
public static ArrayList<ics.com.ics_app.Beans.AnalystSearchBean.Teams> analysts_search_arraylist1 = new ArrayList<>();
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_full_roaster, container, false);
initialization();
full_Roaster_adapter = new FullRoasterAdapter(getActivity(), ApplicationGlobalClass.analysts_search_arraylist);
full_Roaster_adapter.notifyDataSetChanged();
Analystslist.setAdapter(full_Roaster_adapter);
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
full_Roaster_adapter = new FullRoasterAdapter(getActivity(), ApplicationGlobalClass.analysts_search_arraylist);
full_Roaster_adapter.notifyDataSetChanged();
Analystslist.setAdapter(full_Roaster_adapter);
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
Toast.makeText(getActivity(), "Full Roaster on Activity Created", Toast.LENGTH_SHORT).show();
// Populates list with our static array
full_Roaster_adapter = new FullRoasterAdapter(getActivity(), ApplicationGlobalClass.analysts_search_arraylist);
full_Roaster_adapter.notifyDataSetChanged();
Analystslist.setAdapter(full_Roaster_adapter);
Log.e("sizeee",String.valueOf(ApplicationGlobalClass.analysts_search_arraylist.size()));
}
private void initialization() {
Analystslist = (ListView)rootView.findViewById(R.id.Analysts_listview);
Analystslist.setOnItemClickListener(this);
}
#Override
public void onResume() {
super.onResume();
// if (ApplicationGlobalClass.flag) {
Toast.makeText(getActivity(), "full Roaster on Resume", Toast.LENGTH_SHORT).show();
full_Roaster_adapter = new
..
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ApplicationGlobalClass.user_id=ApplicationGlobalClass.analysts_search_arraylist.get(i).get(i).getId();
Log.e("userid",String.valueOf(ApplicationGlobalClass.user_id));
Log.e("userid2",String.valueOf(ApplicationGlobalClass.analysts_search_arraylist.get(i).get(i).getId()));
//here new fragment is calling from this fragment
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new FlexibleSpaceWithImageWithViewPagerTab2Activity()).addToBackStack("profile1").commit();
}
You need to override onBackPressed() in the activity that's hosting the fragments like this:
public void onBackPressed() {
int = count = getFragmentManager().getBackStackEntryCount();
if (count == 0) {
super.onBackPressed();
//additional code
} else {
getFragmentManager().popBackStack();
}

Categories

Resources