Intent from Fragment to Activity

后端 未结 11 1521
再見小時候
再見小時候 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: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.

提交回复
热议问题