Skip to content

Instantly share code, notes, and snippets.

@gvr23
Created January 16, 2018 23:27
Show Gist options
  • Save gvr23/2c03d7dad8f1bafe26a24964b63293fc to your computer and use it in GitHub Desktop.
Save gvr23/2c03d7dad8f1bafe26a24964b63293fc to your computer and use it in GitHub Desktop.
pass from one fragment to an activity, to another fragment
ANDROID
=================================================================================================================================
=================================================================================================================================
FRAGMENT ADAPTER
=================================================================================================================================
package dsbmobile.com.drokasa.liquidacion.adapter;
import android.content.Context;
import android.content.res.Resources;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import org.apache.commons.net.bsd.RExecClient;
import dsbmobile.com.drokasa.R;
import dsbmobile.com.drokasa.liquidacion.fragment.ClientFragment;
import dsbmobile.com.drokasa.liquidacion.fragment.LiquidationFragment;
import dsbmobile.com.drokasa.liquidacion.fragment.TotalFragment;
public class FragmentPagerAdapter extends FragmentStatePagerAdapter {
private String[] liqTabList;
private Context context;
final int PAGE_COUNT = 3;
private ClientFragment clientFragment;
private LiquidationFragment liquidationFragment;
private TotalFragment totalFragment;
public FragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
liqTabList = context.getResources().getStringArray(R.array.liqTabArray);
}
public ClientFragment getClientFragment() {
return clientFragment;
}
public LiquidationFragment getLiquidationFragment() {
return liquidationFragment;
}
public TotalFragment getTotalFragment() {
return totalFragment;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
if (liquidationFragment == null) {
liquidationFragment = LiquidationFragment.newInstance(position + 1);
}
return liquidationFragment;
case 1:
if (clientFragment == null) {
clientFragment = ClientFragment.newInstance(position + 1);
}
return clientFragment;
default :
if (totalFragment == null) {
totalFragment = TotalFragment.newInstance(position + 1);
}
return totalFragment;
}
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
return liqTabList[position];
}
}
=================================================================================================================================
FRAGMENT DESTINE
=================================================================================================================================
public void addClient(ClienteBean client){
boolean found = false;
for (ClienteBean clientVerify : clientAdapter.getClienteList()){
if (client.getCodigo().equals(clientVerify.getCodigo())){
found = true;
break;
}
}
if (found){
Toast.makeText(getActivity(), "Este cliente ya ha sido agregado", Toast.LENGTH_SHORT ).show();
}else{
clientAdapter.addClient(client);
}
}
=================================================================================================================================
FRAGMENT ORIGIN
=================================================================================================================================
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ClientFragment.CLIENT_CODE && resultCode == RESULT_OK) {
ClienteBean clienteBean = (ClienteBean) data.getSerializableExtra("client");
LiquidationMenuActivity activity = (LiquidationMenuActivity) getActivity();
activity.addClient(clienteBean);
}
}
=================================================================================================================================
ACTIVITY
=================================================================================================================================
private FragmentPagerAdapter fragmentPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.liquidation_menu_layout);
ButterKnife.bind(this);
preCargar();
}
private void preCargar() {
fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager(), getBaseContext());
viewpager.setAdapter(fragmentPagerAdapter);
slidingTabs.setupWithViewPager(viewpager);
tvHeaderDate.setText(getIntent().getStringExtra("dateLiq"));
}
public void addClient(ClienteBean client){
fragmentPagerAdapter.getClientFragment().addClient(client);
viewpager.setCurrentItem(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment