Updating ExpandableListView inside a fragment from another Activity (not the parent Activity)

41
January 11, 2019, at 7:50 PM

In my case i have two Activities that contains a ViewPager with the relative Fragments. So the situation is something like that:

Activity1(ViewPager): Fragment1, Fragment2, Fragment3, Fragment n... Activity2(ViewPager): FragmentA, FragmentB, FragmentC, Fragment m...

The Fragment2 contain an ExpandibleListView of Projects that by OnClickListener call the Activity2 with startActivity(intent) for showing the Details and other stuffs. The FragmentB inside Activity2 may change one information that is the name of selected Element into ExpandibleListView and when i change the name of the Project i refresh the current Fragment B with this code (by detacching and re-attaching again the current Fragment):

Fragment frg_feedDetails = adapter.getItem(viewPager.getCurrentItem());
final FragmentTransaction frg_feedDetailsTransaction = getSupportFragmentManager().beginTransaction();
frg_feedDetailsTransaction.detach(frg_feedDetails);                                     
frg_feedDetailsTransaction.attach(frg_feedDetails);                                      
frg_feedDetailsTransaction.commitAllowingStateLoss();
// Update Activity title               
Objects.requireNonNull(getSupportActionBar()).setTitle(projectName + ":" + projectFeed.browse(currentObject).get("name"));

The problem come out when the user press back or click the top back arrow and go back from FragmentB to Activity1 that contain the ExpandibleListView. If the user do that the ExpandibleListView with the list of Projects don't receive the new name, i must switch some Fragments inside Activity1 before this happen.

To avoid this i add this code into Activity1:

public void onResume() {
    super.onResume();
    /* FIXME maybe is not the best way to do that but without this override if user change name
     * of feed when press back button (or back arrow) the expandible listview don't refresh items name
     * by adding and removing fragment we get the results
     */
    if (fragmentProjectFeeds != null && fragmentProjectFeeds.isVisible()) {
        Fragment frg_projectFeeds = adapter.getItem(viewPager.getCurrentItem());
        final FragmentTransaction ft_projectFeeds = getSupportFragmentManager().beginTransaction();
        ft_projectFeeds.detach(frg_projectFeeds);
        ft_projectFeeds.attach(frg_projectFeeds);
        ft_projectFeeds.commit();
    }
}

I hope that there is some best way to achieve this, because this onResume, if the user press back and don't change the name will detach and attach again the Fragment with no sense.

I would ask to community some better way to manage this situations.

READ ALSO
Haven't perfect understanding with Firebase-Database and Firebase-authentication

Haven't perfect understanding with Firebase-Database and Firebase-authentication

In hear I'm supposed to register users from my web appBut after one user registration, the second user always removes current user details and come under the previous uid

52
Android APK not installing in device

Android APK not installing in device

I have created Same project as two WorkSpace in my systemex: AppSampleHindi and AppSampleEnglish,

34