oncreate

Android onCreate or onStartCommand for starting service

非 Y 不嫁゛ 提交于 2019-11-26 18:29:56
Usually when I create an Android service I implement the onCreate method, but in my last project this does not work. I tried implementing onStartCommand , and this seems to work. The question is: when I have to implement a service which method is required? Which methods I have to implement? onCreate , onStartCommand , or both? And what is the role of each? onCreate() is called when the Service object is instantiated (ie: when the service is created ). You should do things in this method that you need to do only once (ie: initialize some variables, etc.). onCreate() will only ever be called

Android - Activity Constructor vs onCreate

痴心易碎 提交于 2019-11-26 17:18:00
I understand that Android Activities have specific lifecycles and that onCreate should be overridden and used for initialization, but what exactly happens in the constructor? Are there any cases when you could/should override the Activity constructor as well, or should you never touch it? I'm assuming that the constructor should never be used because references to Activities aren't cleaned up entirely (thus hampering the garbage collector) and that onDestroy is there for that purpose. Is this correct? I can't think of any good reason to do anything in the constructor. You never construct an

What's onCreate(Bundle savedInstanceState)

假装没事ソ 提交于 2019-11-26 17:04:09
Can anyone help me to know about the Bundle savedInstanceState in onCreate(Bundle savedInstanceState) I am newbie in Android. I try to understand it from developer.android.com. But I am not able to understand. Can anyone simplify it? Dhruv Gairola If you save the state of the application in a bundle (typically non-persistent, dynamic data in onSaveInstanceState ), it can be passed back to onCreate if the activity needs to be recreated (e.g., orientation change) so that you don't lose this prior information. If no data was supplied, savedInstanceState is null. ... you should use the onPause()

onCreate flow continues after finish()

不打扰是莪最后的温柔 提交于 2019-11-26 16:48:23
问题 I would like to finish an activity from inside the onCreate method. When I call finish() , onDestroy() is not immediately called, the code keeps flowing past finish() . onDestroy() isn't called until after the onCreate() closing brace. Per the onCreate() description at developer.android.com/reference. You can call finish() from within this function, in which case onDestroy() will be immediately called without any of the rest of the activity lifecycle (onStart(), onResume(), onPause(), etc)

Android service onCreate is called multiple times without calling onDestroy

限于喜欢 提交于 2019-11-26 16:17:43
问题 In my app, I use a service to communicate with our server. The Service spawns several message queue threads to deal with tasks with different priorities. This model has been used for about one year without big issues. However, recently, I found some time the onCreate of my service class are called multiple times. onDestroy is never called between two onCreate calls. Therefore, I did not get chance to kill existing threads. Once this behavior happens, the service has duplicate threads inside.

Start an Activity with a parameter

冷暖自知 提交于 2019-11-26 11:04:14
I'm very new on Android development. I want to create and start an activity to show information about a game. I show that information I need a gameId. How can I pass this game ID to the activity? The game ID is absolutely necessary so I don't want to create or start the activity if it doesn't have the ID. It's like the activity has got only one constructor with one parameter. How can I do that? Thanks. Wroclai Put an int which is your id into the new Intent . Intent intent = new Intent(FirstActivity.this, SecondActivity.class); Bundle b = new Bundle(); b.putInt("key", 1); //Your id intent

Android: When is onCreateOptionsMenu called during Activity lifecycle?

谁说胖子不能爱 提交于 2019-11-26 09:27:09
问题 I put a couple of breakpoints in onCreate (one at the beginning, and one at the end of the method), and I also put one at the beginning of onCreateOptionsMenu . The onCreate method is called first, and before it finishes onCreateOptionsMenu is called. I\'m trying to separate the Fragment navigation code in my app, so I have a couple of objects that I delegate onCreateOptionsMenu to depending on if the app is running on phone/tablet (I\'m using screen size to determine this, my layout file for

Android onCreate or onStartCommand for starting service

◇◆丶佛笑我妖孽 提交于 2019-11-26 06:25:13
问题 Usually when I create an Android service I implement the onCreate method, but in my last project this does not work. I tried implementing onStartCommand , and this seems to work. The question is: when I have to implement a service which method is required? Which methods I have to implement? onCreate , onStartCommand , or both? And what is the role of each? 回答1: onCreate() is called when the Service object is instantiated (ie: when the service is created ). You should do things in this method

Android - Activity Constructor vs onCreate

帅比萌擦擦* 提交于 2019-11-26 05:22:06
问题 I understand that Android Activities have specific lifecycles and that onCreate should be overridden and used for initialization, but what exactly happens in the constructor? Are there any cases when you could/should override the Activity constructor as well, or should you never touch it? I\'m assuming that the constructor should never be used because references to Activities aren\'t cleaned up entirely (thus hampering the garbage collector) and that onDestroy is there for that purpose. Is

What's onCreate(Bundle savedInstanceState)

瘦欲@ 提交于 2019-11-26 05:17:09
问题 Can anyone help me to know about the Bundle savedInstanceState in onCreate(Bundle savedInstanceState) I am newbie in Android. I try to understand it from developer.android.com. But I am not able to understand. Can anyone simplify it? 回答1: If you save the state of the application in a bundle (typically non-persistent, dynamic data in onSaveInstanceState ), it can be passed back to onCreate if the activity needs to be recreated (e.g., orientation change) so that you don't lose this prior