Sending Data Back to an AlertDialog's Host - java

To the point: what is the best way to send data inputted into an AlertDialog back to the host of the AlertDialog?
I have an AlertDialog which is created by a DialogFragment created in the MainActivity of my Android application. It has OK and cancel buttons, and uses a custom layout simply to display an EditText box. When the user hits OK, the contents of the EditText box should be sent to a method in the host activity of the Dialog, MainActivity.
I've accomplished this by creating a listener in the DialogFragment that gets implemented in the MainActivity. The listener gets invoked and the activity receives the DialogInterface and a String from the EditText. It works fine, but it seems awfully complicated code-wise for what seems like something that should be able to be accomplished without much extra code in the onClick method of the Dialog.
Is this really the best way to pass back a simple command, such as running a single method with an argument in the host Activity? Or even just changing the value of a variable of the host activity? If I wanted to use more than one class of DialogFragment in the same activity. I would have to implement another listener, and make sure the methods in each don't have the same names?
Thanks.

This is a pattern that fragments and host activity communicate each other. An other way plz try "Otto event bus". Google it you can find some.

Related

Passing data though multiple activities (while one gets destroyed)

I'm building my first Android app in Android Studio (with Java) and I need some help understanding how to pass data through multiple activities.
The setup of affected classes/activities is this:
MainActivity: Opens a dialog called AddDialog (by creating a new Instance of it).
AddDialog (extends AppCompatDialogFragment): A dialog that has some buttons. One of them launches a class called BarcodeScanActivity (using intent).
BarcodeScanActivity: Simple activity that scan QR codes.
I want to pass the list of QR codes scanned by BarcodeScanActivity (I store them in a String Array) to MainActivity in order to use them, although BarcodeScanActivity is launched from the dialog that gets destroyed once buttons are clicked. Because of that I'm unable to set some startActivityForResult on the dialog and chain the result (onActivityResult) back to the MainActivity.
Also since MainActivity launches the AddDialog by creating an instance, I can't set a startActivityForResult there too.
I have tried adding Intent.FLAG_ACTIVITY_FORWARD_RESULT while launching BarcodeScanActivity from AddDialog, hoping that the result from BarcodeScanActivity will be forwarded back to MainActivity where I have created an onActivityResult method since AddDialog gets destroyed, but I don't know if that is even supposed to work as AddDialog is "launched" by a new instance of it, and not by using an intent.
I have thought about using broadcasts as a last resort, although I read that they are insecure, unreliable and not supposed to be used for passing that kind of data.
Any help is highly appreciated! Thanks

How to automatically fulfill task on activity start?

I have a code that requires two edittexts, this information is used to display direction on a map. How can I submit this data automatically on activity start, without the need of a button.
Side question, is there a way to hide these edittexts? To prevent further manipulation by users.
EditTexts can be either disabled using editText.setEnabled(false); or hidden using editText.setVisibility(View.INVISIBLE);.
If you want to execute code on an activity start just write it into the onCreate(...) method.

add a new button from another fragment class

My problem is that I have two classes that extend Fragment.Now I have a button(its name is save) in 1 fragment class.I want to add a new button in another fragment class when 'save' button is clicked.I know I need to have an onClickListener for the 'save' button but I don't know how to go further from there.I also want an onClickListener for the new created button.
Any help would be much appreciated.
use interface to communicate from one fragment to another.
follow the below link. You will find out something:
onItemClickListener between two fragments
There are a number of ways to do this, depending on the relationships between fragments, whether they are nested etc.
1) Use SharedPreferences. That means you would write to the apps defaultSharedPreferences some flag which says "save has been pressed", and then in the other fragment any time you call createView you would check this flag in preferences. IF save has been pressed you would then show the button.
This approach has a few issues though depending on how long you want to show this button for, if it should be shown forever etc.
2) The interface approach mentioned is valid, but it has coupling issues, and may not be suited to the framework you have in place.
3) Broadcasts - you can use intents and send messages between fragments. This runs into some vaugeness issues (You need to be careful when documenting broadcasts and intents) and can be somewhat opaque to other readers.

AlertView that can be shown from background

I have a little problem with an alert View, the idea is that when the app starts, it asks on background for new data for the app, so after receiving the information an AlertView should be shown if there is new data. The problem comes because the AlertView is only shown on the main activity where I launched the asyncTask, so if I'm already in other activity, the alert will appear when I come back to the main activity and the Idea is that the AlertView is displayed in whatever activity the user is in.
My first thought was that the Context that I send to the AsyncTask is the problem so I've tried using getApplicationContext() so the app crashes in the moment of creating the alertView. So I'm, looking for a way to display this AlertView or something equivalent on the screen wherever he is on the app, does any one have ideas?
any idea would be greatly appreciated
you can use broadcastreceiver in every activity which will contineous monitor your background service contineous whenever event occoured using broadcastreceiver display alertbox with content received by receiver.

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