Reputation: 4258
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);
Upvotes: 0
Views: 1100
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
Reputation: 75778
You must use getActivity()
instead of IntroPage3.this
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