简体   繁体   中英

Can i use TextView.setText() inside of activity class if i pass MainActivity to this class?

I have following Activity:

       public class MapActivity extends Activity implements LocationListener, NavigationView.OnNavigationItemSelectedListener {
    
    TextView mDistance;
 public Navigation navi = new Navigation(this, this);
    
     @Override
        protected void onCreate(Bundle savedInstanceState) {
           
            super.onCreate(savedInstanceState);
    mDistance = findViewById(R.id.distance2);
    
    }
    
   
    }

and class Navigation:

public class Navigation  {

 public Navigation(Context context, MapActivity mapActivity) {
        this.mContext = context;
        this.mapActivity = mapActivity;

    }

 public void updateText(String text){
        mapActivity.mTravelTime.setText(text);
    }
}

Can i update mDistance using mapActivity.mTravelTime.setText(text); from navi class? or i need to do it on runOnUIThread?

Thanks you

It depends on the thread that is executing the call to updateText(text). If you're on the main thread, you'll be good. If you're on a background thread, you'll want to use runOnUIThead(). If you're not sure, assume a background thread to be safe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM