简体   繁体   中英

how we launch Activity from fragments

Intent i = new Intent(this, ActivityTwo.class);
startActivity(i);

I want to go from fragment to Activity by using the seOnClickListener .
Above code is not working

You should use:

Intent intent = new Intent(getActivity(), ActivityTwo.class);
getActivity().startActivity(intent)
Intent i = new Intent(FragmentName.this.getActivity(), ActivityTwo.class);
FragmentName.this.startActivity(i);
Intent i = new Intent(getActivity(), ActivityTwo.class);
 getActivity.startActivity(i);
In your fragment write below code.

private Activity mActivity;

@Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (Activity) context;
    }

and then call your activity

Intent intent = new Intent(mActivity, ActivityTwo.class);
mActivity.startActivity(intent);

this will work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM