I'm developing a android 2D game in android studio.
I have a problem with my activity Lifecycle.
My question is this:
I have 2 activities. One named LauncherActivity, and the other named MainActivity.
When I first launch the app, the LauncherActivity launches, and everything works just fine.Then, when I go the the MainAcitivty, which is the activity that hold my game, meaning that it has the line : setContentView(new GameController(this)), everything still works fine. Then when I go back to the launcher activity, everything still works fine, but NOW when I'm trying to go back to the game, the MainActivity, in the second time, then the Lifecycle is as follow:
onCreate(),onStart(),onResume(),onPause(),onStop(),onResume().
This is ruining all my game, because I have specific things in my onStop() method, which I can not allow them to be executed when the activity launch.
Does someone know how to fix this ? What am I doing wrong ?
If I was not clear, please ask me and I will explain.
Android call onStop when the activity is no longer visible. Please review document. https://developer.android.com/guide/components/activities/activity-lifecycle.html
I hope it can help you.
Related
I have a little problem. I have a Bluetooht activity which contains two fragment. The goal of the first fragments is to know if the bluetooht is activated on the phone (Nothing complicated).
To do something clean I would like to update my fragment when the user opens the phone taskbar since he can activate bluetooht.
The problem is, I can't find the methods to call when opening or closing the taskbar.
I have already tried onResume but obviously it is not calling.
Does anyone have an idea?
thank you in advance
I am working on a minimal open-source Launcher (wikipedia) for Android.
When I run it on a phone, put that on standby and unlock it after 4 - 6 hours, all I get is a black screen.
I hope this has some general solution and is not only caused by my code.
That way answers to this question will also be useful to everyone else on here.
Relevant mentions for my code
In the Android Manifest XML I declared the main activity to be a launcher (home screen). The main activity (MainActivity.kt) has an onCreate function, only refreshes a clock and checks for some OnTouch events. The full project code can also be seen on GitHub.
Go to the related issue on Github.
You should try implementing onResume() if you haven't already. It is possible that the app has not stopped and as such onStart() would not be called when you unlock your phone.
Where an app has paused, it would just call onResume() instead. You can try putting your code here to recreate the views as needed. You can also save any relevant user information in onPause() and reload it in onResume(). This will allow you to preserve a consistent user experience and avoid memory leaks caused by, for example, referring to objects that no longer exist.
Let me know if it works, good luck.
The Activity Lifecycle
I'm trying to add functionality to my foreground service notification to just open the app when the notification is clicked. I don't want to run any activity - I just want to display the app.
Basically I want to achieve same thing that can be achieved in Android by bringing list of apps and clicking on one).
I've checked lots of posts on StackOverflow, but all of them use activity class in Intent, while I don't care about any particular activity - I just want to bring the app up.
You must have a launcher activity right? so you always can start an activity with your launcher activity and use a launch mode in the AndroidManifest.xml to define to bring it up if it already has been created.
The company I started working for recently gave me their current Android project to read and understand. I am having a really hard time which activity starts or intends to start which one, basically not being able to understand the flow of things (Apart from the launcher activity)
Is there an easier way to determine the activity flow of the application apart from searching for Intent calls in every file, each file being large no. of lines of code?
Can you build and run the app so you can see and feel what different screens do?
Find main activity in a manifest. Search in main activity startActivity() or startActivityForResult(), navigate to the next one and so on. This way you can draw graph, you may try any scripting language to automate the process.
The project may have fragments, so you will have to find FragmentManager and commit().
Basically I want my app to start running in the background when the ACTION_MEDIA_BUTTON is clicked. I've seen other apps do this, so it must be possible. I just don't know how it is possible. Some kind of special manifest code?
Currently my app can receive intents from the ACTION_MEDIA_BUTTON when it has been opened (and even running in the background) via intentfilter that I register in Java code of the main activity (when the app is first opened). But how would I have this button just... open up the app?
For reference, the following app can already do this functionality: https://market.android.com/details?id=com.kober.headset
Much appreciated, thanks.
Use a BroadcastReceiver and register for the intent. See this Android Developers Blog post for more details.