Open Another Activity Using Fragment Button

22,963

Do not handle the onClick for the fragment button in the Fragment. Let it go it's parent activity. And start the activity from the parent activity.

To make sure that the button onClick event is sent to the parent activity, make sure, in your about.xml, for the button with id btnContactDev, you have the following parameter:

<Button android:id="@+id/btnContactDev"
  android:onClick="buttonClick"
  ...
/>

and in your parent activity (parent of About fragment), you have:

public void buttonClick(View v) {
  switch(v.getId()) {
    case R.id.btnContactDev:
      Intent myIntent = new Intent();
      myIntent.setClassName(your_package_name_string, your_activity_name_string);
      // for ex: your package name can be "com.example"
      // your activity name will be "com.example.Contact_Developer"
      startActivity(myIntent);
    break;
  }
}

HTH.

PS: This solution is very specific for what your requirement. In general, it's best to handle the onClick events related to the fragment inside the fragment class.

PS: Yes, as the other solution says, make sure you have registered the Contact_Developer Activity in your Manifest file.

Share:
22,963
Kodi
Author by

Kodi

I'm an Android developer and new to iOS developing. I'm not really good but I do have dedication to getting my projects done and if that means asking questions to reach my goal then so be it. I have 1 year of experience with Android and only months with iOS but progress is progress.

Updated on October 14, 2020

Comments

  • Kodi
    Kodi over 3 years

    Okay so I've tried two types of code to get this to work and it keeps giving me force closes when I press the button to go into another Activity. I'm using a Fragment and there's a button in that Fragments code but I can't seem to get it to work. I'm not an experienced Android developer but I'm trying my best to learn.

    Here's the Java code:

    1st Method

    public class About extends Fragment {
    
        Intent intent;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
            View rootView = inflater.inflate(R.layout.about, container, false);
    
            intent = new Intent(getActivity(), Contact_Developer.class);
            final Button button = (Button) rootView.findViewById(R.id.btnContactDev);
    
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    startActivity(intent);
                }
            });
    
            return rootView;
        }
    }
    

    2nd Method

    public class About extends Fragment {
    
        public void onCreate (Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
            View rootView = inflater.inflate(R.layout.about, container, false);
    
            Button button = (Button) rootView.findViewById(R.id.btnContactDev);
            button.setOnClickListener(new View.OnClickListener() {
    
               @Override
               public void onClick(View arg0) {
                  Intent intent = new Intent(getActivity(), Contact_Developer.class);
                  getActivity().startActivity(intent);
               }
            });
            return rootView;
        }
    }
    

    I really don't know what's going on and why I'm getting force closes but if anyone could help me and explain a little what I did wrong that'd be more than enough