android-lifecycle

save the state when back button is pressed

吃可爱长大的小学妹 提交于 2019-11-27 14:45:44
I am developing an android app. If I press a back button the state of my application should be saved .What should i use to save the state ..am confused with all of these onPause() , onResume() , or onRestoresavedInstance() ??? which of these should i use to save the state of my application?? For eg when i press exit button my entire app should exit i have used finish() ? public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); s1=(Button)findViewById(R.id.sn1); s1.setOnClickListener(this); LoadPreferences(); s1.setEnabled(false); }

Android: onDestroy() or similar method in Application class

送分小仙女□ 提交于 2019-11-27 14:15:27
I am extending Application class to work with some global variables that need context. I know there is onCreate() method in the Application class that gets called before any other onCreate() in activities, but I would like to know if there is onDestroy() or similar method in the Application class that could be overridden so that I would be able to store variables in persistent memory, unregister listener and send last message to server before app process gets killed? If not, is there any other way to do that? There is no such call back on a production device for the Application class. The

Public static variables and Android activity life cycle management

前提是你 提交于 2019-11-27 13:31:28
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 paused after the user navigates to some other activities in other apps. Eventually, after a memory

What is lifecycle for RecyclerView adapter?

帅比萌擦擦* 提交于 2019-11-27 12:39:43
问题 I'm requesting images from presenter in adapter: @Override public void onBindViewHolder(SiteAdapter.ViewHolder holder, int position) { Site site = sites.get(position); holder.siteName.setText(site.getName()); requestHolderLogo(holder, site.getLinks().getLogoUrl()); } private void requestHolderLogo(final ViewHolder holder, final String logoUrl) { compositeSubscription.add( presenter.bitmap(logoUrl) .subscribe( bitmap -> { holder.siteLogo.setImageBitmap(bitmap); holder.siteLogo.setVisibility

Android - Performing stop of activity that is not resumed

寵の児 提交于 2019-11-27 11:19:50
When I push my app to background, and do some other stuff like whatsapp or sms, onResume it works great. But I recently discovered that when I open/launch facebook app while my app is on background, I don't know what happens... But onResume, the app misbehaves... Don't do what it is required to do, but when I go back to homepage and come back it works fine Please help me out.. how to fix it ??? Logcat with all messages (without filter) 10-15 12:53:59.899: I/Adreno-EGL(32033): Remote Branch: quic/LNX.LA.3.5.1_RB1.1 10-15 12:53:59.899: I/Adreno-EGL(32033): Local Patches: NONE 10-15 12:53:59.899:

Should the call to the superclass method be the first statement?

余生颓废 提交于 2019-11-27 10:49:11
The results of a speech recognition can be read in the onActivityResult(int requestCode, int resultCode, Intent data) method, as shown in this example . This method overrides the same method in class Activity : why is the call to the superclass method not the first statement? @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) { // Fill the list view with the strings the recognizer thought it could have heard // ... } super.onActivityResult(requestCode, resultCode, data); }

Android lifecycle library ViewModel using dagger 2

╄→尐↘猪︶ㄣ 提交于 2019-11-27 10:47:41
I have a ViewModel class just like the one defined in the Connecting ViewModel and repository section of Architecture guide . When I run my app I get a runtime exception. Does anyone know how to get around this? Should I not be injecting the ViewModel? Is there a way to tell the ViewModelProvider to use Dagger to create the model? public class DispatchActivityModel extends ViewModel { private final API api; @Inject public DispatchActivityModel(API api) { this.api = api; } } Caused by: java.lang.InstantiationException: java.lang.Class has no zero argument constructor at java.lang.Class

When exactly are onSaveInstanceState() and onRestoreInstanceState() called?

≯℡__Kan透↙ 提交于 2019-11-27 10:26:55
The following figure (from the official doc ) describes the well-known lifecycle of an Android activity: On the other hand, when the activity is destroyed by the system (for example because memory needs to be reclaimed), the state of the activity is sometimes automatically saved and restored by means of the methods onSaveInstanceState() and onRestoreInstanceState() , as illustrated by the following figure (also from the official doc ): I'm aware that onSaveInstanceState() is not always called when an activity is about to be destroyed. For example, if it is destroyed because the user has

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

Android - How to tell when a specific view has been rendered?

怎甘沉沦 提交于 2019-11-27 08:17:15
问题 I have a regular button that I want to display a timer on. So imagine the text of the button changes every second as a countdown timer. However, I need this timer to only start when the button is fully rendered and ready to go on the screen. Right now I'm starting the timer (which is an AsyncTask ) in the onCreateView() of a fragment. This isn't that accurate, because I do some other loading stuff that I have to have. I could potentially move the timer start at the bottom of onCreateView()