App crashing after sleeping - java

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

Related

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

Loading Images,Videos in background

I am trying to make an App which is going to display Some Images and Videos. So I am planning to add a splash screen of around 2seconds. After 2 seconds the user will be taken to the Main Screen of the App.
I want to start the loading of the Images, Video when then user is at the splash screen itself so that the user should wait for the least time when he is at the Main Screen.
So the loading will be started at the Splash Screen and then after two second the user will be taken to main screen irrespective of the completion of the loading.
Now since this involves two activities should I use a Async task or should I Use a service with an Async Task(For the callback of completion of code) within it?
Which one would be better. Also in Android 8.0 are there any restriction in using Services?
I think using a Async Task between two screens may cause Memory leak if not coded properly.
Any help would be really grateful.
EDIT: My app is having one more feature hence cannot make the user wait in the Splash Screen till the loading is over.
It is not very good to use AsyncTask for sharing results between 2 activities, because AsyncTack created in Splash activity will be destoyed (stopped) when switched to Main activity. Better to use service in this case and Main screen will subscribe for result.
So basically you want to start the download in the splash screen and continue the download in the activity that follows. In this way, you still have to implement a loading animation. In your case, I would recommend finishing your splash screen, as soon as everything is downloaded. In that way, you don't have to download anything anymore inside the app's lifecycle.
AsyncTasks continue to run even after switching to a new activity. You can try the following flow:
1. Splash screen
2. Trigger Async Task
3. Main Activity
4. Show Images/Videos
The only catch is, you will not be able to fix a time for #2 to complete to be able to start #4. This is the nature of AsyncTasks. You can workaround by using the OnPostExecute within AsyncTask.
Example: OnPostExecute call another method that will enable a button. Users can click on the button to view Images/Videos. But then, this might not be a good user experience to see some button suddenly getting enabled.
In that case I would rather create some Singleton with own Handler (that works in separate Thread), that will be started at Splash screen. After Main screen will start, it should ask that Singleton about respective data or should sign himself for receiving that data.

catching back press outside of activity

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

How to execute code everytime app opens Android

I want to execute a piece of code (say, for example, display a Toast) every time that the app is opened. So far I have managed to do this every time the app is launched by putting the code into my MyApp.java file that extends Application.
However, if I press the homescreen or back out of the app and then go into it, the message doesn't reappear. It only does when I relaunch the app. Any idea how to do this?
EDIT:
basically im asking how to execute code everytime the whole APP is brought to foreground (this can be first time open, after another app was used, after user backed out of app, etc). Where would I place onResume code? It wouldn't be in a particular activity, would it, since I want it to apply when entire app appears in foreground, not just particular activity.
You can try writing that code in your activity's #Override-d onResume() method.
The only way to do this is,
Determine which app is currently in the foreground
.Follow this discussion for getting an idea for the best way to do it.
[Determining the current foreground application from a background task or service
Suppose, if the function name is 'getCurrentForgroundApp()',
You need a service to execute getCurrentForgroundApp(); every one second
(1-second interval is depending on your purpose, can be lower or higher).
Now, you can identify which app is running foreground in every second.
So, check if your app is the one running foreground. If true, then execute the toast or code you need.
This is how app-locker apps showing lock screen over selected apps, whenever they come to the foreground.
You have to use the onResume callback:
Android API
Example of use from previous SO question
In activity class:
#Override
protected void onResume() {
super.onResume();
//your code here
}

android eclipse: switch activity briefly shows previous activity screen

I am sure this is a basic question, but I'm developing an Android app using ADK and Eclipse.
When I try to transition Activities, the screen goes black and then briefly shows the previous activity screen before "flipping" to the new screen.
I don't have any custom transitions; I'm using the default.
I don't have much going on in my onCreate event (EXCEPT: I load my background image during onCreate! Could it be this?)
I really am looking at a very snappy transition, like a game like Words with Friends, where it appears to switch "instantly".
This depends on what phone you're using since different phones use different anamations. Try to call finish(); on every activity but your main one for example.. Or finish them all when the user switches activities

Categories

Resources