android-lifecycle

Why do OnCreate should be called only once on the start of Activity?

只愿长相守 提交于 2019-12-03 14:43:56
I would like to know, why OnCreate() is called only once at the start of an activity? Can we call OnCreate() more than once in the same activity? If yes, than how can we call it? can anyone give an example? Thanks a lot!!! Why would you want to called it again? unless the activity is reconstructed, which is called by system. You cannot call OnCreate manually , it is the same reason why you won't call setContentView() twice. as docs: onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI,

When is the viewmodel onCleared called

旧巷老猫 提交于 2019-12-03 14:37:25
问题 Are ViewModels independent of activity/fragment lifecycles or just their configuration changes. When will they cease to exist and the subsequent onCleared() method called. Can the viewModel be shared with another Activity ? A situation: Activity1+viewModel1--->(rotation)--->Activity1+viewModel1 --->(launch Intent)--->Activity2+viewModel1 is this sharing possible and is it a good practice. Also, since the app lifecycle callbacks, onPause->onStop->onDestroy is same for both 1.activity rotating

How can I save the application back stack to a bundle?

一世执手 提交于 2019-12-03 14:27:38
I'd like to save the state of my application so that when it is reopened from a closed state, the last fragment is visible and the back stack is preserved. I'd like to preserve this state every time the application is closed, not just on an orientation change or when the system kills the app to free up resources (as is the case when using onSaveInstanceState() / onRestoreInstanceState() . So far I've been able to restore the previous fragment and its state as planned by saving the state to SharedPreferences and restoring it later. However, this does not preserve the fragment back stack, so

Animation at the beginning of activity jumps

坚强是说给别人听的谎言 提交于 2019-12-03 12:15:28
I am working on animating custom Views for my Android app. I have accomplished this via Property Animations and calling invalidate() on the View in the onAnimationUpdate() callback, as per https://developer.android.com/guide/topics/graphics/prop-animation.html : Depending on what property or object you are animating, you might need to call the invalidate() method on a View to force the screen to redraw itself with the updated animated values. You do this in the onAnimationUpdate() callback. My problem is that when these animations are run at the beginning of a newly launched Activity, they

Android scenario where ondestroy() is called without onpause() or onstop()

末鹿安然 提交于 2019-12-03 11:06:04
问题 A few days back I was asked to write down scenarios where ondestroy() is called without onpause() or onstop() being called. Is it possible. If yes please explain. 回答1: If you try below code, you will find a scenario where onDestroy() is indeed getting called while onPause() and onStop() Life-cycle call backs are Skipped. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); finish(); } @Override protected

Having One Instance Of Activity

China☆狼群 提交于 2019-12-03 09:45:35
I have a problem with Activity navigation and the back stack and I was hoping you could clear it up for me. The Problem Root Activity >>> SecondActivity >> HomeButton This then takes me to the home page and from there I select... Gmail >> Message >> Open attachment in my application >> ImportActivity >> RootActivity The outcome is a new task being started and another instance of my application being used. This is unacceptable as I don't want two separate tasks running I only want one. The Desired Outcome What I want to happen is when the user opens the attachment and ImportActivity finishes

Android Application class lifecyle documentation

半腔热情 提交于 2019-12-03 07:40:35
I'm trying to find the official documentation about the Android Application class lifecycle. Apparently, for what I found on StackOverflow here and here the Application class can be killed if the system needs memory. Even this tutorial says so. But few things irritates me a bit about this: I can't find an official documentation telling me that yes, the Application class can be killed on low memory. I can't find any official diagram representing the Application lifecycle neither. I can't find any proper callback to use when the Application class is killed except onLowMemory() . Does it mean

When to use and when not to use a Service in Android

自作多情 提交于 2019-12-03 05:11:36
问题 I have been developing for Android for little less then 2 years, and I am still puzzled by this seemingly simple question. When should one implement a service? From my experience there are some rare cases but I am questioning this because on every phone there are quite a lot of them running and I doubt it's just a poor application design. This is essentially core of my question but following are some of my experiences and thoughts about the subject which can explain my question in more detail

android: when to use onStart(), onStop()?

╄→гoц情女王★ 提交于 2019-12-03 05:08:10
I've read several posts that describe the difference between onStart() and onResume() : onStart() is called when the activity becomes visible, onResume() is called when the activity is ready for interaction from the user. fine. I've always just added code to onPause() and onResume() , and never bothered with onStart() and onStop() . Can anyone give some concrete examples of what you might do in onStart() , vs. onResume() ? Same goes for onStop() and onPause() , how is onStop() useful? I must be missing something fundamental here. onStop() will (for example) be called when you leave the

Properly skip login activity if already logged in

你说的曾经没有我的故事 提交于 2019-12-03 03:13:38
问题 My launcher icon currently starts the login activity. I've stored the logged in status in SharedPreferences. Is there any way to properly skip the login activity and go straight to the main activity without any UI glitches. All existing solutions involving finish() in onCreate() cause the login activity title to be briefly visible or some other brief blank screen UI glitch. 回答1: Have a launcher acitivy with no UI that decides to open the MainActivity or the LoginActivity. You can declare no