How to resume a lifecycle state through code? - java

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
;))

Related

How to resume activity in Android?

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

How to check if Android application goes to foreground?

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

Home button override in android

Im trying to understand the ways to override the home button functionality in Android.
Im doing a LockScreen App and the HOME button is unlocking the device.
I have been researching the last 3 days for a way to do it, and all the solutions I have found are not 100% effective (mostly due to the nature of HOME).
I have implemented solutions for all the solutions below in these three days but Im still not satisfied.
Problem:
I want to be able to override the HOME button functionality the same way as the WidgetLocker app in the AppStore.
WidgetLocker is not a home launcher and it still can override the HOME better than anyone.
Link to WidgetLocker App
Disclaimer
First of all, I know that this is a security measure in Android so that an App does not take full control of the device, and I agree with it.
The official way is to implement a home launcher and its my preferred of them all, unfortunately my boss is still not 100% convinced on the path I selected, until I exhaust all other possibilities.
It must be for 4.0+ version of android
Solutions
Implement as Home launcher, catch the HOME intent and take care of home button presses, this has the problem that the user must select the app as the default home launcher and may not understand why (another problem I know, but still)
Implement a Service with a SYSTEM_OVERLAY view that is launched every time the device is locked, being effectively on top of everything, unfortunately (or fortunately security wise) the home button presses are still being sent to the app below and the default home launcher is still called.
Implement an activity parallel to the one that implements the lock screen, that contains the HOME Intent that is activated/deactivated programatically on the my app options, effectively changing the main app to a homelauncher on demand, when this activity is called it launches the lockscreen activity.
Is there any other way to do this?
Thanks in advance.
Look in the following topic for a way to override the home button:
Overriding the functionality of Home Button
Personally though I would suggest going for the option where you implement your own home screen. We use a similair setup where I work. The app we want people to use is set as the default option when the home button is pressed. That way we can control where people can go to a large degree. This options can only be reset by resetting the application defaults of the app. This makes it non-permanent in case a fix to the device is needed.
For the latter implementation just install with the following lines in your manifest
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
Now the next time you press the home button a pop-up will ask you what you want to do.

How to prevent Android application from loading multiple times?

This must be an easy one but I'm not having much luck searching for an answer.
Apologies if this is a regular question.
If I navigate away from my app I cannot return to it. Starting the app again will load a second instance of it rather than returning to it. If I leave an audio loop running in my app, It's hard to get back in and turn it off.
On startup I'd like the app to destroy any previous instance of itself left running.
I'd also like to try having the app shut itself down when I navigate away (I know it's not the right way to do things but I'd like to try this for my own personal use of the app). Or have the "back" button destroy the app.
Thanks.
Add this to your activity definition in manifest...
android:launchMode = "singleInstance"
How to prevent the activity from loading twice on pressing the button
I have answered such a question,you can declare android:launchMode="singleTask" attribute for your MainActivity in the AndroidManifest.xml file, so that the OS won't create a new instance if there is one running in the background.

Android: Possible to start any activity of other apps from my own?

This is an Android noob question.
I am trying to start an activity of another apk through my own application. Now I know I can launch any other application and invoke its main activity. In many cases I'm also able to start subactivities, for example display it's settings dialogue.
However with some applications, for example Facebook or Endomondo I would get a FC everytime I try to launch some specific activity of their application.
Now I suspect that this is a permission issue and that the Facebook or Endomondo devs just don't want other applications to get access to their activities. But do I have to find out which activities I can use and which ones I can't use by trial and error every single time?
Plus: Is there any way around this dilemma? Maybe on a rooted device?
Cheers for any pointers.
As you already said you can only use activities of other apps which are designed to be used by others applications. Normally the developer of the other app define a set of intents and actions their app will be able to understand and process.
Using any other app's activity is by default not possible, this is by design of Android as every app runs in it own sandboxed process (there are some exceptions where apps can share a process).
So to use another app's activities you must know the intents it listen on. Normally this can be found in the applications website or documentation or on OpenIntents a dictionary for intents.

Categories