I have a child activity that updates the database, which is shown in a table below in the parent activity. When the parent activity (Update) is done, a commit is send to the database and the data will be updated. Afterwards I would like to close Update and go back to my parent activity Display. This works, but the data is not refreshed. I read online that there is a simple method to recreate an activity..
public void recreate ()
Cause this Activity to be recreated with a new instance. This results in essentially the same flow as when the Activity is created due to a configuration change -- the current instance will go through its life cycle to onDestroy() and a new instance then created after it.
So I call the UpdateActivity from the DisplayActivity with:
Intent intent = new Intent(app, UpdateActivity.class);
startActivity(intent);
And within the Update there is a button that does:
getParent().recreate();
finish();
However getParent() returns null and I get an error. Why is that?
I have the following in my AndroidManifest.xml (Ofc. more but did not list everything)
<activity
android:name=".SearchActivity"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".DisplaySearchResultActivity"
android:screenOrientation="landscape"
android:parentActivityName=".SearchActivity">
</activity>
<activity
android:name=".UpdateActivity"
android:parentActivityName=".DisplaySearchResultActivity"
android:screenOrientation="portrait">
</activity>
Thanks.
Have you thought about starting your
UpdateActivity
withstartActivityForResult()
? You can read about it here : https://developer.android.com/training/basics/intents/result. With that in yourDisplayActivity
you can implementonActivityResult()
function, which willrecreate()
itself. This should work fine. You won't have to callgetParent()
getParent()
documentation saysReturn the parent activity if this view is an embedded child.
Your activity is not an embedded child. It's not part ofActivityGroups
https://developer.android.com/reference/android/app/ActivityGroup.