Mainactivity with Login

Asked

Viewed 88 times

1

I would like to know how best to use an application with Login (in relation to activities).

I’m using Mainactivity with a Navigation Drawer, and when opening the application it checks for credentials in a sqlite3 database (if the login was done) and then, if it is necessary to login, runs another Login Activity leaving the first one on hold.

When logging in, it ends with finish() acitvity of Login and back to Main. In the Login method Activity onBackPressioned() I got all the activities finished and the program came out.

Finally, I just need to understand how to update Mainactivity data after logging in (i.e., a Mainactivity textfield that must have its value changed, e.g.).

I do not know if it is the best way, I tried to search the internet but I did not find so many examples.

1 answer

2


Instead of launching the Activity Login with startActivity() do it with startActivityForResult()

//Constante para identificar o resultado
static final int TEXT_RESULT = 1;
startActivityForResult(oSeuIntent, TEXT_RESULT);

In the Activity Login at the time of doing finish()

Intent returnIntent = new Intent();
returnIntent.putExtra("result",stringQueQuerPassar);
setResult(RESULT_OK,returnIntent);
finish();

In the Activity who launches Login

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == TEXT_RESULT) {
        if(resultCode == RESULT_OK){
            //Receba a string que vem de Login
            String result = data.getStringExtra("result");
        }
    }
}
  • I get it. I was thinking that it was in Loginactivity that I should update the textFields but in the onActivityResult method I can do this, right?

  • If the textFields are from Mainactivity yes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.