How to change the Activity inside a fragment with Button?

1

I have a simple App, it has two areas, one with the Fragment and the other with a Button Group

How to do so that by clicking the buttons I can change which Activity will be loaded in Fragment?

And how to keep save the state of the last Open Activity?

Example: In Activity 1 he wrote his name, if I go to 2 and come back, the name will still be there.

ps: I'm not using ListView with TwoWay, it's just a ScrollView

Note: It has no relation to the question Android - Transfer data and change Fragment by Click Since I use extends FragmentActivity and ScrollView + ImageButton and need the "exchange" by OnClickListenner

    
asked by anonymous 27.06.2016 / 09:28

1 answer

1

Well, starting at the beginning.

When you click change activity:

I'll go here the code I used in a program:

Fragment code:     public class frag_modifyProduct extends Fragment {

    private Button btMostrarProduto_Familia;

    private OnFragmentInteractionListener mListener;

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

    // TODO: Rename and change types and number of parameters
    public static frag_mostrarProduto newInstance() {
        frag_mostrarProduto fragment = new frag_mostrarProduto();
        //Caso tenhas argumentos gravas aqui:
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        //caso tenhas guardado argumentos, vens buscalos aqui.
    }

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

        btMostrarProduto_Familia = (Button)rootView.findViewById(R.id.btnoticia);
        btMostrarProduto_Familia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //AQUI faco o i.terminar que e uma funcao feita na atividade onde esta o fragmento!!
              MostrarNoticiaTabbed i = (MostrarNoticiaTabbed)getActivity();
              i.terminar();
            }
        });

        return rootView;
    }

    // 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);
    }
}

Activity: I am going to delete the whole code of this activity and I will just leave it finished ();

public class MostrarNoticiaTabbed extends AppCompatActivity implements frag_mostrarProduto.OnFragmentInteractionListener {
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;
    private ArrayList<Noticia> noticias;

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

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * 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.fragment_mostrar_noticia_tabbed, container, false);
            return rootView;
        }
    }

    /**
     * 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) {
            return frag_mostrarProduto.newInstance(noticias.get(position));
        }

        @Override
        public int getCount() {
            return noticias.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return null;
        }
    }
    public void terminar(){
        Intent i = new Intent(this,AtividadePrincipal.class);
        startActivity(i);
        finish();

    }

}

// HERE IS O finish ();

// you finish (); if you want to "close the activity" if you want onBackPressed to work do not use the finish ();

    
27.06.2016 / 11:40