android-lifecycle

Use application class as singleton

做~自己de王妃 提交于 2019-12-11 01:40:56
问题 In Android we often have to use Context classes. When a class or method requires a Context we often use Activites or Services for such arguments. The following code is from a website I found but I do not know if this is good practice and if it is okay to use this solution: public class MyApplication extends Application { private static MyApplication singleton; @Override public void onCreate() { super.onCreate(); singleton = this; } public static MyApplication getInstance() { return singleton;

Pass onActivityResult() data to the same Fragment which is not yet ready

倾然丶 夕夏残阳落幕 提交于 2019-12-11 01:31:57
问题 I am using a Fragment to start a new Activity using startActivityForResult() , I am getting the result (Bundle) in onActivityResult () method.Since onActivityResult () called before onResume ().I want to make sure, I keep/save the Bundle properly so that when Fragment's onResume() gets called, I get the kept/saved result to perform further action. What are the different ways to achieve this. I tried using getArguments()/setArguments(), but that seems to be not the right way to achieve this.

Send data to a parent activity onBackPressed [duplicate]

微笑、不失礼 提交于 2019-12-10 23:54:56
问题 This question already has answers here : setResult does not work when BACK button pressed (10 answers) Closed 6 years ago . Is there a way to send updated data to a parent activity when back is pressed? I'd like to update the data in the bundle, but I don't see how I would access it. For example, I have a gallery activity that opens an image viewer. Say a user scrolls through a dozen images and then backs out to the gallery. It would be ideal to update the focal image in the gallery with the

Android Stop Service on Crash

让人想犯罪 __ 提交于 2019-12-10 21:36:04
问题 I am running a Service and am wondering in the rare case that my app crashes will it automatically kill my Service too? I don't want it continuing if this happens. If not, is there a way to do this such as in the onDestroy() method? 回答1: I've done a bit of research, and I think I have a complete answer to your question. Subclassing Service is fine, assuming you're not using a Remote Service (a Service in a different process.) Assuming your Service is in the same process as your Activity ,

Android - Double lifecycle sequence from landscape to portrait

六月ゝ 毕业季﹏ 提交于 2019-12-10 11:49:16
问题 I'm monitoring an Activity lifecycle to learn more on it, so I put a Log.d() call in almost every method of the cycle. I'm watching particularly the sequence: [A]onSaveInstanceState() - onPause() - onStop() - onDestroy() - onStart() - [B]onRestoreInstanceState() See Activity Lifecycle Switching a 2.2 Froyo emulator with Ctrl-F11 from portrait to landscape I can see one round from [A] to [B]. What is strange to me is that switching back from landscape to portrait I can see two round from [A]

Why does my Activity leak?

混江龙づ霸主 提交于 2019-12-10 10:54:44
问题 I have an Activity that has an IntentFilter and a BroadcastReceiver, and I register them in my onCreate() . IntentFilter filter = new IntentFilter(ACTION_RCV_MESSAGE); filter.addCategory(Intent.CATEGORY_DEFAULT); receiver = new MessageReceiver(); registerReceiver(receiver, filter); But upon ending my app, Logcat says: 07-03 01:38:19.567: ERROR/ActivityThread(304): android.app.IntentReceiverLeaked: Activity com.intentservicetest5.IntentServiceTest5Activity has leaked IntentReceiver com

Failure delivering result ResultInfo | java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

China☆狼群 提交于 2019-12-10 10:28:20
问题 I have Simple app first I display MainActivity then after MainActivity became visible I display TransparentActivity after that onClick I kill TransparentActivity and I create and display dialog. During last step I get Error Error Failure delivering result ResultInfo{who=null, request=1234, result=-1, data=Intent { }} to activity {com.example.kylu.layout/com.example.kylu.layout.GuidePhotoAlbum}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState MainActivity

How to deliver and persist changes to the UI from an asynchronous task hosted by a retained fragment?

余生长醉 提交于 2019-12-09 23:02:53
问题 Using a retained fragment to host asynchronous tasks is not a new idea (see Alex Lockwood's excellent blog post on the topic) But after using this I've come up against issues when delivering content back to my activity from the AsyncTask callbacks. Specifically, I found that trying to dismiss a dialog could result in an IllegalStateException. Again, an explanation for this can be found in another blog post by Alex Lockwood. Specifically, this section explains what is going on: Avoid

Using Service as singleton in Android

丶灬走出姿态 提交于 2019-12-09 15:55:27
问题 Is it a bad practice to create a Service that works as a singleton? I mean a Service that is never stopped and that contains some private data that some other engines and Activities would use, so the Service could have something like: public class CustomService extends Service { private List<Profile> mProfiles; private static CustomService instance; public static CustomService getInstance() { if(instance == null) { instance = new CustomService(); } return instance; } public List<Profile>

Is Fragment.onStop() guaranteed to be called?

情到浓时终转凉″ 提交于 2019-12-09 13:23:08
问题 From the table at this link: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle we can see that an Android Activity is not killable until after onStop() has been called (for Honeycomb and beyond). Do we have the same (documented) guarantee for Fragments? Would greatly appreciate someone pointing me at documentation where it is clearly stated. Edit: As has been pointed out [Fragment.onStop] is generally tied to Activity.onStop of the containing Activity's