SCOPE
I am making an application for my college fest which shows events for 4 particular fixed dates. I have a main_activity which has a linearlayout on which each and every fragment is displayed. Now a I have a Fragment "ScheduleFragment".
ScheduleFragment consists of a ViewPager and a FragmentStatePagerAdapter which make it possible to toggle through 4 dates.
The ViewPager has 4 pages representing 4 dates and these 4 pages come from 4 references of a same fragment i.e "ScheduleListSubFragment"
From here a user selects an Event and can view the eventDescription which is contained in "EventDescriptionFragment"
PROBLEM
The flow goes well. User come across ScheduleFragment, then toggles to the appropriate date and selects an event.
Now when he goes back from the EventDescriptionFragment to ScheduleFragment (I will later tell how I am doing this), the ViewPager shows blank view. And when we swipe to the next date, the events for the next date comes, and we swipe back to previous date, the events for previous date are back.
I debugged my application and came to know that when we go back to ScheduleFragment, then viewpager is not creating new fragment instances for ScheduleListSubFragment unless I scroll the viewpager.
So, I guess ViewPager is enable to bring that particular reference (of ScheduleListSubFragment) which was being viewed by the user.
How am I going back to ScheduleFragment? I have created a stack in main_activity. When a user selects an event, I push the current reference of ScheduleFragment into the stack. And when back is pressed, I pop that reference and place it on the screen.
CODE *ScheduleFragment* import com.Nit_kkr.updates.app.adapters.ScheduleFragmentAdapter; import com.viewpagerindicator.PageIndicator; import com.viewpagerindicator.TitlePageIndicator; import com.viewpagerindicator.TitlePageIndicator.IndicatorStyle;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class ScheduleFragment extends Fragment{
ScheduleFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
TitlePageIndicator indicator;
@Override
public void onViewStateRestored(Bundle savedInstanceState) {
// Toast.makeText(getActivity(), " onViewStateRestored()", Toast.LENGTH_SHORT).show();
super.onViewStateRestored(savedInstanceState);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new ScheduleFragmentAdapter(getActivity().getSupportFragmentManager());
mAdapter.notifyDataSetChanged();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.scheduled_list, null);
prepareSlidingPager(v);
return v;
}
private void prepareSlidingPager(View v)
{
mPager = (ViewPager)v.findViewById(R.id.pager);
mPager.setOffscreenPageLimit(4);
mPager.setAdapter(mAdapter);
mIndicator = (TitlePageIndicator)v.findViewById(R.id.indicator);
indicator = (TitlePageIndicator) mIndicator;
mIndicator.setViewPager(mPager);
final float density = getResources().getDisplayMetrics().density;
indicator.setBackgroundColor(0x18AF0000);
indicator.setFooterColor(0xFFAA2222);
//indicator.setFooterColor(0xFFAA33);
indicator.setFooterLineHeight(2 * density); //1dp
indicator.setFooterIndicatorHeight(3 * density); //3dp
indicator.setFooterIndicatorStyle(IndicatorStyle.Underline);
indicator.setTextColor(0xAA000000);
indicator.setSelectedColor(0xFF000000);
indicator.setSelectedBold(true);
}
public static ScheduleFragment newInstance()
{
ScheduleFragment frag = new ScheduleFragment();
return frag;
}
}
ScheduleListSubFragment:
public class ScheduleListSubFragment extends Fragment implements OnItemClickListener{
long startDate;
long endDate;
private ListView eventlist;
private listadapter adapter;
private List events;
private MyDB db;
@Override
public void onAttach(Activity activity) {
Toast.makeText(getActivity(), " onAttatch()------------------sublist schedule", Toast.LENGTH_SHORT).show();
super.onAttach(activity);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db=new MyDB(this.getActivity());
Toast.makeText(getActivity(), " onCreate()------------------sublist schedule", Toast.LENGTH_SHORT).show();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Toast.makeText(getActivity(), " onCreateView()------------------sublist schedule", Toast.LENGTH_SHORT).show();
View v = inflater.inflate(R.layout.eventslist_subfragment_layout, null);
eventlist=(ListView)v.findViewById(R.id.Event_list);
eventlist.setOnItemClickListener(this);
fetcheventdata(getArguments().getLong("start_time"), getArguments().getLong("end_time"));
return v;
}
public static ScheduleListSubFragment newInstance(long starttime, long endtime)
{
ScheduleListSubFragment frag = new ScheduleListSubFragment();
Bundle bundle = new Bundle();
bundle.putLong("start_time", starttime);
bundle.putLong("end_time", endtime);
frag.setArguments(bundle);
return frag;
}
private void fetcheventdata(long starttime, long endtime)
{
db.open();
events=db.getEvents_bycategory_bytime(null, starttime, endtime);
db.close();
if(events!=null)
{
adapter=new listadapter(getActivity(), android.R.layout.simple_expandable_list_item_1, events);
eventlist.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
else
Toast.makeText(getActivity(), "No Events Yet", Toast.LENGTH_LONG);
//Toast.makeText(getActivity(), events.size(), Toast.LENGTH_LONG).show();
}
private class listadapter extends ArrayAdapter<Event>
{
private List items;
public listadapter(Context context, int textViewResourceId,
List objects){
super(context, textViewResourceId, objects);
items=new ArrayList();
items=objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v=convertView;
if(v==null)
{
LayoutInflater vi=(LayoutInflater)getActivity().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.upcominglist, null);
}
Event event=(Event)items.get(position);
if(event!=null)
{
TextView name=(TextView)v.findViewById(R.id.Name);
TextView venue=(TextView)v.findViewById(R.id.Venue);
TextView time=(TextView)v.findViewById(R.id.time);
name.setTypeface(Utils.getHeroFace(getActivity().getAssets()));
venue.setTypeface(Utils.getHeroFace(getActivity().getAssets()));
name.setTextColor(getResources().getColor(R.color.black));
venue.setTextColor(getResources().getColor(R.color.darkgrey));
time.setTextColor(getResources().getColor(R.color.black));
//ImageView favouriteicon=(ImageView)v.findViewById(R.id.fav_status_image);
// ImageView remindericon=(ImageView)v.findViewById(R.id.reminder_status_image);
name.setText(event.getName());
time.setText(Html.fromHtml("<b>"+event.getEvent_time()+"</b><br></br>"+event.getEvent_date()));
venue.setText(event.getVenue());
}
return v;
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
eventnotifierapp app=(eventnotifierapp)getActivity().getApplication();
app.crntEvent=(Event) events.get(pos);
//startActivity(new Intent(this.getActivity(), EventDescription.class));
EventDescriptionFragment frag = EventDescriptionFragment.NewInstance();
((MainActivity)getActivity()).OpenNextFragment(frag);
}
}
ScheduleFragmentAdapter:
public class ScheduleFragmentAdapter extends FragmentStatePagerAdapter{
String cat_name;
protected static final String[] CONTENT = new String[] { "Thursday, Feb 20",
"Friday, Feb 21", "Saturday, Feb 22", "Sunday, Feb 23", };
private int mCount = CONTENT.length;
public ScheduleFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if(position == 0)
return ScheduleListSubFragment.newInstance(day1start, day1end);
if(position == 1)
return ScheduleListSubFragment.newInstance(day2start, day2end);
if(position == 2)
return ScheduleListSubFragment.newInstance(day3start, day3end);
else
return ScheduleListSubFragment.newInstance(day4start, day4end);
}
@Override
public int getCount() {
return mCount;
}
@Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length];
}
}
Research I did some digging on stackOverFlow, and found some related queries, which were not exactly the same, but couldnt arrive at a solution
Any Kind of Help, is highly appreciated.
Hi just get your code Running , what help me to resolve your problem is :
1. Do use getChildFragmentManager()
on initializing your Adapter like :
fragadapter = new myfragmentAdapter(getChildFragmentManager());
this will give you run time Error right?? that so because on back pressed button application do not have your fragment context(as per my opinion) so do add your fragment in back stack like :
public void addpagerfragment(View v)
{
pagerFragment frag = pagerFragment.NewInstance();
crrntfrag = frag;
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, frag).addToBackStack(null);
ft.commit();
}