Android Run Java every time page accessed - java

I currently have a home screen of my app where it displays a number all this shows is how many questions have been answered. Another button takes you to answer questions and it updates the database value.
I have found that the home page number display only updates the first time the app is opened. If you go back a page it does not update the value.
What should I do to make sure this is run everytime?

I believe putting your method call in the onResume() method of the main Activity will have it run every time it's re-displayed to the user.

Related

Android - Check if activity is open for the first time

Before people say this is a duplicate of "check if android is on first run", this question is to check if the activity itself (not the app in whole) is open for the first time.
I have different activities that run Material Tap Target Prompt, so a few pop-ups that explain the buttons and functions.
But I only want it to run for first time users.
Now I tried the following:
if (prefs.getBoolean("firstRun", true)) {
prefs.edit().putBoolean("firstRun",false).apply();
........Do the pop ups
}
But this will set it for the whole app and so when the user gets to the next screen it won't run because the boolean is set to false.
So I am trying to find a way to check if the activity itself is opened for the first time but I can't seem to find anything that would solve this issue.
I thought about using a variable then setting it to 1. But if the users restarts the app, it crashes etc then that var will be reset.
May other option is to create a row in a DB and then check if that is set to 1 or whatever depending on the activity.
But maybe there is an easier way?
Thank you
Why don't you just create preference keys for each Activity. Sample code added below:
if (prefs.getBoolean(MainActivity.class.getCanonicalName(), true)) {
prefs.edit().putBoolean(MainActivity.class.getCanonicalName(),false).apply();
........Do the pop-ups
}

android app stop variable from resetting - lifecycle

I have an app that has a few screens.
The Main screen automatically opens a "new" screen if it's the first time the user opens the app.
I then set a boolean variable (on the Main screen) keeping track of this.
The intention is if the user goes back to the Main screen, the code that opens the "new" screen can be skipped.
The problem is that the variable keeps getting reset on OnCreate.
So, I added some code to use SharedPreferences.
This works; however, I want to clear the variable when the app exits.
(I want the "new" screen to open every time the app opens the first time).
So, looking at the lifecycle I tried both onStop and onDestory.
The SharedPreferences are cleared but... not when the app is exited; but when the "new" screen appears.
Am I looking at the lifecycle wrong?
Is there some sort of global variable I can declare that only lives while the app is open?
This functionality is the requirement, so I cannot change it.
You should use onSaveInstanceState and onRestoreInstanceState, they will keep the boolean alive if your activity calls onCreate but not if you exit and come back later.
See this answer for implementation:
Saving Android Activity state using Save Instance State
You can define the variable in the line one of ur whole code, that way it will only reset when the app is opened again.

How to execute code everytime app opens Android

I want to execute a piece of code (say, for example, display a Toast) every time that the app is opened. So far I have managed to do this every time the app is launched by putting the code into my MyApp.java file that extends Application.
However, if I press the homescreen or back out of the app and then go into it, the message doesn't reappear. It only does when I relaunch the app. Any idea how to do this?
EDIT:
basically im asking how to execute code everytime the whole APP is brought to foreground (this can be first time open, after another app was used, after user backed out of app, etc). Where would I place onResume code? It wouldn't be in a particular activity, would it, since I want it to apply when entire app appears in foreground, not just particular activity.
You can try writing that code in your activity's #Override-d onResume() method.
The only way to do this is,
Determine which app is currently in the foreground
.Follow this discussion for getting an idea for the best way to do it.
[Determining the current foreground application from a background task or service
Suppose, if the function name is 'getCurrentForgroundApp()',
You need a service to execute getCurrentForgroundApp(); every one second
(1-second interval is depending on your purpose, can be lower or higher).
Now, you can identify which app is running foreground in every second.
So, check if your app is the one running foreground. If true, then execute the toast or code you need.
This is how app-locker apps showing lock screen over selected apps, whenever they come to the foreground.
You have to use the onResume callback:
Android API
Example of use from previous SO question
In activity class:
#Override
protected void onResume() {
super.onResume();
//your code here
}

Android - Start Two Activities

I've looked at a few questions on here and I never found an answer I like. I may not even need to launch two acitivies at the same time, but what I am trying to achieve is that on a button press, it brings up the calendar prepopulated with add ins for the reminders and to also move the view of my app to the next page. They'll see it when they confirm their reminder or cancel it and go back.
So really, the question is, how would I move my app to a new activity and also bring up the calendar with the info? I've been trying to do this onto one button to make it super simplistic, but it is not possible I can do it with two buttons...
Call the calendar activity from oncreate "next page activity". The "next page activity" won't load but will appear when going back.
If you don't want to always load the calendar from that activity you can pass a parameter and check it.

Android - Activity behaviour?

I have a small Android application with a TabScreen as my main screen. I have a small problem where my application currently loses focus on the last Activity the user was on.
This is the scenario:
User launches application
Application shows login screen
User enters login details and goes to tab screen
User leaves application via home key
User presses application icon to return to app and the login screen displays again
I want the application to return to the last known displayed Activity in this case.
What I do at the minute is launch the login screen as the Main/Launcher Actvitiy, then when correct credentials are entered launch the tab screen activity and finish the Login activity.
Is there a launch mode or something I should be using to achieve this?
EDIT: More info
The Tab screen is launched simply like this:
Intent intentTabActivity = new Intent(getApplicationContext(), TabScreenActivity.class);
startActivity(intentTabActivity);
Leaving the application through the home button.
I intend to persist the login state and bypass the login but on smaller applications I have created the application returns to the last displayed activity automatically and does not return to the initial Launcher screen every time and I was wondering why this is not the same behavior in this application.
Also as per my other question HERE the behavior seems to be different for debug and signed releases.
This has always been tested on real devices.
This is the correct behavior. Essentially what happens is as soon as the activity goes in the background it is on the mercy of Android DVM. If DVM feels it needs space it will essentially go ahead and kill your application. So once you try to start the application from the icon it actually restarts it from scratch.
However to solve your problem, you should have a checkbox like "Automatically login" or "Remember password" on the login screen and when the user checks it everytime the app opens it should automatically log you in and take to the next screen. This behavior needs to be implemented by you using some sort of persistent storage.
Might be because you are using your onPause() so that your tabs does some action when the tab is passed.
So eventually when the home key is pressed onPause() will be called which might lead you to this problem. Maybe you will have to check your onPause() for this case.
on home button only onStop is called, are you doing finish of activity on onStop? If not it preserves what was the activity which is top of the stack.

Categories

Resources