MainActivity with Login

1

I'd like to know the best way to use a Login application (in relation to activities).

I'm using MainActivity with a Navigation Drawer, and when I open the application it checks for credentials on a sqlite3 bank (if logged in) and then, if it is necessary to log in, run another Login Activity leaving the first one on hold.

When logged in, it ends with finish() acitvity of Login and returns to Main. In the Login activity in the onBackPressioned() method I caused all activities to be completed and the program to exit.

Finally, I just need to understand how to update the MainActivity data after logging in (ie a MainActivity textfield that should have its value changed, eg).

I do not know if it's the best way, I tried searching the internet but I did not find so many examples.

    
asked by anonymous 28.05.2015 / 14:44

1 answer

2

Instead of launching activity Login with startActivity() do it with startActivityForResult()

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

Activity Login at the time of finish()

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

Activity that 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");
        }
    }
}
    
28.05.2015 / 15:11