android-lifecycle

androidx.appcompat.app.AppCompatActivity doesn't implement LifecycleOwner

喜你入骨 提交于 2019-11-26 23:36:41
问题 I know looks like some stupid error, but I cannot find it. In my project I have access to LifecycleOwner from my Fragments but not from Activity. Here is my build.gradle implementation project(':domain') implementation project(':data') implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines

What is the benefit of using Fragments in Android, rather than Views?

旧城冷巷雨未停 提交于 2019-11-26 23:28:35
When developing for Android , you can set your target (or minimum) sdk to 4 (API 1.6) and add the android compatibility package (v4) to add support for Fragments . Yesterday I did this and successfully implemented Fragments to visualize data from a custom class. My question is this: what is the benefit for using Fragments as opposed to simply getting a View from a custom object, and still supporting API 1.5? For example, say I have the class Foo.java: public class Foo extends Fragment { /** Title of the Foo object*/ private String title; /** A description of Foo */ private String message; /**

Prevent multiple instances of my Android application composed of a single activity

ε祈祈猫儿з 提交于 2019-11-26 20:57:53
问题 I have an Android application which is composed from a single Activity . How can I assure that only one instance of my application (== Activity ) will exist in a given time? I got to a situation in which I succeeded to open multiple instances of my app by clicking on the app icon multiple times (this doesn't reproduce all the time). 回答1: change your manifest like this: <activity android:name="com.yourpackage.YourActivity" android:label="@string/app_name" android:launchMode="singleTask" >

How to execute background task when Android app is closed / set to background?

ぃ、小莉子 提交于 2019-11-26 20:24:47
问题 My Android 4+ app is connected to a custom web service that is used to sync data every few minutes. To make sure, that the online data is always up to date, I want to trigger the sync when ever the app is closed / send to background. Under iOS this is quite easy: Listen to applicationDidEnterBackground: in the AppDelegate Use beginBackgroundTaskWithName: to start you long running background task and to avoid being suspended while the task is still running How to do this on Android? First

Main difference between Manifest and Programmatic registering of BroadcastReceiver

て烟熏妆下的殇ゞ 提交于 2019-11-26 20:24:14
问题 I am trying to understand the main differences between registering a BroadcastReceiver in the Manifest and registering it programmatically... My understanding is basically as follows (would appreciate someone correcting my points if I am missing something). Registered in Manifest: The OS will magically find and instantiate your class if needed, calling the onReceive() method, regardless what the running state of your application was Your receive will only get called once per broadcast (i.e.

What is the difference between setContentView and LayoutInflater?

会有一股神秘感。 提交于 2019-11-26 19:51:08
问题 I am creating a tabs list with several fragments. I have noticed that, in the main activity, I used setContentView to get the layout xml and use findViewById to get the corresponding UI element config. setContentView(R.layout.fragment_tabs); mTabHost = (TabHost)findViewById(android.R.id.tabhost); mTabHost.setup(); mTabManager = new TabManager(this, mTabHost, android.R.id.tabcontent); However, in the different fragment class, I have to use the inflater instead. View v = inflater.inflate(R

What is the difference between onPause() and onStop()() of Android Activites?

混江龙づ霸主 提交于 2019-11-26 18:55:32
问题 From android doc here http://developer.android.com/reference/android/app/Activity.html, it said 'Activity comes into foreground' will call onPause() , and 'Activity is no longer visible' will call onStop() . Isn't 'Activity comes into foreground' same as 'Activity is no longer visible'? Can you please tell me what is the difference between them? 回答1: No, if some activity comes into foreground, that doesn't necessarily mean that the other activity is completely invisible. Consider the

Android checking whether the app is closed

…衆ロ難τιáo~ 提交于 2019-11-26 18:34:19
问题 I have an android application i need one function or any broadcast receiver that can check if the app is closed.. i don't need to call on destroy in every activity (there is about 20 activity into the app) i tried to add this function in Application class public class ApplicationLifeCycleManager implements ActivityLifecycleCallbacks { /** Manages the state of opened vs closed activities, should be 0 or 1. * It will be 2 if this value is checked between activity B onStart() and * activity A

View pager and fragment lifecycle

不想你离开。 提交于 2019-11-26 18:04:48
问题 I have a ViewPager that loads three pages at a time. If I swipe from page 1 to page 2 then to 3, the first page(fragment) goes to onPause() . Then, if I swipe to the second page, 1st page comes to onResume() even though the page 1 is still not visible to the user. So, my question is: how to distinguish between the first and second page in code? For example, if I have to run a piece of code when the fragment is visible, how is that done? 回答1: The FragmentPagerAdapter keeps additional fragments

Public static variables and Android activity life cycle management

删除回忆录丶 提交于 2019-11-26 16:23:01
问题 According to the documentation the Android OS can kill the activity at the rear of the backstack. So, say for example I have an app and open the Main Activity (let's call it Activity A). In this public activity class I declare and initialize a public static variable (let's call it "foo"). In Activity A's onCreate() method I then change the value of "foo." From Activity A the user starts another activity within my app called Activity B. Variable "foo" is used in Activity B. Activity B is then