Working with DIalogs in Fragments - java

In my application, I have the Activity and several Fragments (Activity works as Controller and Fragments - as views)
In some of Fragments I need to show AlertDialogs and ProgressDialogs, Activity can change current Fragment.
My problem is: activity can receive broadcasts and C2DM notifications, and when I created AlertDialog, Activity can change fragment, but Dialog stays. So when user clicks on some buttons, app crashes.
DIalogFragments works like a simple Dialog.
Have I dismiss dialog manually or check if fragment is active? Is there any built-in tools?

First of all I might be missing something, no code etc to go by but...
Secondly: maybe you shouldn't be using dialoges? Seems like a cumbersome user interface. Just use fragments for those as well? Though you say you are using DialogFragments so maybe you've already thought about that and use them as "regular" fragments already.
Thirdly: Dismiss the dialogs when the fragment that showed is removed/hidden then? Use the onStop() callback for example in the fragment or a more central place where you are perhaps saving the currently showing fragment and deciding to display a new one.
Dismiss the dialog by calling ´dismiss´ on the Dialog object or Fragment or dismissDialog in the Activity.
See dismissing dialog: http://developer.android.com/guide/topics/ui/dialogs.html#DismissingADialog
You can still call dismiss on those DialogFragments.
http://developer.android.com/reference/android/app/DialogFragment.htm

Related

Android Fragment with ViewPager and SectionsPageAdapter method executed on user clicks tab name to display it or swipes to it

I have been trying to determine why the onPause() and the onResume() methods were not fired when the user switches the the tab to be displayed when using a ViewPager and sectionsPageAdapter.
I assumed that when the user changes the tab being displayed, the onPause() or the onDestroy() methods would be called. Then, if the user returns to the tab, the onResume() method would be called, but it seems it is not the functionality...
is there any way to determine when the user switces a tab and to know to which tab it has been switched? Something like onFragmentBeingDisplayed() or similar.
Thanks!
EDIT: Answer from Suraj Vaishnav
By editing the constructor from the FragmentPageAdapter like this super(fm, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); I could use the desired behaviour on the app
That's because ViewPager loads some fragments on the left side and on the right side of the current fragment. By default, ViewPager loads one on the left side and one on the right side of the fragment. You can change the number via: viewpager.setOfScreenPageLimit(int);
What you thinking is working exactly the same way in androidx's Fragment. Means onPause and onResume will call accordingly. So the first suggestion is to use androix. Check this answer: https://stackoverflow.com/a/57886441/7682257
For some reason, If you do not want to use androidx then in your Fragment, override setUserVisibleHint method, which gives you isVisibleToUser as a parameter.
If you don't want to use Androidx version of Viewpager to get the use of the BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT behavior to fire onResume only on the displayed Fragment then you can instead add an addOnPageChangeListener
You can use the ViewPager.SimpleOnPageChangeListener or your own implementation of the ViewPager.OnPageChangeListener interface.
This PageChangeListener has a onPageSelected method with gets called with the position selected.
While I have given links to the Androidx Documentation this also works in earlier support libraries, just Google have removed the docs for it as they want you to use Androidx
Note this ViewPager.OnPageChangeListener onPageSelected fires at a slightly different time to onResume with BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT. It fires when about 50% of the new tab is swiped on to the screen when onResume fires when 100% of the new tab is on screen.

Hide MainActiviy when DialogActivity starts

I use firebird cloud messaging to receive data in the background, then a dialog (new Activity with Theme.AppCompat.Dialog as style) will appear. My problem is that the surface of the MainActivity is displayed every time in the background. Is it possible to hide MainActivity without terminating it? Someone has perhaps an idea?
When i set in the Manifest at the MainActivity the value
android:noHistory="true"
i looks good, but the notification is then in the app history. this looks bad
I mean when the activity is in Background that the MainActivity is still visible
As you are using an Activity as a dialog, your MainActivity remains in the Activity Stack in a paused state. However, even if you make the Dialog Activity full-screen, it is not guaranteed that MainActivity will be kept alive. The OS can kill the activity anytime if it needs memory for other processes. This is stated on the Activity reference.
Instead you could try one of these alternative solutions:
Use a DialogFragment instead of a new Activity
Use regular Fragments, adding the fragment at runtime, allowing the user to switch between the dialog Fragment and the Main fragment.
Use a FrameLayout, to show/hide the dialog UI inside the same Main Activity layout.
I think are looking for this method
/* When {#link #FLAG_DIM_BEHIND} is set, this is the amount of dimming
* to apply. Range is from 1.0 for completely opaque to 0.0 for no
* dim.
*/
dialog.getWindow().getAttributes().dimAmount = 1f;

Up Navigation: From Activity to Fragment: Android Studio

The basic navigation of my app is a tab bar within one activity in which each tab is its own fragment. I am trying to get the back button to work to go back to the last tab it was on. I can get the back button to work/appear if the activities are going to the first tab by using this in the java class of the activity:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
and this in the AndroidManifest.xml:
<activity android:name=".CreateNewPost" android:parentActivityName=".MainActivity"></activity>
This works fine, however when I go to a new activity from a button within the other tabs, if I use this same method it goes to tab1. How do I tell it to go to a specific tab or the last active fragment?
Any suggestions would help!
When you launch a new activity the old one either paused or stopped , as in either OnPause() or OnStop() method has been called. Then when you navigate back to a previous activity it is either started (OnStart()) or resuemd (OnResume()). And sometime the activity is destroyed and created again when navigating back to it using OnDestroy() and OnCreate() respectively.
The most guaranteed way that I can think of is to store the state of the first activity somewhere persistently and when the user navigates back to it check what has been stored and show that fragment.
You can use SharedPreferences to store this kind of data, see the google developer documentation on that here. Or onSaveInstanceState(Bundle savedInstanceState) check out this SO question on saving activity state. Although from the answers it is clear that onSaveInstanceState should not work in your specific case, but you should look at it any way because it is useful in other cases like changing screen orientation for example.

Architecture for a drawer layout app

I am building an app with a drawer layout similar to the Android Facebook app. I am wondering what the best method for architecture is. Should I have a main activity which is responsible for the action bar, and then have it use fragments to display the content of each menu item, or should I be using one activity to manage the action bar, and then have each menu item kick off entirely separate activities?
I could also imagine building multiple activities, which each have to manage the action bar. This option seems the worst.
You have two architecture options here
MainActivity with Fragments
ParentActivity that handles drawer and lots of Activities that extends this Activity.
I have tried both in different projects and found some things worth sharing.
For me The MainActivity that handles drawer and then using Fragments to fill the display is the best.
You will need to handle callbacks from specific Fragments in your MainActivity and redirect them to the specific Fragment they came from. This is mainly if you use Interfaces in objects lower in the Arcitecture chain since you sometimes need to pass down Activity to certain objects. This generates more code that are not as generic as one might want in top level architecture node.
If you are using a ParentActivity and extending it for each ChildActivity, you can write all specific code in the child, meaning that the toplevel ParentActivity will almost only have generic code.
If you are using the ParentActivity with ChildActivities and you are switching between Activities, you fill get the graphic when an Activity closes and the next opens every time a user switches between navigation objects. If you use Fragments this wont happen as the Fragment will be switched in the background. The user will also experience that the navigation drawer will be closed and recreated each time he clicks on an item there.
Its also unnessecary to recreate the navigation drawer with each click on an item. This is a minus for the ParentActivity approach.
With the ParentActivity approach you will also have to keep track of how the backbutton should function, this will be autoaticly handled for you with Fragments. Also when starting new Activities you have to choose if a new Activity should be created or if the old should be killed etc.
Just my 5c, hopes it helps :)
The best way is to use one Activity with one Fragment per section/view.
Take a look at the design documentation.
Also see the Tutorial and Sample Application. It's fairly straight-forward.
You will have one activity which manages ActionBar, Drawer (ListView!) and Fragment.
Every time it clicks an item in the ListView it updates the fragment with the new view.
If you use different Activities then you should use intents with a very bad effect, use a different activity only if needed (if it's totally unrelated to the current activity maybe?)
Official documentation: http://developer.android.com/training/implementing-navigation/nav-drawer.html
If you got any problem in creating this, online you can found more tutorials but the official is very great.
You should have the activity holding the actionbar & drawer
When using a drawer you should not start new activities from within the drawer but fragment instead
Good post & video about it: https://plus.google.com/u/0/+RomanNurik/posts/3nMVVQzUTjG
another good read: http://www.androiduipatterns.com/2013/05/the-new-navigation-drawer-pattern.html
And finally this is a must see also (check the slides or the video): https://plus.google.com/u/0/+NickButcher/posts/1jeyV2n1ZpM

to launch a Dialog from a Service, should I use Activity or View?

Say I'm running a service and need to pop out a dialog. As we know, it's impossible to launch a dialog directly from a service, therefor we need to launch and activity ( or view ) and then have it launch our dialog.
The dialog, as well as the activity launching it, should NOT obstruct whatever is below it, i.e. what's on screen should not turn gray and any buttons, that are outside of the dialog, should still be clickable.
Can this be achieved with using an activity or would the activity block the view under it anyway?
If so, guess I would have to use a view... since I haven't worked with views before, what would be the right way to initialize it, so that it won't obstruct whatever is under it?
Thanks!
You could have it launch as an activity using the Dialog theme:
http://developer.android.com/guide/topics/ui/themes.html (see heading: Apply a theme to an Activity or application)
Although, no matter what you will probably obstruct the user in some way ;-). This method should only show a minimal dialog box instead of taking up the whole screen though
Can this be achieved with using an activity
No.
would the activity block the view under it anyway?
Yes.
If so, guess I would have to use a view
A view normally is hosted by an activity. A service cannot just create some random view and put it on the screen.
You can attempt to use a Toast with a custom View for a modeless "dialog", but I am uncertain if a service-constructed View will work with that.

Categories

Resources