Search code examples
androidandroid-fragmentsexitonbackpressed

how to really exit an activity which contain fragment?


I have3 Activities.
The first one A contains a Fragment A1 which contains a Spinner.
When I click the Spinner i ll go to Activity B, which contains a Button. When I click the Button will go to Activity C.

I ovverrode the Activity C's OnBackPressed

(
Super OnBackPressed()
Intent i = new intent (this,ActivityB.class)
Finish();
StartActivity i
}

I overrode the Activity B's OnBackPressed

(
    Super OnBackPressed()
    Intent i = new intent (this,ActivityA.class)
    Finish();
    StartActivity i
    }

I overrode the Activity A's OnBackPressed

(
Super OnBackPressed()
Finish();
}

My problem is that each time I click on Android's return Button when I am in Activity A, the application doesn't exit and goes to my old Spinner choice.
I have to click several times to really quit.

What should I do to leave the application by simply clicking on the Android return Button when I am in the Fragment A1?


Solution

  • You can use getActivity().finish(); from Fragment