Android Activity Lifecycle With/Without Lock-screen - java

I'm trying to find out what the difference is in the activity lifecycle when a lock-screen is enabled versus disabled.
Scenario A (no lock-screen)
App is running
power button is hit to turn display off
power button is hit to turn display on
App is immediately resumed
Scenario B (lock screen enabled, swipe to unlock)
App is running
power button is hit to turn display off
power button is hit to turn display on
Swipe screen to unlock
App is resumed (but we are getting a bug in the display, which is where we are trying to find the difference)
I have printed out the activity lifecycle for both scenarios and they show up identical for both scenarios.
What might be the difference in the lifecycle that would cause this different behavior when using lock-screen versus no lock-screen?

Take a look at this documentation. It could be that the lock screen would imply that the app process is killed and therefore data is lost (which is necessary to present the page and therefore the app crashes). I would suggest to debug the activity states to find the answer you're looking for.

You can test it with writing Logs into onResume(), onCreate(), onPause(), onDestroy() etc.
Also pressing Power button and and opening LockScreen changes the devices screen orientation into "Portrait" mode(Phones and tablets with locked rotation only). It may cause different reactions you are mentioned about.

Related

Android: Customizing recent apps thumbnail (screenshot by default)

The app I'm working on shows some sensitive information that must not be shown on the "Recent Tasks" screen when stopping the app by pressing the home button.
I'd like to blur the sensitive data in the screenshot or show the app logo instead.
I am aware of the following approaches but they don't fit my requirements:
Setting the actvitie's android:excludeFromRecents to true in the manifiest prevents the app from being shown at all in the recent tasks. This would disrupt the user experience.
Using FLAG_SECURE results in a blank card on the recents tasks screen. (How do I prevent Android taking a screenshot when my app goes to the background?) I don't like the blank screen. However, I'll stick to this solution if there is no workaround.
Overriding onCreateThumbnail seems like the ideal solution but, unfortunately, doesn't work as it's currently not invoked by the OS :( (https://code.google.com/p/android/issues/detail?id=29370)
And then there are some workarounds that I tried out but that didn't work as hoped:
Start a new activity that shows the app logo in onPause so that it's screenshot is shown instead of the actual activitie's one. But the new activity takes too long to open and it disrupts the user experience.
Set the activitie's content view to an image of the app logo in onPause. That seemed like a great solution to me. Unfortunately, the screenshot for the recent tasks screen is taken at an unspecified time. During testing the app logo quickly appears before the app is closed when pressing 'Home' but the resulting screenshot shows the activity a short time before that.
Removing the sensitive data from the widgets (e.g. textView.setText("")) has the same problem of screenshot timing just mentioned.
Any alternative ideas or solutions to the listed workarounds?
I looked into this a couple of months ago for the same purpose as you.
Unfortunately, I had to conclude that it is simply not possible. I dug through the android source code and confirmed it.
There is no callbacks or methods from android that allows you to customize it (that works anyway). Besides FLAG_SECURE, this part of the code does not accept any input or change.
OnPause and similar lifecycle methods are called too late (the screenshot is taken already). All lifecycle methods that would hint that you're about to go into the background runs too late.
The image you see in the recent tasks is an actual screenshot - and thus isn't affected by changes you do (too late) to your view. That means you can't modify your view just-in-time (like making it invisible, replacing with something else, adding SECURE_FLAG, or any other obstruction of the view). As an aside, these images can be found on an emulator at /data/system_ce/0/recent_images.
The only exception is using FLAG_SECURE, which will prevent the screenshot from being taken of your application. I experimented with setting this FLAG in onPause and removing it in onResume, however as mentioned already these lifecycle methods runs after the screenshot is taken already, and thus had absolutely no effect.
As discussed in How to change the snapshot shown by recent apps list? there used to be a callback that you could use to customize the thumbnail: onCreateThumbnail. However, this does not work and it is never called. To be clear, the callback is still there, it is simply never called by the OS. The fact that it stopped working is poorly documented, but apparently was silently deprecated/removed in 4.0.3
As for the thumbnail itself, it is a screenshot taken serverside. It is taken before onPause is called (or in fact before any callbacks indicating that your activity is about to go into the background is called).
When your app does go into the background, your actual view is animated (to get that zoom-out transition). That animation can be affected through changes you do in onPause (if you're fast enough that is) (I experimented with setting opacity to 0 on the window among other things). This will however only affect the animation. When the animation is finished, the view is replaced by the screenshot taken earlier.
Also see these questions that discuss this:
When does Android take its recent apps switcher screenshot?
Show custom application image in task manager on ICS or JB
Android never call method onCreateThumbnail
Currently (28/10/2020) is impossibile customizing app thumbnail in recent apps screen.
As explained by #Dellkan in the previous answer, the onCreateThumbnail method is not called anymore by the OS.
Unfortunately, also the suggestion to create a kind of launcher/splash screen without the FLAG_SECURE flag to let the app take a screenshot of that activity is not working, because the screenshot is taken on the activity you see and not at the launch of the app.
You cannot even customize the color of window background when using FLAG_SECURE as reported here.
How about implementing a layout overlay on top of your entire activity?
Make it transparent, it's click-through by default, so no negative impact on UX while in use.
In onPause() set a half-transparent, blurred image as the background of that layout, the data will be scrambled behind it. In onResume() change the background to fully transparent again. Voila.
It might be faster than other types of overlays. The positive side effect is, if you do the unblurring as a short animation effect when the user goes back (with a proper library that uses C++ instead of Java), it might even look cool and the users wouldnt even mind seeing it.
I haven't tried this myself, but it's something you haven't tried yet.
Since onPause is called to late, I use WindowFocusChangeListener to observe when the Fragment loses focus. At this moment we can hide all view which show sensitive data:
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.getViewTreeObserver().addOnWindowFocusChangeListener(new ViewTreeObserver.OnWindowFocusChangeListener() {
#Override
public void onWindowFocusChanged(boolean hasFocus) {
// hide sensitive data when window moves to background (before system screenshot is captured)
myViewWithSensitiveData.setVisibility(hasFocus ? View.VISIBLE : View.INVISIBLE);
}
});
There is a way to customize it. You need your Activities with sensitive data to FLAG_SECURE in onCreate before you setContentView. Then you need an empty Activity, which renders whatever you want to have as the customized thumbnail. This usually is some sort of splash screen. This new Activity needs to be the launcher and is the only Activity not FLAG_SECURE. This Activity is launched and in onResume starts your actual Activity with the sensitive data.
Android OS will take a screenshot of that new Activity at the beginning of your App. Unfortunately the users will also see this Activity for a short moment. Since every other Activity is FLAG_SECURE, Android OS will use the only available screenshot it made at the beginning.
Was looking for a solution and found some dirty things in case you don't want to use 'FLAG_SECURE'. It doesn't give a nice picture but protects data and doesn't prevent making screenshots for the user while they are in the app.
protected void onPause () {
this.getWindow().getDecorView().getRootView().setScaleX((float)200);
this.getWindow().getDecorView().getRootView().setScaleY((float)200);
super.onPause();
}
protected void onResume () {
this.getWindow().getDecorView().getRootView().setScaleX((float)1);
this.getWindow().getDecorView().getRootView().setScaleY((float)1);
super.onResume();
}
I think this can only achieve through BroadCastReceiver but there is no receiver present. So therefore you first disable default screenshot functionality in android and then implementing your own functionality to take screenshot and before taking screenshot you should blur your secure information.

How to monitor if my App is paused or resumed?

I have an Android app where I want to track when the app is paused or resumed.
paused: User pressed the home button and the app is still running in the background.
resumed: app runs in background and user opens the app.
How can I being notified when my app was paused/resumed?
paused: User pressed the home button and the app is still running in the background.
I am going to guess that the initial state is that one of your activities was in the foreground at the time the HOME button was pressed.
On the whole, there is no notion in Android of an "app" being in the foreground or the background, though we sometimes use that phrasing as shorthand for other scenarios.
Whatever activity was in the foreground will be called with onPause() and onStop() when the user presses HOME, but those events are also called in many other scenarios (e.g., user presses BACK). onUserLeaveHint() will be called when the user presses HOME but not BACK, but onUserLeaveHint() is not called in other scenarios (e.g., incoming call screen takes over the foreground). Whether onUserLeaveHint() will meet your requirements, I cannot say.
resumed: app runs in background and user opens the app.
onStart() and onResume(), at minimum, will be called on your activity that takes over the foreground. Those will be called at other times too, such as when the activity is coming onto the screen for the first time. There is also onRestart(), which will be called only if the activity is being started after having been stopped (i.e., after a prior onStop() call), which will weed out the newly-created-activity scenario. However, onRestart() will be called in other scenarios as well, such as part of a configuration change (e.g., screen rotation).
In general, what you are seeking is not really part of the Android architecture. You may need to approach your problem in some other way.
In you Android activity you can override the onPause and onResume methods.
See the documentation on Lifecycle Callbacks for a list of other lifecycle callbacks that you can implement.

Refresh of screens on backgrounding and foregrounding an application

I am working on an Android app with multiple activities. When moving from one activity to another in certain cases I want to refresh the display but not in others.
One case is where I background the application and foreground it again. When I foreground it, I want to refresh everything on the screen depending on which activity
I backgrounded to begin with. How can I do this? I am unfortunately a bit new to Android so some appropriate basics where applicable would also be helpful.
http://developer.android.com/guide/components/activities.html#SavingActivityState
You can use onSaveInstanceState() to save information before onPause() is called. In onResume() you can use the saved info as a case in a switch statement or some conditional to refresh what you want.

Android screensaver in app if it's not used

I created an android app. The screen never turns off:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Now I want to show a picture if the screen is not pressed for 5 minutes or something else. The app should not be closed, when pressing on the image the app should be open.
How can I realize that?
I would discourage you from taking this approach. Users expect to have a consistent user experience between various apps on their devices, and likely have a preference to how their device sleeps, either by having specified a sleep timeout or displaying a daydream as introduced in Android 4.2.
If you'd like to provide users with the option to display a screensaver associated with your app, I suggest including a Daydream in your app and otherwise acknowledging the user's preferences.
That being said, if you cannot use Daydream, you could observe if the app is being used or not. Two things come to mind:
Have the root view of your activity intercept touch events to observe if any of its children have been touched.
Observe the activity's onPause() and onResume() to acknowledge that the activity is still being displayed.
You could then invoke a Runnable by posting it to a view using postDelayed(Runnable action, long delayMillis), being wary to remove it when the activity is paused or the timer should be reset using removeCallbacks(Runnable).
I solved the problem!!!
I used that event:
public boolean dispatchTouchEvent(MotionEvent ev)
{
super.dispatchTouchEvent(ev);
// cancel my Timer
return true;
}
Thanks!!

How do i start activity when screen goes off?

I have an application which is doing its work only when devices screen goes off. I have set up broadcast receiver (Intent.ACTION_SCREEN_OFF) and it works fine. I got record in my LOGCAT always when devices screen goes off. So far so good.
My onReceive code from ACTION_SCREEN_OFF uses some code calculating stuff, and all executes fine (When screen goes off). So far so good.
At the end of my onReceive code i'm starting new activity, but the onCreate of targeting activity is NOT always executing. (For example on my HTC Desire 2.3.7 works fine. On HTC Wildfire S 2.3.5, Xperia Arc S 2.3.4 it does not execute, and on Samsung Galaxy ACE 2.2.1 , it depends. Sometimes is executing sometimes isn't). My LOGCAT shows that onReceive executed till the end but Activity was not started. I'm using following code to starting this activity:
Intent startH_Activity = new Intent(getApplicationContext(), HandleActivity.class);
startH_Activity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startH_Activity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(startH_Activity);
The important things to note:
I said that on some phones mentioned above it doesn't work. Well its not working immediately, like it should (when SCREEN_OFF fires ). Always activity starts like.. 10-15 minutes after screen went off (which is not acceptable)
When screen goes off and activity does not start , if I press POWER button on device it immediatelyfires off my target activity and app is working like normal. (again this is not acceptable. It should fire automatically).
When ANY device is connected to the PC it works like it should. Activity starts immediatelyafter screen goes off.
After reading a lot of Stack Overflow I realized that this is because MAYBE device goes to sleep. That's why it works like normal if i press POWER button, or Wakes up automatically after 10-15min because it synchronizes or something. So I used WAKELOCK.
//OnCreate Service
powMan = (PowerManager) getSystemService(POWER_SERVICE);
wake = powMan.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
//OnReceive (screen goes off)
wake.acquire();
But still without a success. (Am i doing wrong?) Even wake lock is acquired my activity won't show up on these devices.
Basically what I'm asking. How do i open usual activity when screen goes off? Or at least how to turn screen ON if activity won't start (remember, pressing on power button shows up my activity
I would really need some help now, because I'm getting really frustrated about this. Thank you for help.
You should be starting Service instead of Activity when screen goes off.
I would register yet another broadcast receiver and start the Activity when screen goes on (Intent.ACTION_SCREEN_ON).
Two issues:
1) How do you pass data from one receiver to another? What comes to my mind is either starting a Service or saving the information to a file (or shared preferences).
2) What happens if you have two events that must start an Activity? (E.g. "something started" and "something finished".) You must resolve this, either showing both events or just the latest one.
Note that the user can turn the screen on and off without unlocking the screen (Intent.ACTION_USER_PRESENT), and that if the phone is configured not to lock the screen, Intent.ACTION_USER_PRESENT does not happen.

Categories