activity-lifecycle

Why implement onDestroy() if it is not guaranteed to be called?

吃可爱长大的小学妹 提交于 2019-11-27 11:53:34
According to the android Activity Lifecycle, the only callback guaranteed to be called (if an activity ever leaves the Running state, which is typically expected) is onPause() . So, I must assume that there are scenarios in which it makes sense to implement onStop() and onDestroy() although they are not really guaranteed to be called. I understand that onStop() should be implemented when it's possible for an activity to return to the Running state via the Stopped state (why would it do that instead of returning directly is a different question). But the need for onDestroy() , when I can place

Is onResume() called before onActivityResult()?

烂漫一生 提交于 2019-11-27 10:13:03
问题 Here is how my app is laid out: onResume() user is prompted to login If user logs in, he can continue using the app 3. If the user logs out at any time, I want to prompt login again How can I achieve this? Here is my MainActivity: @Override protected void onResume(){ super.onResume(); isLoggedIn = prefs.getBoolean("isLoggedIn", false); if(!isLoggedIn){ showLoginActivity(); } } Here is my LoginActivity: @Override protected void onPostExecute(JSONObject json) { String authorized = "200"; String

Simulate killing of activity in emulator

ⅰ亾dé卋堺 提交于 2019-11-27 07:39:05
I would like to test out onSaveInstanceState and onRestoreInstanceState for my app on the emulator. I have found this , which says that we could simulate this during orientation change, but I'm storing some of my variables on the application level (subclass android.app.Application ), so the orientation change does not wipe out the variables. So my question is, how can I simulate the low memory situation thus killing of my activities? Hope I've made myself clear. Thanks You can pause your application (by pushing the Home button, simulating a call, whatever). Then kill the app's process through

Why does calling getWidth() on a View in onResume() return 0?

家住魔仙堡 提交于 2019-11-27 05:11:22
Everything I've read says you can't call getWidth() or getHeight() on a View in a constructor, but I'm calling them in onResume() . Shouldn't the screen's layout have been drawn by then? @Override protected void onResume() { super.onResume(); populateData(); } private void populateData() { LinearLayout test = (LinearLayout) findViewById(R.id.myview); double widthpx = test.getWidth(); } Onik A view still hasn't been drawn when onResume() is called, so its width and height are 0. You can "catch" when its size changes using OnGlobalLayoutListener() : yourView.getViewTreeObserver()

Is there a function in Android analogous to “int main” in C/C++ which contains the program's main loop?

懵懂的女人 提交于 2019-11-27 01:40:06
问题 Normally in a C or C++ program there's a main loop/function, usually int main () . Is there a similar function that I can use in android Java development? 回答1: As far as an Android program is concerned there is no main(). There is a UI loop that the OS runs that makes calls to methods you define or override in your program. These methods are likely called from/defined in onCreate(), onStart(), onResume(), onReStart(), onPause(), onStop(), or onDestroy(). All these methods may be overriden in

Activity lifecycle - onCreate called on every re-orientation

我怕爱的太早我们不能终老 提交于 2019-11-27 00:10:11
问题 I have a simple activity that loads a bitmap in onCreate . I find that if I rotate the device I can see from the logs that onCreate called again. In fact, because all instance variables are set to default values again I know that the entire Activity has been re-instantiated. After rotating 2 times I get an FC because not enough memory can be allocated for the bitmap. (Are all instances of the activty still alive somewhere? Or does the GC not clean up fast enough?) @Override public void

finish() and the Activity lifecycle

。_饼干妹妹 提交于 2019-11-26 21:17:19
问题 I'm learning Android programming for a class, and I have a quick question about how finish() fits into the Activity lifecycle. When you make a call to finish(), what lifecycle callback is started? I presume it's onPause(), then onStop() and onDestroy(). Is this correct? Really, I just want to make sure that it doesn't jump straight to onDestroy(). 回答1: You are correct. onPause, onStop, onDestroy. Here are the docs. 回答2: Really, I just want to make sure that it doesn't jump straight to

Is Activity.onStop() guaranteed to be called (API 11 +)

巧了我就是萌 提交于 2019-11-26 20:17:39
问题 It is unclear (to me, at least) from the documentation whether Activity.onStop is guaranteed to be called. There are two places with seemingly contradictory information. Javadoc for Activity.onStop: Note that this method may never be called, in low memory situations where the system does not have enough memory to keep your activity's process running after its onPause() method is called. Documentation (in particular 'Killable' column) for Activity class http://developer.android.com/reference

Life cycle of Android Activity after pressing Back button

送分小仙女□ 提交于 2019-11-26 19:19:26
问题 I am little confused between the life cycle of two activities. Suppose I have Activity A and Activity B. B is called From A i.e A ----> B . Now currently B is on the screen and I pressed back button. Here I want know:- is there any memory still available for B (Active) or B 's memory is flushed(Inactive). 回答1: Suppose there is an activity A, from which you launch activity B. If, while in activity B, you hit the back button, you are popping activity B off the stack and B will not be in the

OnPause and OnStop() called immediately after starting activity

醉酒当歌 提交于 2019-11-26 19:02:37
问题 I have an activity that needs to turn screen on(if offed) when it is started. So in onCreate, I have: this.getWindow().setFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); Using this with help of wakelock in broadcasr receiver , I am able to