Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I need to understand how the Fragment can get the data passed by main, maybe I'm missing something but really I haven't understood how could I do this.</p> </blockquote> <p>From this it seems that you want to pass the data at the moment of the creation of the <code>Fragment</code>. If this is what you want you shouldn't make a custom constructor(and generally for a <code>Fragment</code> you should have only the default no-args constructor(because the platform needs it to recreate the <code>Fragment</code> at certain times)).</p> <p>Instead you should either pass the data in a <a href="http://developer.android.com/reference/android/os/Bundle.html" rel="nofollow"><code>Bundle</code></a>:</p> <pre><code>@Override public Fragment getItem(int position) { switch (position) { //... Bundle args = new Bundle(); args.put("a_string_representing_theKey", data) LineFragment f = new LineFragment(); f.setArguments(args); return f; //... } } </code></pre> <p>This will only work if the data can be put in a <code>Bundle</code>(see the <code>put...</code> method of the <code>Bundle</code> class, all primitives + objects that are <code>Parcelable</code>). In the fragment you could then retrieve the arguments using <code>getArguments()</code>.</p> <p>You can also make the <code>Fragment</code> retrieve the data directly from the <code>Activity</code>:</p> <pre><code>// in the lifecycle method where you want the data ((MainActivity)getActivity()).getDataForFragment(); // use the data </code></pre> <blockquote> <p>For example I have this Fragment that show a chart, (in my intention) when I invoke setFragment(int interval, Point[] snodePoint) from the main, the chart should be updated in ViewPager</p> </blockquote> <p>You seem to contradict yourself. For this use the code from the question I linked to, of course just calling your current <code>setFragment()</code> method will not do anything as you just update the value of two variables, you'll need to let the UI know about the change(like calling another method to recreate the view hierarchy, setting the new values on the chart view and calling <code>invalidate()</code> etc).</p> <p>Edit:</p> <p>The code used in the question I linked to should work, but you aren't using the proper code. You have a <strong><code>FragmentStatePagerAdapter</code></strong> but you're using the code for a <code>FragmentPagerAdapter</code> which will not work:</p> <pre><code>FragmentStatePagerAdapter a = (FragmentStatePagerAdapter) mViewPager.getAdapter(); LineFragment f = (LineFragment) a.instantiateItem(pager, thePositionYouWant); </code></pre> <p>Keep in mind that the code above will work only for the visible fragment plus 1 fragment on each side, the other fragments will not be available(and as the user doesn't see them or can't move to them right away they shouldn't be updated).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload