Trombone0904
Trombone0904

Reputation: 4258

onclicklistener in fragment with intent

I have an an Android app with an Intro class. This Intro class has got three fragments.

in fragment 3 (IntroPage3) i would like to set an onclicklistener with an intent form the IntroPage3 to Overview.class like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    FragementView = inflater.inflate(R.layout.intro_page1, container, false);

    Button FinishIntroButton = (Button) FragementView.findViewById(R.id.FinishIntroButton);
    FinishIntroButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            intent = new Intent(IntroPage3.this, Overview.class);
            startActivityForResult(intent, 0);
        }
    });


    return FragementView;
}

Problem: is this line:

intent = new Intent(IntroPage3.this, Intro.class);

Error message: enter image description here

Upvotes: 0

Views: 1100

Answers (3)

AminFeizi
AminFeizi

Reputation: 31

public  class Fragment3 extends Fragment{


    @Nullable
    @Override
    public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view;
        view = inflater.inflate(R.layout.page_4, container, false);
        Button button=view.findViewById(R.id.exit);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              Intent intent;
              intent=new Intent(getActivity(),FragmentLogin.class);
              startActivity(intent);

            }
        });
        return view;
    }
}

Upvotes: 1

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75778

You must use getActivity() instead of IntroPage3.this

WHY

getActivity() in a Fragment returns the Activity the Fragment is currently associated with.

Button FinishIntroButton = (Button) FragementView.findViewById(R.id.FinishIntroButton);
FinishIntroButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        intent = new Intent(getActivity(), Overview.class);
        startActivityForResult(intent, 0);
    }
});

Upvotes: 0

Kunu
Kunu

Reputation: 5134

use intent = new Intent(getActivity(), Overview.class);

Upvotes: 1

Related Questions