CircularProgressIndicator in Dialog stuck or freezes after onPause of Activity - java

I created a custom class which extends Dialog class directly, where i gave it my custom layout with a simple TextView and a progress bar CircularProgressIndicator from google's material library. Everything is working as expected after calling show() method and then calling dismiss() after my work done.
But that progress bar stucks or freezes when i am minimizing the app (hence onPause()) and return back to my app or activity, however dismiss() still works when task done.

Related

Adding a widget from activity to another activity's in Android Studio

I'm developing a task reminder app and still not familiar with Android Studio. The layout was an Activity Form with a floating action button that opens the second activity for inputting name, date, time, etc., the second activity has a 'create' button that supposes to add a checkbox widget to the main activity form and I was having difficulties to make it work unlike at java where you just have to add it directly on its respective container.
By the way, this is how I created the object for my checkbox...
private class task extends androidx.appcompat.widget.AppCompatCheckBox{
task me;
public task(#NonNull Context context) {
super(context);
me=this;
me.setText(taskName);
}
}
it's not correct to add a widget in another activity in real-time, there are many solutions for this case, the simplest way is to start the second activity with startActivityForResult and in the second activity, return changes to the first activity and on onActivityResult handed if there are any changes or not, if yes create your widgets based on the returned result.

How to populate activity when splash ends without delay?

I am Loading all the data on splash Activity and when all calls return I want to populate another activity but keeping the splash activity until the main activity is populated.
I tried using AsyncTask but still not working, I have a delay when switching from the splash activity to the main activity.
I expect to go from splash activity to another Activity without Delay and the activity to be populated.
There are 2 solutions to your problem:
Disable animation when starting your MainActivity and try to minimize the consuming tasks in that activity.
Combine SplashScreen to your MainActivity as a View, after finishing loading all necessary tasks, just hide that SplashScreen View
You have to upload the part of your code where you are calling the intent to the other activity before we can actually understand what is wrong. However, ensure you are not doing too much activity on the application main thread since this can make your UI unresponsive and make the app slow. See this for reference =>
https://medium.com/#yossisegev/understanding-activity-runonuithread-e102d388fe93
https://developer.android.com/topic/performance/threads
start new Android Activity is so slow
You can also make your app call the new activity on a separate thread to ensure all the data it needs to pass to the other activity is received before navigating to the new activity and also ensure you are not doing too many resource consuming task in the onCreate method of the new activity you are calling

Suspend Android app's main thread while showing the ProgressDialog

When my Android app is starting up needs to load a lot of data from the database during onCreate in the firstly loaded activity. I would like to show a ProgressDialog for that. ProgressDialog however can't be shown in the main thread, so one must use AsyncTask or new Thread. But that also means, that the activity continues to be initialized as the main thread goes on.
Basically, I need to show the ProgressDialog or a kind of its equivalent while processing in the main thread (not in AsyncTask).
Is there a way to do it?
ProgressDialog however can't be shown in the main thread, so one must use AsyncTask or new Thread.
How do you come to this conclusion? ALL UI stuff is shown in the UI Thread, thus also the ProgressDialog. It needs to be created and invoked inside the UI Thread to work or else your App crashes.
First you need to check on onCreate() if your stuff is already loaded and if not, show a ProgressDialog, load stuff in the background and then do a post in the UI Thread to dismiss the ProgressDialog and show the results.
That's how it usually works.
The Main/UI Thread is responsible for drawing the UI, and hence, the ProgressDialog itself . So you can not block it and hope that he is going to draw the UI. You should move the initialization stuff inside AsyncTask's doInBackgroud, and move on with the other suff after onPostExecuted is called
You should load the the data with the Thread (ASyncTask) you should display your ProgressDialog with "onPreExecute()" update it with "onProgressUpdate()" and finish the dialog with "onPostExecute()" all of them is running on UI thread already.
You will never be able to show progress because your view of activity have not created, because you read from database in onCreate methode after reading the database onCreate method finshes and now your view inflate and so on . . .

How can I trigger onPause and onResume?

On the click of a button, I want onPause to be called. On clicking back/after a time interval, I want onResume to be called.
I find that system alerts, dialogs etc. do not call onPause and onResume on show and cancel.
I need to do this to verify a functional test. If i show a transparent activity, it's lifecycle methods would be called which would contradict what I am trying to test.
I only want to verify onPause and onResume on my current activity. Is this possible?
You need to create a new transparent (or partially transparent) Activity to trigger onPause. Simply adding a View won't do anything.
Then, hitting back, or finish-ing the newly created transparent Activity will trigger the onResume method.
If you want to test onStop functionality in addition to onPause, you'll want your new Activity to be opaque and the full screen size.
You should test it this way -- let the Android system trigger these methods to be called, instead of manually running them. Hence, you can let a button click launch the new Activity, then either hit back to close it, or add a button to the new Activity to finish it.

when to use WindowPopup or Dialog or Fragment Dialog

I have read in the documentation and this is what i have learned
Dialog
Base class for Dialogs.
Note: Activities provide a facility to manage the creation, saving and
restoring of dialogs. See onCreateDialog(int), onPrepareDialog(int,
Dialog), showDialog(int), and dismissDialog(int). If these methods are
used, getOwnerActivity() will return the Activity that managed this
dialog.
Thus FragmentDialog is just a subclass of Dialog which is in a fragment and thus disconnected from the creating Activity life cycle
A fragment that displays a dialog window, floating on top of its
activity's window. This fragment contains a Dialog object, which it
displays as appropriate based on the fragment's state. Control of the
dialog (deciding when to show, hide, dismiss it) should be done
through the API here, not with direct calls on the dialog.
and WindowPopup is disconnected from creating Activity's life cycle (so what is it differ from FragmentDialog?)
*Is WindowPopup more like a toast message going on top of all activities even when app is invisible?
*when should i use each of them?
*other differences?

Categories