Detecting 'back key' press from within a leaderboard? (Android) - android

I have a leaderboard set up in my Android game and it seems to work pretty well.
From the main menu the player can press the 'show scores' button to simply view the leaderboard and when the player's game is over (either through losing all their lives or completing the game), they are given the option of submitting their score. If this is pressed, the score is submitted and the leaderboard displayed to show the new score.
I have my app set up so that it will return to it's current scene (menu, game etc.) if it is temporarily interrupted.
However, I can't work out how to 'know' if the leaderboard is currently being shown to the user. (Well, I do know how to set this, rather I don't know how to 'unset' it when the leaderboard is exited)
I have a variable set up which determines which action my app should take when 'onSignedInSucceded()' is called like so:
#Override
public void onSignInSucceeded() {
//If the flag is set, then display the leaderboard
if (signInAction==SHOW_LEADERBOARD){
displayLeaderBoard();
}
//If the flag is set, then display the submit score
else if (signInAction==SUBMITSCORE){
submitScore();
}
//Otherwise, reset the flag and take no action
else {signInAction=NO_ACTION;}
}
However, what happens is that, lets say the user pressed the 'show scores' button, we set the signInAction to SHOW_LEADERBOARD and then connect. The leaderboard is displayed.
The user then presses the back key and returns to the app. If the app is now interrupted, onSignInSucceded() is called and the leaderboard is displayed again. (but it wasn't being displayed when the app was interrupted therefore this diminishes the user experience).
It would be great it, when the user presses the 'back' key to exit from the leaderboard, I could set the signInAction back to 'No_Action' - how can I do this?
Just for additional information, my app is a single activity and utilises a custom scene manager so in my Activity class, I can do things on back press like:
If (CurrentScene = mainMenu){
//Do Something Here
}
However, the leaderboard is not a custom scene. So I'm not sure how to go about this.
I guess this question could probably best be summed up as 'How can I detect a back key press from Google Play Leaderboard? (when the user exits it back into the app)' - any help would be appreciated.

You should be able to use onActivityResult() to clear signInAction. You'll need to check the request code and maybe the result codes depending on your application.
Some of the possible results will be:
Activity.RESULT_CANCELED (this is the one you're interested in)
GamesActivityResultCodes.RESULT_RECONNECT_REQUIRED
but you'll probably be best off handling them all the same way.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
//just returned from displaying the leaderboard
if(requestCode==REQUEST_LEADERBOARD)
{
signInAction=NO_ACTION;
return;
}
super.onActivityResult(requestCode, resultCode, data);
}

Related

Xamarin forms : Stop page being accessible from hardware back button

I am using xamarin forms prism.
Quick info : I have a page that contains information, this page the user can delete along with its information, this page is an instance of a page called saved conversation, think of each instance as a saved email, you may have many of the one type and are made dynamically.
The user deletes one saved conversation and this also removes an instance for that page, after they have deleted it, it will send them back to one of two pages every time. But they are still able to access this deleted saved conversation page using the hardware back button (Android), either by clicking it straight after they were force navigated back or navigating the app a bit and then pressing the back button multiple times. I would use something such as...
protected override bool OnBackButtonPressed()
{
return true;
}
But I want the back button to work for other pages and only not work if the previous page is an instance of the Saved conversation page, regardless if it has been deleted or not (As I think I may be able to figure out if it was deleted or not myself, just need to detect the page the hardware back button is going to send the user to, I think).
This might fall under a user experience decision over a technical workaround; but it sounds to me like this page you have might be a good candidate to be a Modal Page.
Navigating away from the modal page (popping it from the stack) should accomplish the same goal you are aiming for: not being able to navigate "back" to that page.
What I have done was add this code to all the pages in which I did not want the user to be able to go back. But it would only limit them from going back if the previous page is one of the two shown below. If it isn't then the back button works.
What I did note was you have to make sure modalNavigation is set to false when navigating to the pages (in my case conversation and saved conversation) otherwise they won't appear on the navigation stack to check against.
/// <summary>
/// Will override the harware back button.
/// </summary>
/// <returns> True if it can't go back false if it can.</returns>
protected override bool OnBackButtonPressed()
{
int count = this.Navigation.NavigationStack.Count;
bool shouldntGoBack = false;
if (count > 1)
{
string previousPage = this.Navigation.NavigationStack[count - 2].ToString();
if (previousPage == "NGT.Views.ConversationPage" || previousPage == "NGT.Views.SavedConversationPage")
{
shouldntGoBack = true;
}
}
return shouldntGoBack;
}

Start Android App on same activity every time

I'm working on an Android app that will show college fitness professors how their students are doing in their classes. Since this data is fairly sensitive (biometrics are shown, including weight, something many college students are self-conscious about) I don't want the data to be available to anyone who picks up the tablet. While I have a proper login screen created, complete with authentication for the database, etc. I have an issue when the home button is pressed. Since Android doesn't close a program immediately on leaving the app, it's possible to reopen it and return to where you were. I would like to force the app to return to the login screen each time (I've altered onBackPressed so you can't just return to the previous view from the login screen) so that you have to re-enter your credentials to get back into the app. However, I can't seem to do this. An answer I found on here said to use the following line:
android:clearTaskOnLaunch="true"
However, no matter what XML file I put it in, be it the Manifest or the individual Activity XMLs, it appears to do nothing. So, how do I ensure the login screen comes up each time the app is launched, regardless of whether it is starting from scratch or not?
Try to play around with onUserLeaveHint() method. If you read its documentation, it says:
Its Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called
So, when ever you detected home button pressed, you can finish the running activity/activities. So next time user click the app, it will start from the first login screen.
Hope this helps.
You should override onUserLeaveHint()
#Override
protected void onUserLeaveHint() {
// do your logic
//finish() or start login activity
}
You could set a flag when onPause() is initiated within the activity. And then when you return you could check the flag from within onResume() and then request a login from that point. This will be sure to request it each time; in a simple case of course.
Edit:
With multiple activities, you could check against a saved context to see if they are the same when you start a new activity. If the contexts differ then you can discard the context previous activities context and start a new activity.
If they are the same, then you have come back to the activity from itself (you have lowered and brought the screen back). You would have to use some form of saved state such as that to do it in this manner with multiple activities when outside the case of a simple application.
I found out how to do it in my case. For any others with the same problem, try following the example here:
Android detecting if an application entered the background

Detect application coming from the background and prompt for PIN in Android

I have an app in which I need to prompt the user to enter the PIN every time the app comes from the background.
I looked several answers from stackoverflow from #hackbod and #commonsware
I implemented the timeout approach from #commonsware and it worked fine, but my users are not happy with it. They want to prompt for PIN everytime when the app is coming from background.
Now I am implemeting the solution from #hackbod by having every activity increment the counter in onStart and decrement the count in onStop.
Android detecting if an application entered the background
But the problem is when users changes the Orientation of the current activity the counter sets back to 0 and my app thinks the user is coming from background.
my onStart() increments the counter and onStop is decrementing the counter.
I want android to take care of the orientation change as I have different layouts for different orientation.
I don't want to use GET_TASKS approach for now.
All suggestions are appreciated..
Take a look at the Activity Lifecycle. If I was going to accomplish something like this I would have each activity request a pin unless they the activity was started with an intent and an Extra value with the current pin.
Launch Activity:
Intent intent = new Intent(getBaseContext(), NewActivity.class);
intent.putExtra("PIN", storedPIN);
startActivity(intent);
Activity Starts:
#Override
public void onResume() {
super.onResume();
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("PIN");
}
//Check pin to make sure it matches stored pin
//Else prompt for pin
}
Under this methodology, when the app first starts up it'll prompt for the pin. After entering the pin when the Main activity starts up other activities it'll pass the stored PIN and won't prompt for the pin again.
You might also have to clear the extras from the current activity when it starts another activity.
At some moment I was looking for very similar behavior. Here is the question with the bounty which I have asked:
How to always start from a startup activity on Android?
It had a lot of different approaches. However, each of them was with some downsides.

User leaves activity by going back

Im connected to a chat server which gives me messages i print out in a textview. This continues when the user leaves the application by pressing the home key etc etc. I would like to close all streams if the user goes back with the phones back button to the previous activity. Problem is that onStop() and onPause() are both called independent of if the Home key was pressed or the back key. Its just called when the activity loses focus or visibility, doesnt matter which way it happened.
How do i find out if the back key was pressed, and not home?
You can implement your own version of onBackPressed(). That way you'll know every time it's pressed and can do what ever you need to in that callback. So do something like this:
public void onBackPressed(){
//Do the stuff you want to do
//Then call the parent class version function to allow it to do any other stuff
// that needs to happen as well.
super.onBackPressed();
}

How to check which activity finished

In my Android application I need to Override the onResume() method to check which of two possible activities just finished. The user will either have entered an amount of money, or named and chosen a percent for a category. How can I do that? Also, if a user presses home and then goes back to my app, is onResume() called? If so, I can just call super.onResume(), right?
I have three classes: PaySaver, NewSavingCategory, and NewPaycheck. PaySaver.java is the main Activity, and there are two buttons: New Paycheck (launches a dialog box where a user inputs $ (NewPaycheck.java)) and New Saving Category (launches a dialogbox where a user inputs a name and a % (NewSavingCategory.java)). When the dialog box is closed via an enter button, I want the main activity to be updated with the information entered.
Thanks!
How can I do that?
Most likely, you don't. Both of those other activities updated your central data model. In onResume(), you update your UI from that same central data model. Hence, it does not matter where the user came from -- you are grabbing the latest data.
Also, if a user presses home and then goes back to my app, is onResume() called?
On the activity they return to, yes.
If so, I can just call super.onResume(), right?
Not only "can" you do that, you have to call super.onResume(), or your activity will crash.

Categories

Resources