Android getParent() is null but parentActivityName is set

1.8k Views Asked by At

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.

1

There are 1 best solutions below

0
On

Have you thought about starting your UpdateActivity with startActivityForResult() ? You can read about it here : https://developer.android.com/training/basics/intents/result. With that in your DisplayActivity you can implement onActivityResult() function, which will recreate() itself. This should work fine. You won't have to call getParent()

getParent() documentation says Return the parent activity if this view is an embedded child. Your activity is not an embedded child. It's not part of ActivityGroups https://developer.android.com/reference/android/app/ActivityGroup.