Reputation: 14426
In my Android App, user first lands on Login Activity. I am checking network availability. if network is not available, I want to stop the excision further.
Is is something we need to work on the code level or we have something available like activity.cancel()
or return null
on the Activity to process next code. Note that I do NOT want to close the Activity. I just want to alert the user that network is not available.
Here is the code:
public void onClick(View v) {
if ( v.getId() == R.id.btnLogin ) {
if ( Utilities.isNetworkAvailable(this) ) {
mIsNetworkAvailable = true;
}
if ( mIsNetworkAvailable ) {
username = ((EditText) findViewById(R.id.username)).getText().toString().trim();
password = ((EditText) findViewById(R.id.password)).getText().toString().trim();
}
// If U/P is empty, do NOT process anything, just display the alert and stop.
if ( username.length() == 0 || password.length() == 0) {
Utilities.showAlert("Please enter username and password", this);
// HOW TO STOP THE EXECUTION HERE
} else if ( !mIsNetworkAvailable ) {
Utilities.showAlert("Internet connection is not availble in your device.", this);
// HOW TO STOP THE EXECUTION HERE
}
// If not any of the above, proceed.
User userobj = new User();
userobj.setUsername( username );
userobj.setPassword( password );
TaskHandler handler = new TaskHandler(getString(R.string.server_uri) + "Verify/", userobj, LoginActivity.this);
}
}
EDIT
The above code is in onClick (Login Button). What I want to achieve is, when user clicks on Login button, if network is not available, I need to show the alert and stop further process. If its successful, I need to open the next activity.
This is regular flow like we do elsewhere. I know, we can wrap up the entire code in if-else block. But I am wondering, if there is a way to stop the execution with a call like finish()
.
Note that, I do NOT want to destroy/close the activity. The activity should remain intact so user can keep trying, probably after turning on the network.
Upvotes: 0
Views: 985
Reputation: 2716
Try this. Just return if the constraint is not satisfied. Shown below...
.... .....
.... .....
if ( username.length() == 0 || password.length() == 0) {
Utilities.showAlert("Please enter username and password", this);
return;
} else if ( !mIsNetworkAvailable ) {
Utilities.showAlert("Internet connection is not availble in your device.", this);
return;
}
User userobj = new User();
userobj.setUsername( username );
userobj.setPassword( password );
.... .....
.... .....
Hope it helps you!
Upvotes: 2
Reputation: 395
I think what you are looking for is
finish();
which will terminate the activity in the context.
Upvotes: 0