Reputation: 147
here's a part of my code: after button is clicked, new thread and connection to server are started. If connection is sucessfull, app should start new activity and end current. Can someone explain which way is best for to do that?
transThread.submit(new Runnable()
{
public void run()
{
guiProgressDialog(true);
if(user.length() < 4) guiNotifyUser("Username must have at least 4 characters!");
else if(pass.length() < 4) guiNotifyUser("Password must have at least 4 characters!");
else if(!pass.equals(passrtp)) guiNotifyUser("Password is not same in both fields!");
else if(!isValidEmail(mail)) guiNotifyUser("Your email is not valid email address!");
else if(fname.equals("") || lname.equals("")) guiNotifyUser("All fields are mandatory!");
else {
try {
final String message = AutoDiaryHttpHelper.signUp(user, md5(pass), mail, fname, lname);
guiNotifyUser(message);
//if message equals something start new activity
}
catch(Exception e) {
e.printStackTrace();
}
}
guiProgressDialog(false);
}
});
break;
Upvotes: 1
Views: 489
Reputation: 147
Here's the code that solved my problem (thanks to CodeMagic - I'm not sure if this is the best way to do this): Inner class:
class RegisterTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
String message = null;
EditText username = (EditText) findViewById( R.id.register_username_text );
EditText password = (EditText) findViewById( R.id.register_password_text );
EditText passwordrtp = (EditText) findViewById( R.id.register_repeatpassword_text );
EditText email = (EditText) findViewById( R.id.register_email_text );
EditText firstname = (EditText) findViewById( R.id.register_firstname_text );
EditText lastname = (EditText) findViewById( R.id.register_lastname_text );
final String user = username.getText().toString();
final String pass = password.getText().toString();
final String passrtp = passwordrtp.getText().toString();
final String mail = email.getText().toString();
final String fname = firstname.getText().toString();
final String lname = lastname.getText().toString();
guiProgressDialog(true);
if(user.length() < 4) guiNotifyUser("Username must have at least 4 characters!");
else if(pass.length() < 4) guiNotifyUser("Password must have at least 4 characters!");
else if(!pass.equals(passrtp)) guiNotifyUser("Password is not same in both fields!");
else if(!isValidEmail(mail)) guiNotifyUser("Your email is not valid email address!");
else if(fname.equals("") || lname.equals("")) guiNotifyUser("All fields are mandatory!");
else {
try {
message = AutoDiaryHttpHelper.signUp(user, md5(pass), mail, fname, lname);
guiNotifyUser(message);//prosla vodi na sledeci activity "Succesfull registration"
}
catch(Exception e) {
e.printStackTrace();
}
}
guiProgressDialog(false);
return message;
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
if (result.equalsIgnoreCase("Successful registration!"))
{
RegisterActivity.this.startActivity(new Intent(context,LoginActivity.class));
RegisterActivity.this.finish();
}
}
}
and call from button listener:
public void onClick(View v) {
switch(v.getId())
{
case R.id.register_register_button:
RegisterTask registerTask = new RegisterTask();
registerTask.execute();
break;
Upvotes: 0
Reputation: 44571
You can use runOnUiThread for that. Here is a SO answer that shows how to do that.
I personally like to use AsyncTask
for this. You can do your work in doInBackground()
then return a value to onPostExecute()
and start the Activity
from there or do whatever you need on the UI
.
Here is an answer of mine that shows the basic structure and important details of using AsyncTask
Edit from code in comment
I can't say the exact error you are getting without logcat but the first problem I see is when you initialize context
in AsyncTask
. You don't want to use getApplicationContext()
especially not the way you are. I imagine you are getting a NPE
because context
is not yet initialized. You are passing Context
in the constructor so you would just do
this.context = context
However, it looks like your AsyncTask
is an inner class of RegisterActivity
which means it has access to all member variables of RegisterActivity
and its Context
. This means that to start your Activity
you can use RegisterActivity.this
instead of context
.
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
//if (result == "Successful registration!")
//String i;
//i = "da";
context.startActivity(new Intent(RegisterActivity.this,LoginActivity.class)); // change this here
As stated, your constructor isn't needed for context
if its an inner class but if it was a separate file it would be like
class RegisterTask extends AsyncTask<String, Void, String>{
Context context;
private RegisterTask(Context context){
this.context = context; // use the variable (context) passed in the constructor above
Upvotes: 3