Marinus Snyman
Marinus Snyman

Reputation: 55

How to return to Fragment from Activity on Back Press

In Android Studio I created a HomeFragment from MainActivity, then a new Activity is started when Bluetooth Data arrives. When I press the Back button while in the new Activity, the app closes. I want the App to go back to the HomeFragment.

I tried the following in the new Activity: But the App still closes

@Override
    public void onBackPressed() {
        finish();
    }

Here is the flow of my code for the sequence mentioned.

enter image description here

Upvotes: 1

Views: 73

Answers (1)

David Wasser
David Wasser

Reputation: 95578

When you launch Activity_ListViewInp you have added FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK. This will remove all existing activities from the task and start a new instance of Activity_ListViewInp. When you then press BACK, the app exits because there are no other activities in the task.

Remove FLAG_ACTIVITY_CLEAR_TASK

Upvotes: 1

Related Questions