How to resume activity in Android? - java

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

Related

How to resume a lifecycle state through code?

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

Android app not minimizing as expected when clicking the home button

The Problem:
I have a React-native app, with a third party payment gateway sitting on top of it. My app needs launchMode in the manifest to be set like this:
android:launchMode="singleTask"
In the app itself, everything works as expected, but as soon as the third party payment activity is launched, an issue arises.
In this activity, if you minimize the app using the home button, and then launch the app again from the app drawer, the app restarts instead of resuming on the payment activity as expected.
However, if you set the launchMode in the manifest to :
android:launchMode="singleTop"
it works as expected i.e resumes the activity (with a caveat).
The caveat is that if the app launches via ADB or via the Play store (ie. not clicked from the app drawer) the same issue occurs. However, if you start the app from the app drawer, everything works as expected.
I need the launch mode to be single task though, and this bug is really puzzling me at the moment.
I was wondering if anyone has experienced this before and if someone could perhaps share some light on this issue. I unfortunately can't share too much of the code, but it's mostly standard stuff.

Fresh loading activity every time of calling it

In an android App im showing advertisement from a local advertisement company.
My problem is when start App and close it with back button, and back to it by clicking on its icon, i see the same Ad. But when i close the app from android task manager and back to the app, i see another Ad.
I know this can have multiple reasons and my question is not clear, But my main questions that how i can set each activity runs like first time of running the app? What should cause this reaction? its like a cookie system... i want every activity show new Ad!
Here you can see example code:
http://github.com/adad-project/client-app-sample
Move
Adad.initialize(getApplicationContext());
((AdView) findViewById(R.id.banner_ad_view)).setAdListener(mAdListener);
to onStart and remove from onCreate in MainActivity
Do similar thing in other activities.

Creating a login type of activity and forcing it the only opening point

I have been doing my research, but I feel as if I am missing something.
I have an app with a login. Each time you open the app, you should be forced through that login page. You should never be able to resume onto any activity other than the login.
In the manifest I have
android:clearTaskOnLaunch="true"
on the main activity I wish to use as the login activity,
and
android:finishOnTaskLaunch="true"
as well as
android:excludeFromRecents="true"
on the rest of the activities.
The problamatic situation occurs when you go from the login to another activity, hit home, and relaunch the app via the icon. It should jump back to the login page, but it doesnt. Any idea?
I have also been installing as a regular apk, not via eclipse as I know that there is an issue with eclipse and some of the manifest attributes.
Perhaps if there is a way to detect that the activity launch came from the app icon press, I could manage it that way, but I dont think that is possible either.
In either onResume or onRestart you could check a series of flags, such as a login timeout, then force the user back to the login activity using an Intent, while at the same time finishing the original activity.
I like this method in favor or just finishing the app in either onPause or onStop because it gives you a chance to make some checks before blindly closing the application.
Or, you could try using the android:noHistory tag in your manifest file.
A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.
There are also other tags such as, android:finishOnTaskLaunch
Whether or not an existing instance of the activity should be shut down (finished) whenever the user again launches its task (chooses the task on the home screen) — "true" if it should be shut down, and "false" if not. The default value is "false".
More information here: http://developer.android.com/guide/topics/manifest/activity-element.html
The easiest and fastest way is probably to terminate the activity with finish(); in its onPause() since this is invoked when the App is put into the background.
It might be possible to solve with XML configuration as well, but not that I know of by heart.

Retaining android app state using alwaysRetainTaskState and lauchMode

In my Android app, I have a main activity that serves as an entry point to my application, which is configured in my manifest file like this :
<activity android:name=".Main"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:alwaysRetainTaskState="true"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
So for a particular use case, lets say a user starts up the app from the home screen by clicking the icon inside the application launcher. After starting the app, the user navigates from the Main activity to activity A and then finally to activity B. At this point, the user decides to check their facebook, so they click the home button to put my app in the background, and launches the facebook app.
After checking their facebook, the user wants to return to my app, so they press the home key, and launch the application from the application launcher (just like they did the first time it was launched).
When a user returns to my app, I want the app to return to the last activity the user was at when the app was put into the background, which in this case is activity B. In the manifest file, I have set alwaysRetainTaskState=true to make sure the OS doesn't kill my app's activities.
Now to my question: how do I get the behavior I described above? Whenever I click my app's icon, it always starts at the Main activity, no matter what. I think this is because of the category.LAUNCHER attribute. I have tried android:launchMode=singleTask, but it hasn't made a difference; it always starts at Main.
If someone could clarify intent filters, launch modes, and tasks, that would be great!
FYI singleTask is not what you want, since it starts a new task:
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
How are you launching Activity B? Any non-standard launch modes or Intent flags?
For anyone coming here with similar problems, I found something strange that might be what you are seeing... maybe.
Say I have an app with activities A -> B -> C etc. I was having issues with my app always "resuming" to A if it was launched from the app list aka launcher. Resuming from the "resents" screen (long home press) would exhibit correct resume behaviour though (resume to B or C as expected). My manifest was nothing special, I have alwaysRetainTaskState="true" set in my root activity, and launch mode is default (standard).
I was loading the apk onto my phone via a website. After downloading and installing, I would press "Open" to launch the app right away. For some reason (after uninstalling the app) I tired downloading again, installing, but then I pressed the "Done" button instead. Then Launching the app from the launcher/"all apps" list has the same resume behaviour as resuming from recents - in other words my problems were being caused somehow because of the installation process when clicking "Open" instead of "Done".
I verified this "solution" on API10 (2.3.5) and API15 (4.0.4)
I solved this by adding the screenless DispatcherActivity and making it the default one (by using the very same intent filter). In its onCreate method you create and call the Intent based on some reasonable default (your Main activity for example) OR based on some saved token that identifies which Activity should be started. That token is saved/refreshed in onStop method of any Activity you want to call on restart. You can save this token to Preferences.
The rational here is that last activity that was visible will execute onStop method when interrupted.
Word of caution here: I did implement this pattern and it worked reasonably well. However it seems not play too well with history and finally I just gave up and yanked this code out. Nobody complained so far.

Categories