Open a fragment from another fragment of different activity

1

I have a question In this code that I currently have, located in a fragment contains a "swich case" that opens a different activity, now what I did is create another an activity 2 which loads 12 fragments with a viewPager, which would replace these 12 activities Does anyone know how to do it so that in each "case" there will be a fragment in specific activity 2

THIS IS THE FRAGMENT WHERE THE LINKS CONTAIN

public class ElRecorridoFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

RecyclerView recyclerEstaciones;
ArrayList<EstacionesVo> listaEstaciones;

public ElRecorridoFragment() {
    // Required empty public constructor
}



public static ElRecorridoFragment newInstance(String param1, String param2) {
    ElRecorridoFragment fragment = new ElRecorridoFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }



}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View vista = inflater.inflate(R.layout.fragment_el_recorrido, container, false);

    listaEstaciones = new ArrayList<>();



    recyclerEstaciones = vista.findViewById(R.id.RecyclerRecorridoId);
    recyclerEstaciones.setLayoutManager(new LinearLayoutManager(getContext()));

    llenarLista();

    AdaptadorEstaciones adapter = new AdaptadorEstaciones(listaEstaciones);

    adapter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            switch (recyclerEstaciones.getChildAdapterPosition(view)){

                case 0: Intent est1 = new Intent (getActivity(), Estaciones.class);
                    startActivity(est1);
                    break;


                case 1: Intent est2 = new Intent (getActivity(), Estacion02.class);
                    startActivity(est2);
                    break;
                case 2: Intent est3 = new Intent (getActivity(), MapsActivity.class);
                    startActivity(est3);
                    break;
                case 3: Intent est4 = new Intent (getActivity(), Estaciones.class);
                    startActivity(est4);
                    break;
                case 4: Intent est5 = new Intent (getActivity(), Estacion05.class);
                    startActivity(est5);
                    break;
                case 5: Intent est6 = new Intent (getActivity(), Estacion06.class);
                    startActivity(est6);
                    break;
                case 6: Intent est7 = new Intent (getActivity(), Estacion07.class);
                    startActivity(est7);
                    break;
                case 7: Intent est8 = new Intent (getActivity(), Estacion08.class);
                    startActivity(est8);
                    break;
                case 8: Intent est9 = new Intent (getActivity(), Estacion09.class);
                    startActivity(est9);
                    break;
                case 9: Intent est10 = new Intent (getActivity(), Estacion10.class);
                    startActivity(est10);
                    break;
                case 10: Intent est11 = new Intent (getActivity(), Estacion11.class);
                    startActivity(est11);
                    break;
                case 11: Intent est12 = new Intent (getActivity(), Estacion12.class);
                    startActivity(est12);
                    break;

            }

            //  Toast.makeText(getContext(), "haz hecho click en: " +listaEstaciones.get(recyclerEstaciones.getChildAdapterPosition(view)).getNombre(), Toast.LENGTH_LONG).show();

        }
    });


    recyclerEstaciones.setAdapter(adapter);

    return vista;
}

private void llenarLista() {

    listaEstaciones.add(new EstacionesVo(getString(R.string.Estacion_1_Titulo), R.drawable.estacion01));
    listaEstaciones.add(new EstacionesVo(getString(R.string.Estacion_2_Titulo), R.drawable.estacion02));
    ETC........
    listaEstaciones.add(new EstacionesVo(getString(R.string.Estacion_12_Titulo), R.drawable.estacion12));

}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}
}

THIS IS THE SECOND ACTIVITY WHERE THE 12 FRAGMENTS ARE CHARGED

     public class Estaciones extends AppCompatActivity implements
        EstacionF_1.OnFragmentInteractionListener,
        EstacionF_2.OnFragmentInteractionListener,
        EstacionF_3.OnFragmentInteractionListener,
        EstacionF_4.OnFragmentInteractionListener,
        EstacionF_5.OnFragmentInteractionListener,
        EstacionF_6.OnFragmentInteractionListener,
        EstacionF_7.OnFragmentInteractionListener,
        EstacionF_8.OnFragmentInteractionListener,
        EstacionF_9.OnFragmentInteractionListener,
        EstacionF_10.OnFragmentInteractionListener,
        EstacionF_11.OnFragmentInteractionListener,
        EstacionF_12.OnFragmentInteractionListener {

private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_estaciones);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_Estaciones_ID);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(getString(R.string.app_name));


    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    mViewPager =(ViewPager)findViewById(R.id.view_pager_est_Id);
    mViewPager.setAdapter(mSectionsPagerAdapter);



}

@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_estaciones_2, 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;
    }*/

    int id = item.getItemId();
    if (id == R.id.home_Id_2) {
        startActivity(new Intent(getApplicationContext(),MainActivity.class));
        return true;
    }


    return super.onOptionsItemSelected(item);
}

@Override
public void onFragmentInteraction(Uri uri) {

}

public static class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }


    public static Fragment newInstance(int sectionNumber) {

        Fragment fragment = null;

        switch (sectionNumber){
            case 1: fragment = new EstacionF_1();
                break;
            case 2: fragment = new EstacionF_2();
                break;
            case 3: fragment = new EstacionF_3();
                break;
            case 4: fragment = new EstacionF_4();
                break;
            case 5: fragment = new EstacionF_5();
                break;
            case 6: fragment = new EstacionF_6();
                break;
            case 7: fragment = new EstacionF_7();
                break;
            case 8: fragment = new EstacionF_8();
                break;
            case 9: fragment = new EstacionF_9();
                break;
            case 10: fragment = new EstacionF_10();
                break;
            case 11: fragment = new EstacionF_11();
                break;
            case 12: fragment = new EstacionF_12();
                break;

        }

        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }
}


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() {
        // 12 paginas a mostrar
        return 12;
    }

}

public void setViewPager (int fragmentNumber)
{
    mViewPager.setCurrentItem(fragmentNumber);
}

}

AND THIS IS THE FIRST FRAGMENT OF 12

public class EstacionF_1 extends Fragment {


private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

public EstacionF_1() {
    // Required empty public constructor
}



public static EstacionF_1 newInstance(String param1, String param2) {
    EstacionF_1 fragment = new EstacionF_1();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_estacion_f_1, container, false);
}

public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}


public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

}

I hope and you can help me. Greetings to all

    
asked by Nestor Reyes 09.08.2017 в 07:06
source

1 answer

-1

It is a good solution that you perform, instead of several Activities having a single Activity with a FragmentPagerAdapter that contains the Fragments that would represent several Activities.

If you have the fragments loaded in the viewPager and you want to open a specific one, you can use the setCurrentItem () , in which you define the index of the element in the viewPager you wish to view.

For example if you want to visualize the third Fragment:

mViewPager.setCurrentItem(2);

To define which Fragment would be displayed before loading the Activity that contains the Fragments you can send this data through a bundle when loading the Activity

intent.putExtra("indiceFragment", 2);   
startActivity(intent);      

You receive the data in the Activity that contains the Fragments and move the pager to the desired Fragment within the viewPager.

Bundle datos = this.getIntent().getExtras();  
int indiceFragment = datos.getInt("indiceFragment");
mViewPager.setCurrentItem(indiceFragment);
    
answered by 09.08.2017 в 13:46