I believe I am running into a race condition. From a Fragment, I am starting an Activity, and following that I am removing the Fragment. While the app is kept in the foreground there is no issue with this implementation. However, if right before the Activity starts I put the app into the background, the Activity I am calling will start, however, the fragment is not removed. Here is my code,

```
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.putExtras(extras);
startActivity(intent);

    if (getActivity() != null) {   
        getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
        try {
            getFragmentManager().popBackStack();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

```

Is there a better way to implement this? I thought about adding the remove to another thread, but then I receive an exception saying that I cannot perform the remove fragment action before onSavedState. Thanks in advance!