Changing text from another activity

吃可爱长大的小学妹 提交于 2019-11-27 14:24:45

You should use Android Architecture Components :

You can create a ViewModel containing LiveData of your data object (LiveData<String> in case you want to change only text).

When you will change your live data object from one Activity or Fragment all other Activities and Fragments observing for this live data object will get notified.

Official API doc has complete example with description.

Make a public method in your TabActivity that sets the TextView's text, then call getParent() from the child activity, cast it to your TabActivity class, then call that public method.

You could try implementing a handler for the parent Tab which does the job. Pass the text in a message object from each of your respective tabs. To be safe, make changes within the handler inside a runOnUI block

S. M. Mostaq Hossain

In a case of changing text from asynctask file, you need to implement an interface with a listener. Example:

AsynctaskFile:

OnReadyListener onReadyListener;

public class ABCAsynctaskFile{

   ...

   onReadyListener.onReady();

}

public interface OnReadyListener{

void onReady();

}


public void setOnReadyListener(OnReadyListener onReadyListener){

this.onReadyListener = onReadyListener;

}

ActivityFile:

public class ABC extends AppCompactActivity implements ABCAsynctaskFile.OnReadyListener{
   ..

   ABCAsynctaskFile aBCAsynctaskFileObj = new ABCAsynctaskFile(context);

   aBCAsynctaskFile.setOnReadyListener(ABC.this)

}

@Override

public void onReady(){

   // Your wished changed in edit text.

}

This structure will help you to prevent null pointer exception.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!