Я пытаюсь изменить элементы пользовательского интерфейса, которые находятся внутри FragmentPagerAdapter
на ViewPager
. Все идет хорошо, пока я не поверну устройство. Затем FindViewById
возвращает null
, и я не могу изменить элемент.
Когда я поворачиваю устройство, я должен иметь в виду некоторые операции? FragmentPagerAdapter
управляет фрагментами при повторном создании действия? runOnUIThread
имеет какое-либо значение для этого? Я использую что-то не так?
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(":", "_");
...
}
}
}
Заранее спасибо.