How to Restart Activity in Android

how do I restart an activity in android?

This has been posted before:

Intent intent = getIntent();
finish();
startActivity(intent);

As of API level 11, you can also just call an activity's recreate() method. Not only is this cleaner because it is less code, it avoids issues that may arise if your activity was launched by an implicit intent.

Reload activity in Android

You can Simply use

finish();
startActivity(getIntent());

to refresh an Activity from within itself.

Restart Activity onPostExecute

You can Simply use

startActivity(getIntent());
finish();

to refresh an Activity from within itself.

Alternatively you can call recreate() to refresh the activity.

how to restart activity from fragment?

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.



Related Topics



Leave a reply



Submit