0

I have a login page and then a home page, then I have an exit button on the home page. When you press the exit, I need the app to close. If I use finish() on the home page's exit onClick(),it just take me back to the login page.

So I am using

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Now this does act like an exit but when you start the application again, it by passes the login screen and directly goes to the home page (as the app was never closed). What would be the best solution here?

Egor
  • 39,695
  • 10
  • 113
  • 130
donsalari
  • 61
  • 1
  • 2
  • 10
  • you shouldn't exit the application on your own... this is the Android system's job. – Alex Lockwood Jul 08 '12 at 19:14
  • @AlexLockwood I agree, however, that is really only part of the problem. The real problem here is that calling finish() on the home page goes back to the login page. VenomVendor answered that part of the question correctly, but someone voted down his answer :-( – David Wasser Jul 08 '12 at 19:17

2 Answers2

3

What would be the best solution here?

The best solution would be to delete the exit button and its associated functionality.

First, it is not necessary, as what you are providing with the exit button is already provided via the HOME button on the device.

Second, you have been told, repeatedly, by Googlers, not to have such a button.

See also: Is quitting an application frowned upon?

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I completely agree with that, but just read my response to the above comment. so every time the form is filled, i want to quit the app so that another user can log in with different credentials. – donsalari Jul 08 '12 at 19:21
  • You don't need to "quit the app". You just need to finish all your activities. That's all. – David Wasser Jul 08 '12 at 19:24
  • Of course the whole "should you quit an app?" debate probably needs to be reopened as soon as we talk about applications that can be used by multiple users. The whole thing kinda falls apart when you consider the authentication and security issues. Of course, that whole debate started when Android was only for smart phones and nobody thought much about the whole multi-user issue. – David Wasser Jul 08 '12 at 19:26
  • @donsalari @DavidWasser: You are both describing a "logout", not a "quit" or "exit". I agree with David's comments on the other answer in terms of the mechanics of doing that sort of logout (and don't forget to `null` out any static data members that represent your login credentials, if you have them). For David's multiple-users scenario, you would probably also implement some sort of timeout-based mechanism, so you don't need to explicitly logout. – CommonsWare Jul 08 '12 at 23:33
1

Before Coming to HomePage from Login Page use finish();

Intent i = new Intent(Login.this, Home.class);
StartActivity(i);
finish();
VenomVendor
  • 15,064
  • 13
  • 65
  • 96
  • Ok now there is another issue. I have Page 1,2,3,4. Once the person finishes filling the form on page 1,2,3,4, I redirect him to the home screen. I cannot put finish on all the pages. If the user is on page 3 and wants to go back to back page 2, all the data previously filled on page 2 should be present. – donsalari Jul 08 '12 at 19:16
  • This answer solves the problem of returning from the home page to the login screen. It may not solve any of your other problems. – David Wasser Jul 08 '12 at 19:18
  • 1
    to redirect back to the home screen from page 1,2,3,4 you can use this: `Intent intent = new Intent(this, Home.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent);` You may also need to use `Intent.FLAG_ACTIVITY_SINGLE_TOP` as well. See the documentation about FLAG_ACTIVITY_CLEAR_TOP it explains the alternatives. – David Wasser Jul 08 '12 at 19:23
  • 1
    God bless you David Wasser! I had a deadlind to finish this app and this was the only prob.. FLAG_ACTIVITY_CLEAR_TOP solved it.... :) Thanks.. – donsalari Jul 09 '12 at 06:56