when to use WindowPopup or Dialog or Fragment Dialog - java

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?

Related

How do I not reset my fragments when configuration changes happen?

So I have this activity that creates 4 fragments for me, which are a bottom navigation bar.
when I change to dark mode or light mode, the fragments reset and take me back to the "main fragment" which is basically home in the bottom navigation bar.
so my question is how do I stay in the tab I was in while changing UI mode? while of course changing the UI mode of the fragments as well
The best aproach is to let activity recreate itself with its fragments, some configuration changes might be required to show correct UI. And its always possible that your user will move your app to the background, system will kill it after a while and then when your user will go back to it - you will be back with your problem of recreation. I am talking about it to prevent you from using hacks like configChanges in androidmanifest which prevent Activity recreation on configuration change event.
But back to the main point:
when configuration change destroys your Activity, fragments will be automatically recreated when new instance of Activity will be created. But its up to you to save some of its state. I am not sure how your UI and code looks like. The very basic way to save and restore state (like active tab in your question) is to use: Activity.onSaveInstanceState(Bundle) / Activity.onCreate(savedInstanceState: Bundle?) and Fragment.onSaveInstanceState(Bundle)/Fragment.onCreate(savedInstanceState: Bundle?) mechanism. You save relevant variables in bundles in onSaveInstanceState, and later in onCreate restore this state using savedInstanceState variable, but only if its non null - which happens when Activity is created for the first time.
This is a broad topic, you can find more on this here:
https://developer.android.com/topic/libraries/architecture/saving-states
https://developer.android.com/guide/fragments/saving-state

OnResume() is not called of the home activity when back pressed from a fragment

I am working on an android application. I have cardviews for multiple fragemnts. I have put these cardviews in the main activity. So when I pressed the cardview a fragment will open. So i will make these cardviews visibility as gone. But on back press the onResume() is not called. Hence the layout is not getting visible. I tried many things but did not work. What should I do?
From what I understand, you have an activity with a fragment. Then you load a new fragment and then you press back. This interaction will not fire the activity's onResume() because the activity has never been stopped. If there is logic you need to perform between fragments, it needs to be somewhere else. I cannot provide any more details without some more information.
I think first of all use the navigation on Android Jetpak

Connection between activity and layout? How to change layout? How to start and destroy an activity?

I'm a total beginner with Android and Eclipse and I have few questions that will help me understand the philosophy of Android:
The activity does all the work "behind the scenes". Activities are connected to a layout in the XML file. When the activity is started, if declared in setContentView method, the connected layout will be shown. Activity can be independent, without any layout, it can be invoked by another activity and will do all the work without showing any layout.
Activity is something like a php file which is invoked by my submit button in HTML, and the layout is .HTML which shows elements.
Am I right with this one?
For example, if I want to change the layout of my app, I want to show Layout2.xml when clicking button in Layout1.xml. Then I have to destroy the activity which is connected with Layout1.xml and start the activity which is connected with Layout2.xml? Is this correct? Is there any better way to do this?
How can I (by which method) destroy/stop a certain activity?
Thank you in an advance.
The best bet is to read the Android documentation regarding activites at http://developer.android.com/reference/android/app/Activity.html
I will answer your specific questions here though
An Activity is a window that the user can see (or a hidden window if there is no layout defined). It deals with the logic of a part of the app that the user can see and interact with. If we take the MVC model (Model View Controller), the Activity is the controller, in terms of it controls which data from the Model is shown on the View (the xml layout).
If you want to show a new window/screen/activity you do not need to destroy the current one. You can open a new activity whilst keeping the old one in the background (in the back stack). With the use of fragments, you can have multiple fragments in an activity so rather than changing activities, you can change fragments in a single activity. For more information about fragments take a look at http://developer.android.com/reference/android/app/Fragment.html.
This point relies heavily on the activity lifecycle. When an activity is destroyed, it means it is finishing and this can be done by the user pressing the back button whilst on the activity, the activity calling finish() on itself or by the Android operating system destroying the activity because memory is required elsewhere (this can happen when the app is in the background).
When we say an activity is stopped, it means that the activity is no longer visible to the user. This can be the case where the activity is in the back stack (another activity is in front of it) or if the app has been put into the background.
This is a brief answer to your questions but I highly recommend you read the Android documentation to gain better knowledge.

To overlay a custom dialog for parent Activity to all views including child activity in android

I need to show a custom dialog overlays over all child activities. But that dialog's base context is background. Is it possible?. This is used for custom lock to whole application after wake up(Screen On). I have a broad cast receiver to manage that. But that receiver is registered through parent activity. When the screen go to sleep from parent activity that dialog/process executes correctly. But the screen go to sleep from child activity that dialog shows background of child activity, when I will back to the parent activity then that shows.
I think this occurred on dialog's context.
You need to use an Activity instead of a Dialog. Set your Activity theme to emulate a Dialog in the manifest.
Example - AndroidManifest.xml:
android:theme="#android:style/Theme.Dialog"
Then, you will have more flexibility and control over its display.

What is the difference between activity visible liftime and foreground lifetime?

We know that the android activity cycle has multiple phases.
between onStart() and onStop() is called the visible lifetime
between onResume() and onPause() is called the foreground lifetime
What are the the key difference between them? Please give examples if possible.
If I display an Activity on the screen and the user is interacting with it, it is both in the foreground and visible.
If I start another Activity, which is transparent and shows a dialog box over the previous Activity, then the new Activity (the dialog box) is in the foreground and the old Activity is not in the foreground but still visible.
between onStart() and onStop() called visible lifetime that mean that the activity is visible either entire activity or partially visible and the user can see it on the screen and interacte with
between onResume() and onPause() called foreground lifetime that your activity is full visible and running and have full focus .
UPDATE
partially visible for example if another activity come in front of the current one and it only display a dialog and a transparent background . the user can see that activity but cant interact with it
Foreground Activity: You’d think what the user is currently interacting with would be the most important thing to keep alive.
Visible Activity: You’ll find that there are situations where your activity can be visible but not in the foreground. A simple example is when the foreground activity starts a new activity with Dialog theme or a translucent activity. Another example might when you invoke the runtime permission dialog.
Please find below link for better understanding
https://medium.com/androiddevelopers/who-lives-and-who-dies-process-priorities-on-android-cb151f39044f
The Visible Lifetime: Although this is termed the "visible lifetime", the app may not be directly visible and interacting with the user at any one time if it is not in the foreground. The feature that distinguishes this lifetime is that, even if not in the foreground, the app maintains resources such that it can instantaneously return to the foreground.
The Foreground Lifetime: During foreground lifetime the activity is in front of all other activities and interacting with the user.

Categories