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.

Share:
14,309

Related videos on Youtube

DancingMonkeyOnLaughingBuffalo
Author by

DancingMonkeyOnLaughingBuffalo

Updated on June 23, 2022

Comments

  • DancingMonkeyOnLaughingBuffalo
    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 Faridnia
      Milad Faridnia
      what do you want by finish do you want to finish your activity ?
  • Emre Koç
    Emre Koç over 9 years
    getActivity.finish(); should be getActivity().finish();
  • weston
    weston over 9 years
    This is the same as other answers.

Related