2

I have a Fragment, let's call it FragmentA. Inside FragmentA, I start an Activity which changes the data I display in FragmentA, and once I finish the Activity I have launched, I return to FragmentA. The problem is, when this happens, FragmentA still displays the old data. So, I tried to replace FragmentA with a new instance of itself in the onResume, but to no avail, because now FragmentA causes the whole viewpager (which it belongs to) to freeze.

Here's my code:

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

FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.detach(this);
fragmentTransaction.attach(this);
fragmentTransaction.commit();

}

I also tried to replace FragmentA with this code:

@Override
public void onResume() {

FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();

FragmentA fragment = FragmentA.newInstance();
fragmentTransaction.replace(ID?, fragment);

fragmentTransaction.commit();

}

but I don't know how to retrieve the "ID?" to use in fragmentTransaction.replace. I'm rather new to Android and I'm still not sure of how to do certain things. Thank you in advance.

  • Possible duplicate of [ViewPager and fragments — what's the right way to store fragment's state?](https://stackoverflow.com/questions/7951730/viewpager-and-fragments-whats-the-right-way-to-store-fragments-state) – Varun Chandran Mar 28 '18 at 10:07
  • @pskink this is exactly what I've tried to do at first, simply recalling the code that retrieve data and bind them to the views in the fragment. But I can't figure out why the view is not changing at all despite the code being used and the new data being correctly retrieved. So, I figured, I just had to recreate the fragment somehow... – Chiara Pironti Mar 28 '18 at 10:26

4 Answers4

1

Assume FragmentA is started by ActivityA. Now, from FragmentA, you start ActivityB You can use startActivityForResult to start the ActivityB and receive data in the FragmentA inside method:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
   super.onActivityResult(requestCode, resultCode, data);
}

Here, you can update the FragmentA as you desired.

Victor Cao
  • 54
  • 2
0

It's freezing because your creating an endless loop of creating new FragmentA's. I can't find a pic to represent this so here is the best I can do:

  1. Create and show FragmentA.
  2. When FragmentA becomes visible its onResume is called so create another FragmentA.
  3. This 2nd FragmentA then becomes visible and its onResume is called so create a 3rd FragmentA.

This cycle will continue forever which is likely the reason it is freezing. You are looking to use startActivityForResult like @Victor Cao explains is his answer and do not create a new instance of any activity or fragment within itself in one of the life cycle methods like this.

Being new I would also suggest reading the docs on the Fragment and Activity lifecycle and become well knowledged in how they work and when they are called as this is base understanding that every android developer needs to know.

Jason Crosby
  • 3,533
  • 4
  • 28
  • 49
-1

Just update the data in onResume() method of FragmentA

Varun Chandran
  • 647
  • 9
  • 23
Shubham
  • 521
  • 4
  • 11
  • It doesn't work. I tried to retrieve the new data in the onResume and it indeed re-retrieve it, but the values displayed in the view don't change despite the code being correctly called. – Chiara Pironti Mar 28 '18 at 09:49
  • 1
    can you please show the complete code of FragmentA? – Shubham Mar 28 '18 at 09:51
-1

The First thing i need to mention is, better use fragment inside fragment. If so, You can replace that fragment with

Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

You will have a container to hold fragment, you have to inflate it, and provide that id for replacing.

Then if you want to use activity itself, no issues,It will work if you write that code onResume() of the parent activity where fragment is placed.

please go through this question How to start an Activity inside a Fragment class

and refer these sites http://www.oodlestechnologies.com/blogs/Tabs-Activities-inside-Fragment-Activity

Varun Chandran
  • 647
  • 9
  • 23