Intent from Fragment to Activity

后端 未结 11 1485
再見小時候
再見小時候 2021-02-03 23:22

I was trying to go to another page using button, but it always fail.

Here is my First Class with its XML:

public class Fin         


        
相关标签:
11条回答
  • 2021-02-03 23:51
     FragmentManager fragmentManager =  getFragmentManager();
     fragmentManager.beginTransaction().replace(R.id.frame, new MySchedule()).commit();
    

    MySchedule is the name of my java class.

    0 讨论(0)
  • 2021-02-03 23:51
    public class OneWayFragment extends Fragment {
    
        ImageView img_search;
    
    
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
    
            View view = inflater.inflate(R.layout.one_way_fragment, container, false);
            img_search = (ImageView) view.findViewById(R.id.search);
    
    
            img_search.setOnClickListener(new View.OnClickListener() {
                @Override`enter code here`
                public void onClick(View v) {
                    Intent displayFlights = new Intent(getActivity(), SelectFlight.class);
                    startActivity(displayFlights);
                }
            });
    
    0 讨论(0)
  • 2021-02-03 23:56

    Try this code once-

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

    And as suggested by Raghunandan remove below code from your fragment_home.xml-

    android:onClick="goToAttract"
    
    0 讨论(0)
  • 2021-02-03 23:56

    Hope this code will help

    public class ThisFragment extends Fragment {
    
    public Button button = null;
    Intent intent;
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
        View rootView = inflater.inflate(R.layout.yourlayout, container, false);
    
        intent = new Intent(getActivity(), GoToThisActivity.class);
        button = (Button) rootView.findViewById(R.id.theButtonid);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(intent);
            }
        });
        return rootView;
    }
    

    You can use this code, make sure you change "ThisFragment" as your fragment name, "yourlayout" as the layout name, "GoToThisActivity" change it to which activity do you want and then "theButtonid" change it with your button id you used.

    0 讨论(0)
  • 2021-02-03 23:56

    If you can get a View in that fragment, you can access the context from there:

    view.getContext().startActivity(intent);
    
    0 讨论(0)
  • 2021-02-04 00:05

    use this

    public void goToAttract(View v)
    {
        Intent intent = new Intent(getActivity(), MainActivityList.class);
        startActivity(intent);
    }
    

    be sure you've registered MainActivityList in you Manifest

    0 讨论(0)
提交回复
热议问题