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();
Possible duplicate of Android - Transfer Data and Change Fragment by Click
– Armando Marques Sobrinho
Possible, but it is not, note that I said clearly that I do not use Listview, in addition to using extends Fragmentactivity.
– Andiie
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
– Armando Marques Sobrinho
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.
– Andiie