android-lifecycle

Which function is called when application is removed from task manager

假如想象 提交于 2019-11-30 09:55:36
问题 I need to make status of user offline. When I press home button onStop() is called, that's fine. When I press back button onDestroy() is invoked. But when I close the app from recent apps by swiping it, onStop() or onDestroy() isn't called. I need to know when the app is closed from recent apps to do something (e.g make user offline). 回答1: Make a service : public class MyService extends Service { private DefaultBinder mBinder; private AlarmManager alarmManager ; private PendingIntent

AsyncTask will always run even if app is destroyed?

人盡茶涼 提交于 2019-11-30 08:19:50
I have an application and because you can't do network operations on the main thread I'm using AsyncTask , so the question is once I execute() the AsyncTask and right after that I finish() the activity, and maybe the user will finish() the whole app, so what I'm wondering is: Will AsyncTask always finish doInBackground() and onPostExecute() even if the app is closed as long as execute() was called when the app was running? wtsang02 You will be able to test this. And yes It does. If execute was called you can see Asynctask will still execute UNLESS it does something to the forground or UI

RuntimeException:ClassNotFoundException android.arch.lifecycle.ProcessLifecycleOwnerInitializer

↘锁芯ラ 提交于 2019-11-30 08:06:38
问题 I am getting this error only on Android SDK < 5.0. So 4.0, 4.2, 4.3 ect. Anything running Android 5.0+ works flawlessly. Any ideas? Crashes on launch. Following this guide for setup -> https://developer.android.com/topic/libraries/architecture/adding-components.html App.java public void onCreate() { super.onCreate(); ProcessLifecycleOwner.get().getLifecycle().addObserver(new AppLifecycleListener(this)); registerActivityLifecycleCallbacks(this); } AppLifecycleListener.java public class

Measure view in fragment

守給你的承諾、 提交于 2019-11-30 08:02:19
问题 I need to know ImageView width and height. Is there a way to measure it in fragment ? In standard Activity I use this : @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); image.getWidth(); image.getHeight(); } But where can I use image.getWidth(); and image.getHeight(); in fragment ? 回答1: Use the GlobalLayoutListener. You assign the listener to your ImageView in onCreateView() just like you do in that answer in the link. It also is more

ExoPlayer Restore State when Resumed

戏子无情 提交于 2019-11-30 07:30:22
I have implemented the Player and now there is a problem. When the video is playing and if the app is closed and resumed, the video screen freezes. I even saw the ExoPlayer Demo Activity from Google for better understanding but I could not get through it for implementing in my app. I have attached the Player Activity here and for the full code, I am sharing the GitHub repository for the complete set of files used. RecipeStepDetailFragment.java package com.example.android.recipe.ui; import android.content.Context; import android.content.res.Configuration; import android.net.Uri; import android

Differentiate between Android killing the app and user swiping it off on the recent apps list

拈花ヽ惹草 提交于 2019-11-30 06:59:28
I am working on a project, where while being on a specific Activity we show a local sticky notification. That should also be the case when the app is minimized. What I have to accomplish is to remove the local notification whenever the app is killed (by Android, because of memory lack or by the user, with a swipe from the recent apps list). Usually onDestroy would be called whenever Android takes the Activity to open some space. That is fine in one of the cases, however swiping an app from the recent app lists doesn't call the onDestroy and the sticky notification stays. What I did is, I

Android how to stop refreshing Fragments on tab change

折月煮酒 提交于 2019-11-30 06:16:27
问题 I have the following code : MainActivity.java package com.erc.library; import java.io.BufferedInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import android.app.ActionBar; import android.app.ActionBar.Tab; import android.app.FragmentTransaction; import android.content.SharedPreferences; import android.content.res.Resources; import android.graphics.Color; import android.os.Bundle; import android

Can some draw lifecycle of Fragment and its parent FragmentActivity?

谁都会走 提交于 2019-11-30 05:24:31
In Android docs, I found a specs on Activity lifecycle and on Fragment lifecycle individually, but never together. It does not seem obvious as I attached a debugger to FragmentActivity which hosts my fragment, and the life cycle is more than crazy. It looks like activity finishes first and then fragments starts, which is impossible. Fragment's lifecycle Activity's lifecycle Logically, fragment should "jump into" activity's lifecycle after its onResume and it would end before activity's onPause , but it seems it is not happening. Can someone either show me the lifecycle of the fragment in

Leaving android app with back button

送分小仙女□ 提交于 2019-11-30 05:13:38
I want the users of my android app to leave my app when they press back at a certain activity. Can this be done? You can try something like this (shows a dialog to confirm exit): @Override public void onBackPressed() { new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit") .setMessage("Are you sure you want to exit?") .setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); System.exit(0); } }).setNegativeButton("No", null).show(); } It must be noted as it is mentioned in

Best way to implement Socket.io in android

人走茶凉 提交于 2019-11-29 21:01:28
I am planning to implement Socket.io in android by this library for a chat based application. As far as I understood the library seems to be pretty good. I want to know how to maintain a single socket connection throughout the app all the time? Here I have listed out ways to achieve, in which I need the best and stable way. Three ways MainApplication Class extends Application By this we have a good scope that the socket connection is maintained in the main thread ( or application's life cycle) and whenever the socket instance is needed from the activity we can get it easily. But it's main