0

I have a FragmentPagerAdapter that calls back to my MainActivity as follows:

class MyFragmentPagerAdapter extends FragmentPagerAdapter
{
    public void someMethod()
    {
        mMainActivity.setTextViewText("This is a test string");
    }
}

In MainActivity:

class MainActivity extends Activity
{
    public void setTextViewText(String text)
    {
        mTextView.setText(text);
    }
}

The result is a single line of text that does not wrap in the TextView (it gets cut off). The TextView is 0dp in width and uses the weight of its parent as a width. The bizzare thing is that if I call setTextViewText() from within MainActivity itself, the text wraps just fine. It's only when I call it as a callback that the text no longer wraps. Why would this be? P.S. Both approaches are being done on the UI thread as proven by logging Thread.currentThread.getId().

SilentByte
  • 1,098
  • 1
  • 10
  • 21
  • How do you get the mMainActivity ? – zzy Dec 30 '14 at 01:40
  • The MainActivity instance gets passed into the constructor of MyFragmentPagerAdapter and stored as a global member. – SilentByte Dec 30 '14 at 01:55
  • Have you call other function before call `someMethod` ? – zzy Dec 30 '14 at 02:06
  • It gets called as a result of the corresponding ViewPager calling onLayout(). – SilentByte Dec 30 '14 at 02:08
  • Just found out that if I call back to the MainActivity from the ViewPager, it works fine. So it's only a problem when I call back from ViewPager --> MyFragmentPagerAdapter --> MainActivity. – SilentByte Dec 30 '14 at 02:14
  • So , post the code between the ViewPager and MyFragmentPagerAdapter – zzy Dec 30 '14 at 02:24
  • I take that back. It doesn't work when I call back from the ViewPager. I was testing on my Lollipop device and it works fine there, but it doesn't work on my older non-Lollipop devices. So weird! – SilentByte Dec 30 '14 at 02:38

0 Answers0