Skip to content Skip to sidebar Skip to footer

How To Interact With Ui From A Different Class

I would like to update my UI from a different class. I am familiar with runOnUiThread() method, but don't know how to implement it in this scenario? public class UploadAct extends

Solution 1:

Use BroadcastReceiver

// define a Broadcast Intent Action in String resources
<string name="broadcast_id">MY_BROADCAST_ID</string>

// register receiver in constructor/onCreate()
MyBroadcastReceiver myBroadcastReceiver = new MyBroadcastReceiver();
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction(context.getString(R.string.broadcast_id));
context.registerReceiver(myBroadcastReceiver, myIntentFilter);

// place your BroadcastReceiver in MainActivity, your UploadData classpublicclassMyBroadcastReceiverextendsBroadcastReceiver {

    publicMyBroadcastReceiver(){
        super();
    }
    @Override publicvoidonReceive(Context context, Intent intent) {

        Log.d(TAG, "Broadcast received");
        if(intent.getAction() != null && intent.getAction().equals(context.getString(R.string.broadcast_id)) ){
            // do something
        }
    }
}

// send Broadcasts from where you want to act, your UploadAct class.
Intent intent = new Intent();
intent.setAction(context.getString(R.string.broadcast_id));
context.sendBroadcast(intent);
Log.d(TAG, "Broadcast sent.");

// you can unregister this receiver in onDestroy() method
context.unregisterReceiver(myBroadcastReceiver);

Solution 2:

You can also use an interface to update your UI as a listener.

First, Create an interface

publicinterfaceUpdateTextListener {
    voidupdateText(String data);
}

Then, Call its method in your UploadData class

publicclassUploadDataextendsUploadAct {

     UpdateTextListener listener;

     publicvoiddoSomethig(){
         listener.updateText("data to be loaded");
     }
 }

Then, Update your UploadAct by listening to this method

publicclassUploadActextendsMainActivityimplementsUpdateTextListener {

     @OverridepublicvoidupdateText(String  data) {
         textview.setText(data);
     }
}

Solution 3:

First of all - there is no such thing like UI of some class. There are activities that can have handles to UI widgets (ex TextView). If you want to make some changes to UI from your UploadData class you have to pass somehow reference to this class. Possibly by constructor:

publicclassUploadDataextendsUploadAct{
privateTextView txt_upload;

publicUploadData(TextView tv)
{
      txt_upload = tv;
}
publicvoiddoSomethig(){
printThis("I want to print this message to the UI")
}

 publicvoidprintThis(String messsage) {
    final String mess = message;
    runOnUiThread(newRunnable() {
        @Overridepublicvoidrun() {
                       Toast.makeText(getApplicationContext(),mess,Toast.LENGTH_LONG).show();// I want this to display on the main thread
            txt_upload.setText(mess);// and this also
        }
    });
}
}

I assume that you create DataUpload in your MainActivity.

Solution 4:

Everyone use so much library to be trendy as they forget built in functions in Android :)

For sure isn't any hard thing to use AsyncTask, beside it provides the doInBackground function it has the https://developer.android.com/reference/android/os/AsyncTask.html#publishProgress(Progress...) function too, what you have asked for.

Just create a class (UploadTask) which extends AsyncTask and override 1-2 function.

Post a Comment for "How To Interact With Ui From A Different Class"