• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Interacting between Fragments

hozdaman

Lurker
Hi I have been have lots of issues trying to understand how to interact between fragments with a button click. I have been playing around with a sample app I created:

Here is what I have done:
1. created 2 fragment classes and 2 activities
2. I created a listener to for my first fragment to interact with my activity
3. on the first fragment layout i have a button that when clicked it goes to another activity that has a fragment.
4. when i click the button i get no response. I even looked at the logcat and nothing gets registered.

I am really confused as why this doesn't work. Any help would be great thanks

Here is my code...
Activity 1.

Code:
public class Activity1 extends FragmentActivity implements Fragment1.OnFragmentInteractionListener  {

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

    
    }

    public void onFragmentInteraction(){
        Intent intent = new Intent(this, Activity2.class);
        startActivity(intent);
    }



}
Fragment 1

Code:
public class  Fragment1 extends Fragment {

    private OnFragmentInteractionListener mListener;
    private Button mNewButton, mExistingButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment1_main_fragment, parent, false);

        mNewButton = (Button)v.findViewById(R.id.new_event_button);
        mNewButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onFragmentInteraction();
            }
        });
        return  v;
    }

    @Override
    public void onAttach (Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e){
            throw new ClassCastException(activity.toString() + " must implememnt OnFragementInteractionListener");
        }
    }

    public interface OnFragmentInteractionListener{
        public void onFragmentInteraction();
    }

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

Code:
public class Activity2 extends FragmentActivity {


    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment2_fragment);
    }
fragment 2

Code:
public class Fragment2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment2_fragment, container, false);
        return v;
    }
}
 
Back
Top Bottom