Reputation: 6516
I'm new in android and I want to create simple application for it. This is how it should work:
observer
so it will be notified by observable
when data change, for example:
main activity
needs to be updatedconnection refused
activity. After that information activity should been shown on top of other (active) activity.connection refused
activity will be alive all the time so I will be able to show/hide it at any timeProblems:
connection refused
activity on top of other (active) activity? Almost everything in app works correctly (connection refused activity is notified that connection has been refused) but I don't know how to bring it to top.connection refused
activity should be shown on top. User shouldn't have possibility to back to blocked activities.ConnectionRefusedActivity:
public class ConnectionRefusedActivity extends Activity implements Observer {
private ServerService service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
service = ServerService.getInstance();
service.addObserver(this);
progressDialog = new ProgressDialog(this);
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setMessage("Unable to connect to server. Click OK to reconnect.");
alertDialog.setButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
service.connect();
}
});
progressDialog.setMessage("Please wait...");
// this method tries to connect to server; if it fails `service` will sernd notification to this activity with data `false`
service.connect();
}
@Override
public void update(final Observable observable, final Object data) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (observable instanceof ServerService) {
boolean isConnected = (Boolean) data;
if (isConnected) {
progressDialog.dismiss();
alertDialog.dismiss();
}
else {
// this will be called if connection with server has been refused; the problem is that I don't know how to bring this activity to top
// ATTENTION! I want to bring this activity to top here
progressDialog.show();
alertDialog.show();
}
}
}
});
}
}
connection refused activity
is notified that connection has been refused so it shows information "Connection has been refused. Do you want to reconnect?" to userconnection refused activity
Upvotes: 3
Views: 134
Reputation: 4752
This is what you can do,
Don't keep the ConnectionRefusedActivity
as the observer. You rest 2 activities should be observer. Best way to do this is, keep a BaseActivity which implements Observer. When the observer is Notified, open the ConnectionRefusedActivity
by calling startActivityForResult
. And when the user tries to reconnect, send the result back for reconnect. In the respective activities do the Reconnect again.
Upvotes: 1