Reputation: 11
I am trying to modify UI elements that are inside a FragmentPagerAdapter
on a ViewPager
.
Everything goes well until I rotate the device. Then, FindViewById
returns null
and I can't modify the element.
When I rotate the device do I have to keep in mind some operation?
FragmentPagerAdapter
manages the Fragments when the activity re-creates?
runOnUIThread
has any implication on this? I am using something wrong?
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
//detect if it was recreating by rotation device
if(savedInstanceState != null) {
rotateEvent = savedInstanceState.getBoolean("rotateEvent");
}
// Set up the ViewPager with the sections adapter.
mViewPager = findViewById(R.id.pager);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOffscreenPageLimit(5);
tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
...
}
/**
* On Received Status update on UI state
* @param messageReceived: string received
*/
public void updateReceivedStatus(final String messageReceived) {
runOnUiThread(new Runnable() {
@Override
public void run() {
int idElementToUpdate = getResources().getIdentifier(messageReceived, "id", getPackageName());
View element = findViewById(idElementToUpdate); // element is null after rotation
//Switch type send different message
if (element instanceof RadioGroup) {
//obtain id string of radiobutton
String radioButtonId = message.replace(":", "_");
...
}
}
}
Thanks in advance.
Upvotes: 1
Views: 135