how to restart activity from fragment?

12,454

Solution 1

Your question is not clear. I think FragmentA is a DialogFragment. At least i assume it.

You can override onDestroy method in FragmentA and write

((MyActivity)getActivity()).refreshUI();

We basicly, casted activity instance to our activity for letting us call our method that you can refresh ui.

An dirty way is, You can also write

Intent intent = new Intent(getContext, MyActivity.class);
intent.setFlag(Intent.CLEAR_TASK);
startActivity(intent);

By this way, we started our activity again and killed the one which is in backstack. I assume your datas is hold from another class which like singleton. Otherwise you lose them or you can use first method.

Good luck there.

Solution 2

use this code

getActivity().recreate();
Share:
12,454
Sammys
Author by

Sammys

Updated on June 19, 2022

Comments

  • Sammys
    Sammys almost 2 years

    I have a MainActivity which has a navigation drawer. If I select an item in the navigation drawer, it launches a dialog fragment FragmentA. Now, if I change a few things in FragmentA, I want MainActivity to reflect the new changes once FragmentA is dismissed. What is the best way to restart MainActivity from FragmentA?