Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Android Design Flow Inquiry for Login Activity and Main Activity

I am wondering about a design question about android. I often have two activities to separate my login and my main activity (side note: main activity is usually a navigation drawer). I flow is usually like this:

I was thinking of changing this to:

What do people think? My issues:

  1. Which Activity really should be in the launcher drawer?
  2. I store authentication information whether they are logged in just in the SharedPreferences. This is how checkLogin() is performed and where data is stored after a successful attemptLogin().
  3. I don't want the user to be able to press the back button to access the Login. Only pressing a designated "Log out" Button should bring them back to the login Activityenter image description here.

This is an example of some code for the parts

Part A:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    checkLogin();

    uprintDatabaseHelper = new uprintHelper(this);
    uprintDatabaseHelper.getWritableDatabase();


    setContentView(R.layout.activity_main_nav_drawer);
    mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();
    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));

}

Part B:

SharedPreferences sharedPreferences = getSharedPreferences("UserData",MODE_PRIVATE);
if(!sharedPreferences.getBoolean("LOGGED_IN", false))    {
    if(!sharedPreferences.getBoolean("DATABASE_EXIST", false))  {
        uprintDatabaseHelper = new uprintHelper(this);
        uprintDatabaseHelper.getWritableDatabase();
    }
    Intent intent = new Intent(this,LoginActivity.class);
    startActivity(intent);
}

Part C:

final boolean attemptLogin(String user, HttpClient client)  {
    HttpPost post = new HttpPost(getString(R.string.authorization_url));
    String body;
    body = "email=" + user;

    post.setHeader("Content-type","application/x-www-form-urlencoded");

    try {
        post.setEntity(new StringEntity(body,"UTF-8"));
        try {
            HttpResponse response = client.execute(post);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
            String json = reader.readLine();
            try {
                JSONObject finalResult = new JSONObject(json);

                return finalResult.getBoolean("success");
            } catch (JSONException e1) {
                e1.printStackTrace();
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }


    return false;
}

Part D:

SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean("LOGGED_IN", true);
editor.commit();
finish();
like image 782
napkinsterror Avatar asked Nov 02 '25 12:11

napkinsterror


1 Answers

I'd recommend the following order.

  1. Start your LoginActivity first
  2. Check whether the user is logged in
    • Not logged in? Proceed to 3.)
    • Logged in? Proceed to 4.)
  3. Prompt your user to login. Go back to 2.)
  4. Call finish() on your LoginActivity and start your MainAcitivty

Calling finish() will close the LoginActivity and thus make it impossible for the user to get back to it via backpress.

You can implement the log out function vice versa - call finish() on your MainActivity and start your LoginActivity again.

like image 182
Endzeit Avatar answered Nov 04 '25 01:11

Endzeit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!