0

I have 2 fragments FragmentA and FragmentB

From FragmentA I open FragmentB

val transaction = fragmentManager.beginTransaction()
transaction.add(CONTAINER_VIEW_ID, FRAGMENT_B_INSTANCE, FRAGMENT_B_TAG)
transaction.addToBackStack(null)
transaction.commit()

User can back to FragmentA by both ways pressing a custom back button and pressing Phone back button. Custom button call the below method.

fragmentManager.popBackStack()

FragmentB is user adding form with API call, FragmentA onResume I call user list API and display user list. After add user in FragmentB and when back to FragmentA I need to call API again in FragmentA onResume or custom method in FragmentA to get latest users list. (FragmentA onResume won't call since not calling Activity onResume in this scenario)

How can I do this?

AndroidDev
  • 35
  • 5
  • 1
    There r multiple ways to do it depends on where r u loading data now. One ways is You can use [Fragment result API](https://developer.android.com/guide/fragments/communicate#fragment-result) to send back data weather to refresh of not. – ADM Feb 21 '23 at 05:28
  • @ADM I saw above answer already. It's not matching with my requirement. For better understanding I edited my question with more details. – AndroidDev Feb 21 '23 at 05:55
  • I think this is Exactly what u need . So u only need to reload users in Fragment A if and only if there is new user added from fragment B .. So u can send a flag back to FragmentA whether to reload the data or not .. You don't have to rely on `onResume`.. – ADM Feb 21 '23 at 06:21

2 Answers2

0

if your want dont want to come back to FragmentA then you have to remove from stack when you go to FragmentB. So, in this way move from FragmentA to FragmentB and to FramentC but on coming back from FragmentC to FragmentB not to FragmentA

0

There are methods to call resume functions

in Activity

public class MainActivity extends AppCompatActivity {

@Override
protected void onPostResume() {
    super.onPostResume();
}

in Fragment

public class MainFragment extends Fragment {

@Override
public void onResume() {
    super.onResume();
}

Hope this works for resuming