package com.odc.beachodc.activities; import android.app.ActionBar; import android.app.Activity; import android.app.FragmentTransaction; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.inputmethod.InputMethodManager; import com.odc.beachodc.Home; import com.odc.beachodc.Logout; import com.odc.beachodc.R; import com.odc.beachodc.db.models.Playa; import com.odc.beachodc.fragments.BuscarPlayaFragment; import com.odc.beachodc.fragments.BuscarPlayaFragmentExtras; import com.odc.beachodc.fragments.edit.ValoracionPlayaFragment; import com.odc.beachodc.utilities.Utilities; import com.odc.beachodc.utilities.ValidacionPlaya; import java.util.Locale; import de.keyboardsurfer.android.widget.crouton.Crouton; import de.keyboardsurfer.android.widget.crouton.Style; public class BuscarPlaya extends LocationActivity implements ActionBar.TabListener { /** * The {@link android.support.v4.view.PagerAdapter} that will provide * fragments for each of the sections. We use a * {@link android.support.v4.app.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 android.support.v4.app.FragmentStatePagerAdapter}. */ SectionsPagerAdapter mSectionsPagerAdapter; /** * The {@link android.support.v4.view.ViewPager} that will host the section contents. */ ViewPager mViewPager; BuscarPlayaFragment buscarPlayaFragment; BuscarPlayaFragmentExtras buscarPlayaFragmentExtras; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //opening transition animations overridePendingTransition(R.anim.activity_open_translate,R.anim.activity_close_scale); setContentView(R.layout.activity_home); buscarPlayaFragment = new BuscarPlayaFragment(); buscarPlayaFragmentExtras = new BuscarPlayaFragmentExtras(); ValidacionPlaya.playa = new Playa(); // Set up the action bar. final ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); actionBar.setDisplayHomeAsUpEnabled(true); // 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(R.id.pager); mViewPager.setAdapter(mSectionsPagerAdapter); // When swiping between different sections, select the corresponding // tab. We can also use ActionBar.Tab#select() to do this if we have // a reference to the Tab. mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { actionBar.setSelectedNavigationItem(position); } }); // For each of the sections in the app, add a tab to the action bar. for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { // Create a tab with text corresponding to the page title defined by // the adapter. Also specify this Activity object, which implements // the TabListener interface, as the callback (listener) for when // this tab is selected. actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this)); } Utilities.setActionBarCustomize(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu items for use in the action bar MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.search, menu); return super.onCreateOptionsMenu(menu); } // Si se ha clickado en la opcion de cerrar sesión del menu, mostraremos el fragment que nos permitirá cerrar la sesión. @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle presses on the action bar items switch (item.getItemId()) { case R.id.menu_logout: if (Utilities.isAnonymous(this)) { Crouton.makeText(this, getString(R.string.need_login), Style.ALERT).show(); } else { if (Utilities.haveInternet(this)) { Intent intent = new Intent(this, Logout.class); startActivity(intent); } else { Crouton.makeText(this, getString(R.string.no_internet), Style.ALERT).show(); } } return true; case R.id.menu_search: if (Utilities.haveInternet(this)) { Utilities.buscarPlaya(ValidacionPlaya.busqueda, ValidacionPlaya.nombrePlaya, this, ValidacionPlaya.porCercania, ValidacionPlaya.direccion, ValidacionPlaya.playa); } else { Crouton.makeText(this, getString(R.string.no_internet), Style.ALERT).show(); } return true; case android.R.id.home: Intent intentH = new Intent(this, Home.class); // Para eliminar el historial de activities visitadas ya que volvemos al HOME y asi el boton ATRAS no tenga ningun comportamiento, se resetee. intentH.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intentH); finish(); default: return super.onOptionsItemSelected(item); } } @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { // When the given tab is selected, switch to the corresponding page in // the ViewPager. View focus = getCurrentFocus(); if (focus != null) { Utilities.hideSoftKeyboard(focus, this); } mViewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { View focus = getCurrentFocus(); if (focus != null) { Utilities.hideSoftKeyboard(focus, this); } } @Override public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { View focus = getCurrentFocus(); if (focus != null) { Utilities.hideSoftKeyboard(focus, this); } } /** * A {@link android.support.v4.app.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. switch (position) { case 0: return buscarPlayaFragment; case 1: return buscarPlayaFragmentExtras; } return null; } @Override public int getCount() { // Show 2 total pages. return 2; } @Override public CharSequence getPageTitle(int position) { Locale l = Locale.getDefault(); switch (position) { case 0: return getString(R.string.title_section_search_beach).toUpperCase(l); case 1: return getString(R.string.title_extras_beach).toUpperCase(l); } return null; } } @Override public void onPause() { super.onPause(); //closing transition animations overridePendingTransition(R.anim.activity_open_scale,R.anim.activity_close_translate); System.gc(); Runtime.getRuntime().gc(); } @Override protected void onDestroy() { super.onDestroy(); System.gc(); Runtime.getRuntime().gc(); } }