pepuch
pepuch

Reputation: 6516

Activities - how to work with them?

I'm new in android and I want to create simple application for it. This is how it should work:

  1. App has got 3 activities: main activity, create player activity, connection refused activity.
    • main activity shows players list
    • create player activity allows user to create account
    • connection refused activity is shown when connection with server has been interrupted
  2. App flow looks like this ("->" means that application allows to go from one activity to the other):
    • main->create
    • create->main (using back button)
    • *->connection_refused->* : this activity is special because it will be shown on top when connection with server has been refused so it is independent of other activities and can be shown at any time
  3. Socket connection is used in application
  4. Every activity is observer so it will be notified by observable when data change, for example:
    • user has created account: server will send notification to all users that some user has created account so list of players in main activity needs to be updated
    • server has been shutdown because of some problems: client checks connection every 5 seconds and if connection has been refused it will send notification to connection refused activity. After that information activity should been shown on top of other (active) activity.
    • the best will be if connection refused activity will be alive all the time so I will be able to show/hide it at any time

Problems:

  1. How to show 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.
  2. Is activity good for this problem or should I use some other solution?
  3. If connection with server has been refused all activities should be blocked and 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();
                    }
                }
            }
        });
    }
}
  1. Example use case:
    • User is in main activity
    • User want to create new player so he goes to create user activity
    • User want to click "create user" but at this moment connection with server is refused
    • connection refused activity is notified that connection has been refused so it shows information "Connection has been refused. Do you want to reconnect?" to user
    • User clicks "Yes, I want to reconnect". At this time server is working so application tries to reconnect and hides connection refused activity

Upvotes: 3

Views: 134

Answers (1)

Sagar Waghmare
Sagar Waghmare

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

Related Questions