Lock activity on the screen - android

I have a question about Android Activities.
How to lock current activity on screen and block any navigation/starting new activities?
I have a application, based on WebSocket communication system. And on new transaction started i need to wait for getting any communication result (success or error) and only then i can run new activity or go to previous activity.
But how to implement there? I need to universal method, who will be able to intercept any navigation attempts of standard keys and any navigation attempts of user code.
Thanks.

Actually, I don't think it's possible.
Of course you can override onBackPressed method of the Activity so user will not be able to navigate back. Also you can set FullScreenMode to hide status bar from user.
However, that's very hard to intercept Home key. It's by Android design, so user will always be able to go to the HomeScreen.
So generally, I would recommend using ProgressDialog reference here , you can even setCancelable(false) to prevent it being dismissed by "Back" but "Home" will still minimize your app and go back to the homescreen.

Related

Android: Not end (finish) the last task in stack

I would like to know if there is a way to prevent the last task in the Android Task Stack from being finish(); if the user clicks the back button. I do like that the user is sent back to the Home Screen but I would like the last activity to remain in the stack so if the user clicks on the app launcher from the home screen again it doesn't start the main activity again with a new task stack. My app requires the users to sign in and out of it so if they do sign out I want the task stack to be cleared. Does anyone know if this is possible?
Thanks
This is partially possible changing the implementation of your application as a background Service. Of course, preventing your application from being closed is a very bad practice, actually anything that restricts users from closing an app is a bad practice. As said before, the best way to implement this is putting it within a Service, but this way you should implement at least one mechanism to cleanly close your app (an exit button, for instance), otherwise your users will be in need to force your app to close (and that you won't be able to prevent).
More on this, here.

Returning to calling client with Back button when normally Back is disabled

My app is designed to run as a single instance and the Back button does not allow you to exit the app and return to the Start screen because it is used internally to navigate a hierarchy of screens where each screen can be an activity.
However, an external app can launch one of the app's internal activities. When the user is done with whatever the activity is designed for, the user's intuitive action is to hit the Back button to return back to the calling client. But because I prevent the Back button from exiting, the user cannot return.
I can add code to override this when the code detects that the activity is being launched by a client. The problem however is that if the app closes to return, the user might return to the app from where they left off. But since I closed the app to return to the calling client, the user cannot return back to the app as it was last opened. My app needs to remain as a single instance, so the activity that gets launched cannot be created more than once. Any suggestions on how to return back to the calling client but also keep the app running if it was running when the calling client used one of its activities?
In general, you can programmatically control the back navigation trail. Take a look at
TaskStackBuilder and the documentation for handling notifications Responding to Notifications. It seems that what you're trying to do is control the so-called "back stack". If you use TaskStackBuilder, the behavior will match the platform version you're on.
In pre 3.0 platforms, the Back button went all the way through the back stack to the first task the user did since the phone was turned on. Post 3.0, back does not traverse task boundaries; to get to other tasks, the user clicks the "Recent Items" icon. There's also the "up" icon in an app to navigate to the "beginning" of a task within an app. TaskStackBuilder will "do the right thing" for all versions.
In the current platform version, not allowing Back to exit your app is OK, because Back should only go to the first Activity in the current task. In versions previous to 3.0, not allowing Back to exit the app is more problematic, and I personally wouldn't do it that way, but it's up to you.
What happens when the user clicks Back after an Intent starts your app should be clear from the documentation I've cited. Basically, what you want to do is go back to the previous task.

How to close an application when the Home button is pressed

Hi I have application with more than 20 activities.I want to close my app when Home button is pressed.
There is no notion of "close my app" in Android. Android will get rid of your activities, followed by your process, after some period of inactivity by the user.
You could use the launchMode and clearTaskOnLaunch flags on your root activity from your AndroidManifest.xml:
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
When you again start your app, all activities will be killed.
You don't want to do System.exit() -- that's not how the Android Activity Lifecycle normally works (read this also).
What you should do is move the App to the background with moveTaskToBack(). Android will then keep your app running in the background, and kill it if it's unused and something needs its resources later.
If you want it to close all of the open Activities when your App is no longer visible, you can set noHist = "True" for all of the child activities (leave the main activity with noHist = "False", though). This will make it where instead of reopening your application on the last Activity they were on, it will open it on the "main" activity (i.e. it will be as if they just restarted the app).
Anyhow, read through the following answers for more information: Close application and launch home screen on Android
I have the same problem. Im writing a banking app and am required, by contract, to log off the user (or exit) when the app is put into background. There are obvious security concerns there.
There are a couple of ways Im looking to do this:
1. Intercept home button (and back button for the root activity) key press events to call logoff and/or finish()
2. In the onStop() method, for every activity, detect whether the activity is being stopped due to a new activity being show - if not, assume app is being put to background so logoff and/or finish()
The first may not work if a notification is brought to the front then the user clicks home (I havent investigated yet). Or maybe there are other ways for an app to be put into the background without pressing these buttons
The second way sounds messy & difficult to maintain
Id welcome any other ideas
Drapes
I know android has been designed this way, but it seems naive to think that apps wouldnt want an applicationOnStop event
Hi guys what I understood is that u need to know when app goes in background and how to detect it and if I am wrong plz correct me----
The user can go in background if ur app does not provide any way by pressing Back key or Home Key.
You need to use methods "dispatchKeyEvent(KeyEvent event)" to get home key event or back key event and after getting the event you can execute your task.
you can even restrict user from pressing any key but u can not control the home key.

Android: Determine the previously displayed external Activity

In my application, I used a library that displays ads in my app. When a user clicks on the ad, it launches the Browser app, and the onClick events are handled by the library itself.
I also have a placed code in onPause() and onStart() methods that detects whether any of my Activities are sent to background (user pressed Home) or switching between any of my Activities.
So if the app is either freshly opened (no instance is running) or re-opened from background, it will display a dialog box. If the user is only switching from any of my Activites, then the dialog box shouldn't be displayed.
Now the problem is that when the user clicks on an Ad, the Browser app gets loaded and would mean that my App has been sent to background, and so when the user closes the Browser, it will still display the dialog box when it shouldn't.
Is it even possible for my App to determine the previously displayed external Activity (ie. Browser) and not display the dialog box?
Or are there better approaches that I can follow in implementing such setup?
Thanks in advance.
I don't think that there is a way for your Activity to know, which was the previous activity. A simple work-around is to save the time the dialog was displayed and not displayed it again, before some time passes. You can decide on the exact time period based on your application's requirements.
This may be in fact better in some situations. If the user switches from the Browser to your application and it has been some time he has used your application, it will be appropriate to show the dialog again.
you can use onResume() method to specify the behaviour when your activity get back from the background.

How can I use back button while loading a page in an Android WebView?

I have a link in my application that triggers a webview to open from webview.loadUrl(...).
Sometimes it's takes quite a while to load the page, and in these cases I want to be able to use the built in back button on the phone to cancel the loading and go back to my application.
How do I do this? Is there some way I can listen to the backbutton while loading the url?
The back button is intended to move the user backwards through the activity stack.
I think it is possible you are handling too many user actions in a single Activity. As a general rule, any kind of user 'select' should trigger a new Activity.
Whenever the user does something that should be cancelable with the back button, you should trigger a new internal Activity via an Intent. Then the OS will give you this behavior automatically.

Categories

Resources