activity-lifecycle

onStop vs onDestroy

你离开我真会死。 提交于 2019-12-04 02:36:12
I have tried to research exactly when the onDestroy method is called for an activity, but I've read some confusing and conflicting information. In general, my question is: under what circumstances is the onDestroy method actually called on an activity? More specifically, if I have two activities, activity A and activity B, if activity A is running and I create an intent and switch to activity B, is activity A only stopped, or is it destroyed? JoaoBiriba Like stated in the official documentation : onDestroy() The final call you receive before your activity is destroyed. This can happen either

Android Storing Socket.io object for multiple activities

自古美人都是妖i 提交于 2019-12-03 20:48:28
I am making my first Socket.io based android application. The socket sends and receives data from a web service. There are a number of screens in the application for different features. How do i use the same socket connection in these different activities. I have tried setting and storing the Socket Object in the Application class and it appears to work well but when the application goes into the background and left there for some time the application is killed and the socket object is then NULL causing the aoo to crash with a null pointer exception. public class MyApplication extends

Distinguish between pushing “home” button and opening another Activity

核能气质少年 提交于 2019-12-03 15:46:26
I have three activity: - SplashActivity - MainActivity - PlayerActivity Of course the app starts with SplashActivity, then it starts MainActivity and closes. MainActivity in some moment starts PlayerActivity and goes to backstack. (MainActivity is alive but is onStop) Then I need open MainActivity and set PlayerActivity to background (PlayerActivity is alive but is onStop). Then I need open PlayerActivity again and set MainActivity to background. So PlayerActivity and MainActivity often gets onPause() and onStop() without onDestroy when app switch one to another and back. I need finish all

Android View - What is automatically saved and restored in an Activity

﹥>﹥吖頭↗ 提交于 2019-12-03 14:12:35
I am a beginner with Android. In Android, some generic elements can be automatically saved/restored in onSaveInstanceState / onRestoreInstanceState . For example, an EditText saves/restores the Text property, a RatingBar saves/restores the Rating property... I see from some tests but I can't find anything about this in the documentation. How can I know what is saved/restored implicitly, without my intervention? For example, where I can find that the EditText.Text is automatically saved/restored? I specifically don't want test all properties. Edit from JRG answer : https://developer.android.com

Save interface (Listener) in onSaveInstanceState

£可爱£侵袭症+ 提交于 2019-12-03 13:25:30
问题 SaveInstanceState For data like Integer, Long, String and else are fine, I just put it in the bundle and get it back once the onCreateView gets called again. But my fragment also has listener like following, public class SomeFragment extends Fragment { public interface SomeListener { public void onStartDoingSomething(Object whatItIsDoing, Date when); public void onDoneDoingTheThing(Object whatItDid, boolean result); } private SomeFragmentListener listener; private String[] args; public static

Lifecycle of a session cookie in an Android WebView / CookieSyncManager

谁说胖子不能爱 提交于 2019-12-02 20:55:53
I have an Android application which makes requests to my webserver via both a WebView and an HttpClient. I sync cookies between the two using a CookieSyncManager. So far, so good. When my application starts (inside onResume()), I run a piece of logic similar to the following: if ( appHasBeenIdleFor30Minutes() ) { CookieManager cookieManager = CookieManager.getInstance(); cookieManager.removeSessionCookie(); CookieSyncManager.getInstance().sync(); } This correctly resets any session cookies that were set from the user's previous session. My question is: will this behavior happen periodically on

Detect when android app go backrgound

一笑奈何 提交于 2019-12-02 01:10:05
In my application I need to detect whether my application is going to background or is switching to another activity of the same application... I know that I have to use the onPause method... but how can I distinguish the two cases? Thank you! private static boolean isApplicationGoingToBackground(final Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(context

Are there any callback methods for when the quick settings dropdown menu is opened?

雨燕双飞 提交于 2019-12-02 00:50:01
When I pull down the quick settings drop down menu while my app is opened and in the foreground, are there any callback methods called? I tried a few quicks tests and Activity.onWindowFocusChanged(boolean hasFocus) might work for you. It fires when I drag down quick settings but it will also likely fire for other reasons, like AlertDialogs. onWindowFocusChanged void onWindowFocusChanged (boolean hasFocus) Called when the current Window of the activity gains or loses focus. This is the best indicator of whether this activity is visible to the user. 来源: https://stackoverflow.com/questions

Android: Check if activity is destroyed by a system from service

社会主义新天地 提交于 2019-12-01 23:52:37
I have a service listening to some events from server. A service has START_STICKY flag that makes him restart when it's killed by OS. When service receive an event i have two scenarios. First, if activity isn't killed i need to send result to local broadcast receiver and update UI. Second, if it's killed by OS i want to recreate it and send data in bundle. But i don't know how to recognize that android killed my activity. onDestroy activity event doesn't come in this situation. @Override public void onComplete(CurrentOrdersResponse response) { if (response == null) { return; } boolean

What Android methods are called when battery dies?

我与影子孤独终老i 提交于 2019-12-01 16:38:49
问题 When the battery on my Android device dies what methods in the Activity and Fragment classes (if any) are called during the "Powering Off" stage of the device? Also, if a user is currently looking at a screen in my app and they hold the power button and choose switch off, do the events called/not called coincide with when the battery is depleted and shuts down automatically? OnPause? OnStop? OnDestroy? OnDetach? Bonus: Will I have enough time to save a small amount of data to a web server? To