Is it possible to check if the Android application goes to foreground? Maybe anything callback function? Any idea or suggestions is appreciated.
You can write code for callback activity in onRestart() Method.
ActivityManager.getRunningTasks(1).get(0).yourActivity.getPackageName()
This method can get the foreground app package name.
you can use thread listen change.
As per android life cycle onResume and onStart will be called once your activity comes to foreground. For more details Android life cycle
Related
I am trying to create my own Alarm Android application. I want to achieve that when alarm is triggered, MainActivity is resumed (not created again). It basically means that if I set alarm and leave my application, I want that application to be resumed when alarm is triggered.
Currently, I am facing a problem that when alarm is triggered while my application runs in background and I click on application icon, onCreate method is called and basically two instances of application are running simultaneously (I have used Toast messages to confirm this). I expected that click on application icon will cause its resuming if it is already running in background, but it seems it is not the case.
Also, I have tried procedure explained here: Resume activity in Android but it didn't work for me, Toast message from onCreate method appears on screen.
Can anybody help, please? I am really running out of ideas here. Thanks in advance!
What you need to do is specify your activity's launch mode to singleTask or singleInstance. To do this, go to your AndroidManifest.xml and change/add launchMode to your activity.
<activity
android:name=".YourActivity"
android:label="Your Activity"
android:launchMode="singleInstance">
More info on the differences of different launch modes are explained here: https://developer.android.com/guide/topics/manifest/activity-element
I have seen other question they asked how to start activity from service but methods they used doesn't work in android 10. so i'm trying to start another activity when the thing i'm doing in my service is done
if any one know how please help.
I found way by my self you can set it in the alarm manager to open it after your works done. thanks to one of the gods of android amin haery ny teacher.
I have an application in java, android studio.
This app will be used for a specific business purpose on a hardware (tablet) that will be dedicated solely to it(Users cannot close the app to make another use of the tablet).
I need to make sure the application is never closed ...
My first idea was to make the navigation bar never appear to the user, so he would not have the option to close.
I was unsuccessful, after a lot of research ... immersive mode, sticky immersive mode..I can even make it difficult to access the bar, but never deprive.
My idea now is to solve the problem through the application life cycle.
I want that when the onPause or onStop () method is called it will execute a code that retrieves the cycle to onStart ().
It's possible?
Is there any easier or better way to do it?
You can use KIOSK MODE to achieve this functionality in android, In KIOSK Mode it self use for COSU purposr(Corporate Owned Single Use application) or a Single-Use device.
Here is the step by step guide, which explain how to achieve this.
COSU / KIOSK MODE
4 Solutions you can try
Use a local broadcast receiver you can send a broadcast from `onPause or onStop () and start Activity you want
create a service which will bind to the activity and there you can monitor your activity
use alarm manager and start onPause or onStop () and start your activity in when the alarm invokes
Use "Kiosk mode" to achieve this
`
You need to declare your application as launcher so that pressing home button or killing apps would return to your application. You can do it by declaring these two parameters in android manifest and then set your app as default launcher app in android system.
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
Add these categories to your AndroidManifest.xml inside activity tag.
Your problem with the navigationbar might be solved using windowFocusChangeListener
but i would like to recommend a probably better solution for you, you need to achieve what's called "Kiosk mode", please have a look at android's solution for that here https://developer.android.com/work/dpc/dedicated-devices?hl=en
or maybe use a 3rd party app like https://play.google.com/store/apps/details?id=com.gears42.surelock&hl=de
You can do in on 2 ways:
Override onStop() , onPause() , onDestroy(). You can call methods you want or Intent to app again in these methods
Use user background services for keeping the app alive
Have good coding
;))
This may be a silly question but I have the following situation: I want to setup my window layout every time the app is open, for example changing the status bar color. So I created my Application file because I heard it is better if you check something like this in the application file and not in the MainActivity.
Problem: How can I call the getWindow() method without an open activity.
Thanks for helping.
A bit tricky, but you can use Window manager in background-service to display your views without opening an Activity.
Also, for API Level 26+ (Oreo or above) you have to start your service as foreground service
I know that this call should not be made but at the moment is the only thing stopping the activity fast enough.
Basically after returning the app from the background (or using the DDMS "Terminate Application" button) some static variables are null. I would like to restart the activity in order to stop crashes and so all values are updated.
At the moment System.exit(0) has done what I want, but I know this is not good.
I have tried finish(), but that does not stop the activity fast enough, it keeps executing some instructions that still causes the crash.
Any suggestions on how to manage this problem.
Standard way to close your running android app is simple:
finish();
or more complicated way is this:
android.os.Process.killProcess(android.os.Process.myPid());
Only in Android api 21 > you can use >
getActivity().finishAndRemoveTask();
Finishes all activities in this task and removes it from the recent
tasks list.
It stops the application as fast as System.exit (0). But is it better than System.exit(0)? I don't know...
------------------ EDIT ---------------------
In below question
How to quit android application programmatically?
you can see all kinds of ways to stop your application.
According to the docs for Activity.finish():
Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().
When you call System.exit(), you are actually terminating the JVM; by finishing the activity, you allow the Android JVM to do its cleanup.