Determine if returning from HOME press

surfuric

Member
Licensed User
Longtime User
I have an app with 2 activities. 1st is a login screen, 2nd is a data screen.
I need to know while in the 2nd activity if the user pressed the HOME button.

This app will be on a tablet shared by several people. If the person logged in presses the HOME button, the app needs to return to the login screen upon restart, otherwise the next person using the app would continue where the first person left off. (I can't seem to get them to use a LOGOUT button, they keep using HOME and thinking that logs them out.)

I know pressing HOME calls the Activity_Pause, so I put an activity.Finish there. Unfortunately, Activity_Pause is also called for orientation changes and every time the user changed orientation, it logged them out.

I have a partial fix that is ugly, with flags being set-read etc. But it is giving me problems and I thought before I continued I would ask if I am re-inventing the wheel here.

So . . . Is there a definitive way of telling that the HOME button has been pressed?
Or . . . Is there a way of telling that the app is returning from pausing from the HOME being pressed and not an orientation change.

Sincere Thanks for any help!!
 

surfuric

Member
Licensed User
Longtime User
How I did it so far

Well, I thought I might include how I solved this (not pretty at all), and some of you better programmers can tell me how to do it the correct way.

Basically it sets flags and deals with them on restart. If flag flgOkToRun is False, as soon as the activity is brought to the foreground, it checks if it needs to close the activity. This closes the activity and brings the user back to the first screen which is the login screen.

In 1st activity assuming login/password are OK, before calling "orders" activity do:
B4X:
orders.flgOkToRun = True
StartActivity(orders)

In orders activity add the following:
B4X:
Sub Process_Globals
   Dim flgOkToRun as Boolean
End Sub

Sub Activity_Create(FirstTime As Boolean)
   flgOkToRun = True
End Sub

Sub Activity_Pause(UserClosed as Boolean)
   flgOkToRun = False
End Sub

Activity_Resume
  if flgOkToRun = False then
    Activity.Finish
  End If
End Sub

If I have any other activity I need to jump to, or as in my case reading a pdf, another flag must be set and dealt with in Activity_Resume.

My only problem now is that any activity (such as receiving a phone call or responding to a notification) will unfortunately log the user out. (Sigh.)

If you have a better solution I would really appreciate it. :sign0163:
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I use this but am not sure if it is really stable:

When a user presses HOME, we get back to the home screen, and upon returning to our activity, the activity_resume is fired, but not the activity_create, in contrary to what happens when we have an orientation change (the activity_create is fired). Now, I have a process_global in a module, which is getting set to 'true' when we enter the activity_create and to 'false' when we exit the 'activity_resume'. Thus, if we get back to activity_resume after a HOME click, we have this variable set to 'false'. This is not happening if we come from an orientation change. Thus, we can exit this activity and get back to where we want to (probably a previous activity).

Now, what happens if the user stays at HOME for enough time, so that activity gets killed or loses variables (or whatever since I really dislike android's way of handling apps) ?
In this case, Erel's approach is really the best. You check time, and upon returning from HOME, you find some seconds, minutes difference (I use 2-3 minutes, simply because there is really no need for the user to stay at these activities for too long).
If you think about it, it's the same approach we're using in other external apps as well, when we want to force the user return to a panel, independently of other actions of user.

I just note that I use these two procedures, only when I have activities which should simply enter or get some data and their inactivity should really get us back to another activity.
 
Upvote 0

surfuric

Member
Licensed User
Longtime User
Thank you both for your suggestions. I will first try the timeout method. That is probably better for the user anyway.

When trying to use flags as I show in post#2, if they did anything (like respond to notifications, or take a call, etc) it would automatically log them out. With a timout they can still bounce around from app to app, they just have a finite amount of time to do so.

Thanks again.
 
Upvote 0
Top