У меня приложение с Navigation Drawer которое имеет 6 фрагментов. Первый фрагмент называется MainFragment. MainFragment в свою очередь имеет 4 фрагмента(таба). Они называются FragmentOne(COMMON), FragmentTwo(Battery), FragmentThree(Internal), FragmentFour(Internal).В MainActivity запускается AsyncTask(MyTask) который меняет текст в текущем фрагменте OneFragment(Common) и на не активном фрагменте ThirdFragment(Internal). Мой вопрос: этот AsyncTask не может изменить текст на TextView на этих табах(Common and Internal). Как это сделать? Что я делал: если сделать активным таб Common(FragmentOne) TabLayout.getTabAt(0).select() то изменение текста на TextView на Common(FragmentOne) работает. Если переключиться на третий таб Internal(FragmentThree) TabLayout.getTabAt(2).select() то срабатывает exception. Как изменить текст TextView на не активном фрагменте. Исходный код прилагается. Основные фрагменты: MainActivity MainFragment FragmentFirst FragmentSecond .... FragmentSix. Исходный код
https://yadi.sk/d/2IyWYe0f3J6Pmo
class MyTask extends AsyncTask<Void, Void, Integer> {
@Override protected Integer doInBackground(Void... params) { //Thread.currentThread().setPriority(Thread.MIN_PRIORITY); while (!isCancelled()) { try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } publishProgress(); } return null; }
@Override protected void onProgressUpdate(Void... values) { super.onProgressUpdate(values);
Log.d(TAG, «MainFragment do try»);
try { //tabLayout.getTabAt(0).select(); textV1.setText not work when tab(0) inactive
TextView textV1 = (TextView) findViewById(R.id.TextName2); textV1.setText(«InternalMemory»);
TextView textV2 = (TextView) findViewById(R.id.textParam6); textV2.setText(«All: »+bytesToHuman(getTotalInternalSpace()));
TextView textV3 = (TextView) findViewById(R.id.textParam7); textV3.setText(«Path: »+getInternalMemoryPath());
TextView textV4 = (TextView) findViewById(R.id.textParam9); textV4.setText(«Free: »+bytesToHuman(getAvailableInternalSpaceSize()));
int t4 = (int) Math.ceil(((getAvailableInternalSpaceSize()*100)/getTotalInternalSpace())); TextView textV5 = (TextView) findViewById(R.id.textParam8); textV5.setText(String.format(«%d »,t4) + «% »);
ProgressBar textV6 = (ProgressBar) findViewById(R.id.progressBar1); textV6.setProgress(t4); if (t4<=20) { textV6.getProgressDrawable().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN); } else if ((t4>20) && (t4<=40)){ textV6.getProgressDrawable().setColorFilter(Color.MAGENTA, PorterDuff.Mode.SRC_IN); } else if ((t4>40) && (t4<=60)){ textV6.getProgressDrawable().setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_IN); } else if ((t4>60) && (t4<=80)){ textV6.getProgressDrawable().setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_IN); } else if ((t4>80) && (t4<=90)){ textV6.getProgressDrawable().setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_IN); } else if ((t4>90) && (t4<=100)){ textV6.getProgressDrawable().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN); } else textV6.getProgressDrawable().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN); Log.d(TAG, «MainFragment do internalMemoryInfo»);
//tabLayout.getTabAt(2).select();ThreeFragment.showText not work when tab(2) inactive
ThreeFragment.showText(textV2.getText() + «\n» + textV3.getText() + «\n» + textV4.getText() + «\n» + «Percent: » + textV5.getText());
Log.d(TAG, «MainFragment posle internalMemoryInfo»);
} catch (Exception e) { Log.d(TAG, «MainFragment in Exception»); e.printStackTrace(); } } }