catching back press outside of activity - java

I've spent a few hours looking for this. My test device is a nexus 6, though it has been tried on android 4.4 and 5.0+ as well.
Basically I want to catch a user's click of the onBackPress, but I want to do this outside of the activity. Say I've got an object that is initialized and while its running, It is to handle onBackPress, until the its killed.
I've looked into setting an onKeyListener to the contentView but that does not work at all (I figured as much, but its worth a shot).
Any idea how to do this (again, outside the scope of overriding in the activity)?

I cannot imagine something like that being possible since it would be a rather large security risk if a regular application could just catch user input outside of its scope.
The only hardware button I know of that you can detect being pressed even when your activity is not running in the foreground is the camera button, since pressing that generates an Intent. http://developer.android.com/reference/android/content/Intent.html#ACTION_CAMERA_BUTTON

Related

Split screen, restart activity when move size app

I have a problem when starting my app and another one in split screen mode, when my app's window is enlarged over 2/3 size, my main activity restarts, how to prevent it? When I change the size by less than 1/3 nothing happens .... it doesn't restart. Sorry i didn't provide the code.
Activity restarts are common in Android. They happen whenever there's a configuration change. That includes resizes. You can override that behavior in the manifest (at the cost of being unable to switch layouts at different sizes), but you're better off just being able to support them.
To turn them off in the manifest, use android:configChanges="screenSize" on the appropriate activities. This will cause Activity.onConfigurationChanged to be called instead when the screen is reized.

Why sometimes my Android App restart without a logic explanation?

I'm developing a scientific app in Android Studio. It works smoothy.
The set of source code files is not small, but, as I don't have practically user interface, there is only one activity and there is no intent.
All initialization code is inside OnCreate. Most of times, my app preserves all data, when he gets out of the foreground.
However, maybe (I cannot find a pattern of this event) he loses all data and restart (shows a white screen for 2 / 3 seconds), even if the cell phone don't enter in lock screen and there are just 2 apps running.
There are situations that I comute for another app (like WhatsApp) and resumes for my app, and my data was gone. The app restart again.
There is no error message, no logcat. Nothing.
Mostly, when I lock the screen and enter again, all my app data is there.
PS: My orientation is locked.
PS 2: I've read all related question and there is no hint for me. Based in one answer, I've tried to put in onCreate the following code.
if (!isTaskRoot() {
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {
finish();
return;
}
No changes for me.
Update:
I've stumbled in solution. it can be read in my own answer. it's related to undesired back button effect for one-activity-app (read here and here ).
For me, as my application has only one activity, back needs to be like a home button: exit the app but preserve all activity data. My app has a real exit button, where the user shows that really wants to do this.
It's my first app that I developing in mobile world and, for extension, Android world
Some problems seems to me like that it is only possible find the solution if one has a hint about its solution. it's a contradiction. One doesn't know but has to know to solve that don't know!
And, in this situation, it's not the case. No hints. Just question marks.
Before, I had not noticed any pattern. People sometimes act so automatically ... However, suddenly the penny dropped.
I've stumbled in solution. Fortunately!
Not in a million years could I suppose that if someone has an activity and presses Back button, (right button in the bottom), you practically quit the application, even if it remains as a running app for the left button in the bottom (app switcher button)
When I've noticed it, I start to research with another focus. And I've discovered two sources: Disable back button in android and Android - Simulate Home click
So the solution is simply to make the Back button act like the Home button (middle button in the bottom). Return to the home screen without losing application data.
And this is done simply by putting in the onCreate, for my only activity, the following code.
override fun onBackPressed() {
val i = Intent(Intent.ACTION_MAIN)
i.addCategory(Intent.CATEGORY_HOME)
startActivity(i)
}

android app stop variable from resetting - lifecycle

I have an app that has a few screens.
The Main screen automatically opens a "new" screen if it's the first time the user opens the app.
I then set a boolean variable (on the Main screen) keeping track of this.
The intention is if the user goes back to the Main screen, the code that opens the "new" screen can be skipped.
The problem is that the variable keeps getting reset on OnCreate.
So, I added some code to use SharedPreferences.
This works; however, I want to clear the variable when the app exits.
(I want the "new" screen to open every time the app opens the first time).
So, looking at the lifecycle I tried both onStop and onDestory.
The SharedPreferences are cleared but... not when the app is exited; but when the "new" screen appears.
Am I looking at the lifecycle wrong?
Is there some sort of global variable I can declare that only lives while the app is open?
This functionality is the requirement, so I cannot change it.
You should use onSaveInstanceState and onRestoreInstanceState, they will keep the boolean alive if your activity calls onCreate but not if you exit and come back later.
See this answer for implementation:
Saving Android Activity state using Save Instance State
You can define the variable in the line one of ur whole code, that way it will only reset when the app is opened again.

App crashing after sleeping

I'm currently following some video tutorials on Android development, and it's gone fine up until the 8th tutorial, we make a splash screen, and after 2 seconds it's supposed to go to the main application. The problem is, after 2 seconds, the screen goes black and the app crashes.
Logcat Output
I followed the tutorial exactly as he did it, the only difference with mine is that I'm developing for 4.2 instead of 2.2.
I'm also using my Galaxy Note 2 instead of an emulator to test.
I did it all as he did, and pressed play, the app opened on my device and the splash screen was there for 2 seconds (i set it to 2000) and then the spinner (which I added) freezes. The screen goes black, and then it crashes and says "Unfortunately, The Basic Series has stopped."
I've tried both sleep(2000) and Thread.sleep(2000).
Code for the AndroidManifest.xml and the main.java are here
If anyone is curious, this is the video I was following.
avoid splash screens except if u really need them to do some preparation operations..
avoid Thread sleep otherwise you will face ANR crashes (use post runnable with delay instead)
avoid reading this hint: What is your activity class name? Menu, menu, MENU or uNeM?
not so good tutorial IMHO
not honorable mention: if u really want to do it in this (btw not recommended way) check video at 8:48 and try to add catch clause and log exception.
If you need to do some initializations , use AsyncTask , or a thread that upon completion will use Activity.runOnUiThread (so that it won't crash when doing UI operations).
If you need to just show the splash screen and close it after some time , use Handler.postDelayed .
In all possible solutions , don't forget to cancel them upon onPause/onDestroy (depending on what you do/need) , so that if the user has left the app (exited or left it in the background) , it won't open up with the new activity when he resumes it (or worse , show it while it has gone to the background) .

Preventing tilt from calling onCreate, and time measurement of an activity running.

Hi and thanks in advance for your time and attention.
I actually have 2 questions i'm not too sure about building an android app. Pretty basic stuff I believe:
1) My app is fully on Horizontal mode, like AngryBirds for example. When it starts the user figures out he should hold the phone like that, if he isn't already. And that is setup in the manifest for all the activities and works fine. but is there a way to prevent the physical device tilting to call onCreate again? can i override it's method or whatever? the reason i'm asking, is because i have a few ButtonViews that after you click on them, change their picture. i am using onRetainNonConfigurationInstance() to save the array of those ImageButtons, and i even mark the ones changed with the ImageButton setTag() and getTag() methods, so when onCreate is called because of the device tilt it gets the saved array from getLastNonConfigurationInstance() , but i've been trying to make it work for quite some time now and I just can't get it right. After the device tilt (I'm actually using the emulator so it's Ctrl+F11 but i believe it will happen with a device as well) all of the ImageButtons loose their pictures.. Long story short - are there better ways of doing this that you can recommend or is preventing the tilt from doing anything is possible or better?
2) What is the best way to know how many seconds the user has been on a screen? I tried to use two longs that i get via SystemClock.currentThreadTimeMillis() as follows: get the starting time onCreate, and the ending time on the method i call to move to the second intent just before i startActivity. but I think because they are called from different threads, the endingpoint - startingpoint is not correct. What is the way to insure both of the methods of SystemClock.currentThreadTimeMillis() are called from the same thread, the main thread of the activity that i'm stopwatching? Is there a better way to go around this?
Thanks again.
You are doing the right to handle orientation change. Please refer to this link http://developer.android.com/guide/topics/resources/runtime-changes.html . This will help you to get it working.
Good way would be to count the time between onResume and onPause. OnCreate is not called all the time if you are resuming activity.
1) You can try adding the property android:configChanges="orientation" to your activity in the manifest file. It worked for me when my dynamic Action Bar tabs got messed up upon rotation of the screen.
You need specify orientation in android manifest for each of your activities, it will not call onCreate then. android:screenOrientation look at http://developer.android.com/guide/topics/manifest/activity-element.html
User see and interact with your activity starting since onResume and ends on onPause. Other time it does not guarantee that user actually see and can click on something in the activity. System.getCurrentMillis() is good enough.

Categories

Resources