3

I am new to android development. I am developing an android app, in which I have few views, say login view, view A, view B, view C and view D etc..

My initial view is a login view. User enter their credentials and if they are valid, view A (for a particular user role) will be displayed.

Now, user clicks the phone home button and now my application is running in background mode.

When user clicks back my app icon, it displays the login view. But actually I need to display view A.

Why is it so? I don't find any error stack trace. Can anyone please suggest me what I am doing wrong.

Thank you.

2 Answers2

2

When the application is brought back up, onCreate is called again. If you want to save your state, you should use SharedPreferences and save them in onPause. Then recall them in onResume.

Tamir Scherzer
  • 995
  • 8
  • 8
  • Thanks. Saving the state means, you are saying that I need to save my view A state using SharedPreferences in onPause() and then recall in onResume(). right? Can you please share some sample code for this. –  Oct 08 '12 at 09:13
  • 1
    save the login state of the user in sharedPreference as true or false then show the login screen only when the preference is false – Syn3sthete Oct 08 '12 at 09:22
1

There are a couple of things you can do. Normally you would override onSaveInstanceState(Bundle savedInstanceState), see for example

Saving Android Activity state using Save Instance State

But of course in your case you must be careful with how long the login state should be valid etc.

Community
  • 1
  • 1
dorjeduck
  • 7,624
  • 11
  • 52
  • 66