当我通过使用显式意图转到HolidayActivity时,从MainActivity我可以看到LisView中填充了片段中的不同元素,按下后退按钮后我再次转到MainActivity,当我再次尝试打开HolidayActivity时,ListView再次填充对于重复元素,我使用了LoaderManager,因此当旋转设备时没有重复元素时,只有当我在如上所述的活动之间切换时才会出现问题。我也尝试覆盖方法onKeyDown()和onBackPresses(),但似乎没有任何结果。需要注意的是,我从服务器获取列表项。
public class HolidaysActivity extends AppCompatActivity {
public static String HOLIDAY_REQUEST_URL;
public static holidayAdapter adapter;
public static final String LOG_TAG = HolidaysActivity.class.getSimpleName();
public static Context mcontext ;
public static Activity activity = null;
public static int list_id;
public static ListView listView_holiday;
/**
* The {@link PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link FragmentStatePagerAdapter}.
*/
private static boolean studentLogin = false;
private static boolean employeeLogin = false;
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
mcontext = this;
activity = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_holidays);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
HolidaysActivity.this.finish();
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onBackPressed() {
super.onBackPressed();
activity.finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_holidays, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<holidays>> {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.holidays_list, container, false);
// condition for switching to student login in TabWidget for registering complain
if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
HOLIDAY_REQUEST_URL = "http://192.168.43.26/fetch_restricted_holidays.php";
listView_holiday = (ListView)rootView.findViewById(R.id.list);
adapter = new holidayAdapter(activity,new ArrayList<holidays>(),0);
listView_holiday.setAdapter(adapter);
LoaderManager loaderManager = getLoaderManager();
loaderManager.initLoader(1,null,this);
}
//condition for switching to employee login in Tabwidget for registering complain
else {
}
return rootView;
}
@Override
public Loader<List<holidays>> onCreateLoader(int id, Bundle args) {
return new HolidayLoader(activity,HOLIDAY_REQUEST_URL);
}
@Override
public void onLoadFinished(Loader<List<holidays>> loader, List<holidays> holidays) {
adapter.clear();
if(holidays!=null && !holidays.isEmpty()){
adapter.addAll(holidays);
adapter.notifyDataSetChanged();
}
}
@Override
public void onLoaderReset(Loader<List<holidays>> loader) {
adapter.clear();
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
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).
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 2 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Gazetted Holidays";
case 1:
return "Restricted Holidays";
}
return null;
}
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout complainLinearLayout = (LinearLayout)findViewById(R.id.complain_LinearLayout);
complainLinearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent complainIntent = new Intent(MainActivity.this, Complain.class);
startActivity(complainIntent);
}
});
LinearLayout holiday = (LinearLayout)findViewById(R.id.holidays_LinearLayout);
holiday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity( new Intent(MainActivity.this, HolidaysActivity.class));
}
});
}
}