Android activity lags after multiple interactions - java

My applications has multiple activities. They work perfectly fine until you decide to open another activity and go back to the last activity and repeat this process. Basically if you go from activity A to B, than go back to A and repeat this the app starts to lag. More specifically the activity A starts to lag until it is refresh. I have checked with leak canary for memory leak but it doesn't seem to be the case. Any clue about why is this happening?

Related

android studio button sometimes doesn't work

I have a menu activity from which I want to go to the meat of my application. When I press the button to do so, it sometimes opens the main screen within a few moments, sometimes takes longer, and in many cases, doesn't work at all. In the case that it doesn't work, the button will usually work after clicking it again. I have used debug statements to determine that the button is (sometimes) not registering the clicks at all. What could be causing this?
I have this problem sometimes, this happens with me because my crappy mouse, and sometimes it is caused by the emulator which starts lagging with time and not recording my clicks properly, from time to time I just to wipe the data of the emulator to make work properly again.
One good solution to check if your doing things right, is to install your app in a real device and check if the button is working properly.

Does Switching off the Android Phone destroys the activity?

I was playing with the activity life cycle today. I had logged all the lifecycle methods. I was on the MainActivity and then switched off the phone suddenly. I noticed 2 methods being called:
onPause,
onStop,
but onDestroy never got called.
Any take on this?
If by "switched off the phone" you mean you turned off the screen, your results are fairly common. There will be differences between devices, due to OS version and manufacturer changes, so you should not make any assumptions. However, I would not expect your activity to be destroyed.
If by "switched off the phone" you mean you completely powered down the phone, there is no guarantee that the OS will take the time to destroy all running components. Once again, there will be differences between devices, due to OS version and manufacturer changes, so you should not make any assumptions.
Is normal because:
onDestroy() is called before the activity is destroyed. The system invokes this callback either because:
the activity is finishing (due to the user completely dismissing the activity or due to finish() being called on the activity), or
the system is temporarily destroying the activity due to a configuration change (such as device rotation or multi-window mode)
https://developer.android.com/guide/components/activities/activity-lifecycle#ondestroy

Android app can't run in background more than 5 or 15 minutes

I know that it is a real problem with running apps in background for different OEMs, but how can I solve this problem?
My app has a webview integrated from an online radio, everything is ok, but after 5 minutes (in locked screen mode), the player stop playing... I can configure my mobile settings, going to Apps, Special access, Optimize battery usage, search for my app and disable that button, but I win only 10 minutes (15 in total), and the app will stop again...
I found something about services, but I'm a really beginner, I don't understand why should I use a service to run in foreground... I have also a notification icon (+title+message) which is showing on display even if the mobile screen is locked. For my understandings, that means the app is running in foreground and in background. Can't figure out how to solve this.
I'm a beginner, but I want to go with this app in production (Google Play), and I want to be useful, not to be just another app...
I hope somebody will have the patience to respond on my issue. Thank you!
(At least some advice, what should I do...)
You need services to run in the background when your application is not visible to the user. Android automatically kills some apps especially when they are resource intensive. This can also be done by some antivirus software, task cleaners, memory cleaning apps etc.
You need to build your application around these challenges because users will not be required to optimize their settings for your application to run.
This services can be triggered by some android activity lifecycles. When you lock your screen, some life cycle methods like the onPause() and the onStop() could be called in your applications by default. You need to handle these events.
Services
A Service is an application component that can perform long-running operations in the background. It does not provide a user interface. Once started, a service might continue running for some time, even after the user switches to another application. You need to create a service that will perform the tasks you want and periodically update the call back in your application.
E.g. The app may fetch notifications from a remote backend and periodically show them to the user at the notifications panel.
Android activity life cycles
As a user navigates through, out of, and back to your app, the Activity instances in your app transition through different states in their lifecycle. The Activity class provides a number of callbacks that allow the activity to know that a state has changed: that the system is creating, stopping, or resuming an activity, or destroying the process in which the activity resides.
Within the lifecycle callback methods, you can declare how your activity behaves when the user leaves and re-enters the activity.
References
Android Services -> Learn about android services
Activity Life Cycles -> Learn about activity life cycles

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.

How to prevent Android application from loading multiple times?

This must be an easy one but I'm not having much luck searching for an answer.
Apologies if this is a regular question.
If I navigate away from my app I cannot return to it. Starting the app again will load a second instance of it rather than returning to it. If I leave an audio loop running in my app, It's hard to get back in and turn it off.
On startup I'd like the app to destroy any previous instance of itself left running.
I'd also like to try having the app shut itself down when I navigate away (I know it's not the right way to do things but I'd like to try this for my own personal use of the app). Or have the "back" button destroy the app.
Thanks.
Add this to your activity definition in manifest...
android:launchMode = "singleInstance"
How to prevent the activity from loading twice on pressing the button
I have answered such a question,you can declare android:launchMode="singleTask" attribute for your MainActivity in the AndroidManifest.xml file, so that the OS won't create a new instance if there is one running in the background.

Categories