Android - Check if activity is open for the first time - java

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
}

Related

Why sometimes my Android App restart without a logic explanation?

I'm developing a scientific app in Android Studio. It works smoothy.
The set of source code files is not small, but, as I don't have practically user interface, there is only one activity and there is no intent.
All initialization code is inside OnCreate. Most of times, my app preserves all data, when he gets out of the foreground.
However, maybe (I cannot find a pattern of this event) he loses all data and restart (shows a white screen for 2 / 3 seconds), even if the cell phone don't enter in lock screen and there are just 2 apps running.
There are situations that I comute for another app (like WhatsApp) and resumes for my app, and my data was gone. The app restart again.
There is no error message, no logcat. Nothing.
Mostly, when I lock the screen and enter again, all my app data is there.
PS: My orientation is locked.
PS 2: I've read all related question and there is no hint for me. Based in one answer, I've tried to put in onCreate the following code.
if (!isTaskRoot() {
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {
finish();
return;
}
No changes for me.
Update:
I've stumbled in solution. it can be read in my own answer. it's related to undesired back button effect for one-activity-app (read here and here ).
For me, as my application has only one activity, back needs to be like a home button: exit the app but preserve all activity data. My app has a real exit button, where the user shows that really wants to do this.
It's my first app that I developing in mobile world and, for extension, Android world
Some problems seems to me like that it is only possible find the solution if one has a hint about its solution. it's a contradiction. One doesn't know but has to know to solve that don't know!
And, in this situation, it's not the case. No hints. Just question marks.
Before, I had not noticed any pattern. People sometimes act so automatically ... However, suddenly the penny dropped.
I've stumbled in solution. Fortunately!
Not in a million years could I suppose that if someone has an activity and presses Back button, (right button in the bottom), you practically quit the application, even if it remains as a running app for the left button in the bottom (app switcher button)
When I've noticed it, I start to research with another focus. And I've discovered two sources: Disable back button in android and Android - Simulate Home click
So the solution is simply to make the Back button act like the Home button (middle button in the bottom). Return to the home screen without losing application data.
And this is done simply by putting in the onCreate, for my only activity, the following code.
override fun onBackPressed() {
val i = Intent(Intent.ACTION_MAIN)
i.addCategory(Intent.CATEGORY_HOME)
startActivity(i)
}

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
}

Programmatically launch user selected application

So, if you haven't noticed. Many new launchers give you the option to select an application to be launched as default for a specific category.
My launcher consists of different fragments which displays cards, these cards hold a button which has a function where when the user touches the button, they're prompted with a window where they can select which application to run for that specific view. To make things easier for the user, the intent or app they selected should be saved, so if they select the button again, they don't have to select the app again.
If you would like a few examples to what I mean, take a look at 9 Cards Launcher or Smart Launcher, or any Windows 7 Phone Launcher. They prompt you with a small pop up which lets you select the application you want to run and its set.
how can I achieve this? Please I have literally searched everywhere, but nothings making sense. There are no tutorials on this, I have also decompiled various launchers to see how this works but I don't know where to start.
It'd be great if you could help.
1st of all you need to get the Installed Applications.
Then you will get the application package info from the user selected Application.
Finally launch the intent.
Here's what you can try out:
Create an intent with action=MAIN and category=LAUNCHER
Get the PackageManager from the current context using context.getPackageManager
packageManager.queryIntentActivity(<intent>, 0) where intent has category=LAUNCHER, action=MAIN or packageManager.resolveActivity(intent, 0) to get the first activity with main/launcher
Get the ActivityInfo you're interested in
From the ActivityInfo, get the packageName and name
Finally, create another intent with with category=LAUNCHER, action=MAIN, componentName = new ComponentName(packageName, name) and setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
Finally, context.startActivity(newIntent)
I would also suggest to take a look at FreeTaskManager

Android Run Java every time page accessed

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.

Categories

Resources