How to change Activity within a Button Ragment?

Asked

Viewed 986 times

1

I have a simple App, has two areas, one with the Fragment and another with a Button Group (Imagebuttons)

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

And how to keep the state of the last Activity open safe?

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

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" for Onclicklistenner

inserir a descrição da imagem aqui

  • Possible, but it is not, note that I said clearly that I do not use Listview, in addition to using extends Fragmentactivity.

  • but it is indifferent, just you change the listview to whatever you want, ie where has listview Voce can use textview, or listview, or any other component, the end will be the same

  • If it were that simple, I would have solved it, but a Button Listener is totally different. pq it is necessary to implement an interface... but anyway, thank you.

1 answer

1


Well, starting from the beginning.

When click change activity:

I’ll go through here the code I used in a program:

Code of the fragment: public class frag_mostrar {

    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 will delete the whole code of this activity and I will just let it finish();

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 THE END();

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

  • Thank you for your help Francisco, I will study your code to understand how you did the click treatment and the savedInstance ^^

  • Opa, thanks to your code, I had a good idea here and solved my problem, vlw ai grande. abs :)

  • Whenever you need :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.