Cannot resolve finish() in intent in Fragment in Android Studio
14,309
Solution 1
Use getActivity().finish();
from Fragment
to finish Activity in which current fragment is attached
Solution 2
You have to use activity instance for finish it.
Call getActivity().finish()
Solution 3
The method finiah()
is not applicable for fragment.
You must use getActivity().finish();
instead, if you want to finish your activity in the fragment.
Related videos on Youtube
Author by
DancingMonkeyOnLaughingBuffalo
Updated on June 23, 2022Comments
-
DancingMonkeyOnLaughingBuffalo about 2 years
Intent myIntent = new Intent(viewEnterChildExp.getContext(), MainActivityExpenses.class); myIntent.putExtra("fromEnterChildExpenseToMainActivityExpenses", "true"); startActivity(myIntent); finish();
I'm using the above code snippet in
fragment
The
finish()
is in red color.When I put mouse cursor over it, an error is popped:
can resolve method finish()
TIA!
-
Milad Faridniawhat do you want by finish do you want to finish your activity ?
-
-
Emre Koç over 9 yearsgetActivity.finish(); should be getActivity().finish();
-
weston over 9 yearsThis is the same as other answers.