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