Action Bar for Android 2.2, Handler not updating by UI
-
22-06-2021 - |
Question
I am trying to abstract out and create a Action Bar (for Android 2.2). Hence I am writing it out myself. For some reason I am not able to get the HomeScreen to update the actionbar. It may not be a handler issue, something more basic than that I guess.
My Class Diagram is like
IBar (interface) ->implementedby-> ActionBarActivity (abstract class) ->extendedby-> HomeScreen
Uses class God
public static methods.
Only relevant pieces of code below
ActionBarActivity, used by all my Activity's.
public abstract class ActionBarActivity extends Activity implements IBar {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actionbar);
connectionStatus = (ImageView) findViewById(R.id.connectStatus);
connectionStatus.setOnClickListener(this);
if (God.isConnectedToServer)
connectionStatus.setImageDrawable(this.getResources().getDrawable(
R.drawable.connect));
else
connectionStatus.setImageDrawable(this.getResources().getDrawable(
R.drawable.disconnect));
connectionStatus.setEnabled(true);
public void setConnectionStatus(boolean status) {
Bundle bundle = new Bundle() ;
bundle.putSerializable(God.BARVALUES, (IBar) this) ;
Message message = updateBar.obtainMessage() ;
message.setData(bundle);
message.sendToTarget();
}
@Override
public boolean handleMessage(Message barData) {
IBar barValues = (IBar) barData.getData()
.getSerializable(God.BARVALUES);
--->>>if (barValues.getConnectionStatus())
connectionStatus.setImageDrawable(this.getResources().getDrawable(
R.drawable.connect));
else if (!barValues.getConnectionStatus())
connectionStatus.setImageDrawable(this.getResources().getDrawable(
R.drawable.disconnect));
return false;
}
public abstract boolean getConnectionStatus();
HomeScreen extends ActionBarActivity
public class HomeScreen extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
try {
God.switchServer((ActionBarActivity)this, God.mCruiseOnServer);
} catch (InvalidServerDNSorIPException e) {
Toast.makeText(this, "Unable to connect to server", Toast.LENGTH_LONG).show();
return ;
}
God Class, public static.
public class God {
public static boolean switchServer(ActionBarActivity activity, String newServer) {
...
activity.setConnectionStatus(true) ;
....
}
The image button on action bar
<ImageView
android:id="@+id/connectStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/refresh"
android:contentDescription="@string/connectionStatus" />
Edit : I ran this under debugger, and I am execution has reached --->>> line. Which means my connection.setImageDrawable is called, but for some reason does not show up on my UI. I am missing something really basic here.
La solution
The root cause was that the base class had a R.layout.actionbar, which was a different instance than that of the R.id.actionbar instead of my homescreen.