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
Related
I am developing an Android Kotlin application which uses two different activities. In each one I have a button that allow me to move to the other one, to say, in Activity 1 I have a button that calls the follwing:
val intentActivity2 = Intent(this, Activity2::class.java)
startActivity(intentActivity2)
This launches correctly Activity2, which similarly inside it I have another button that calls the first activity to return to it:
val intentActivity1 = Intent(this, Activity1::class.java)
startActivity(intentActivity1)
The problem I have is that I want to have both activities running simultaneously (not needed to be shown at screen at the same time), and the issue right now is that every time I call the "startActivity(intent)" a new activity is created, loosing what I had in the previous one, so when I return to Activity1 everything is reset and the same when I go once again to Activity2. Both activities work fine and do their work, the problem is that I can't freely conmute between them.
Is there a way to have both activities at the same time or to not start a new activity everytime I want to change to the other one?
Thank you,
As someone just said you need ViewModel to retrieve your data after deleting/creating new activities. Maybe you can use fragments to do your things. Fragments are attached to activities and it is easier to use them instead of ViewModel.
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.
There are a bunch of other questions about this topic, but I have not been able to figure this issue out.
In the Android documentation (http://developer.android.com/training/basics/activity-lifecycle/recreating.html) it says:
By default, the system uses the Bundle instance state to save information about each View object in your activity layout (such as the text value entered into an EditText object). So, if your activity instance is destroyed and recreated, the state of the layout is restored to its previous state with no code required by you.
So I tested this in the emulator by simply creating a view that contains a EditText-view. I then enter information into it and press the home button. When I reopen the app, the information is gone. Shouldnt this be persisted automatically or am I missing something?
Well you are partially wrong and partially right. You are wrong, because the quotation in grey is taken out of the context. I'll explain briefly, by making the correct quotations from the link you provided:
When your activity is destroyed because the user presses Back or the
activity finishes itself, the system's concept of that Activity
instance is gone forever because the behavior indicates the activity
is no longer needed. However, if the system destroys the activity due
to system constraints (rather than normal app behavior), then although
the actual Activity instance is gone, the system remembers that it
existed such that if the user navigates back to it, the system creates
a new instance of the activity using a set of saved data that
describes the state of the activity when it was destroyed.
Now, after that paragraph we have a clarification:
Caution: Your activity will be destroyed and recreated each time the
user rotates the screen. When the screen changes orientation, the
system destroys and recreates the foreground activity because the
screen configuration has changed and your activity might need to load
alternative resources (such as the layout).
Another one, several linew below is:
To save additional data about the activity state, you must override
the onSaveInstanceState() callback method. The system calls this
method when the user is leaving your activity and passes it the Bundle
object that will be saved in the event that your activity is destroyed
unexpectedly. If the system must recreate the activity instance later,
it passes the same Bundle object to both the onRestoreInstanceState()
and onCreate() methods.
This Bundle (Bundle savedInstanceState) is used, when the application accidentally crashes OR if the rotation of the screen is enabled (to name few), which is also destoying (then recreating) your foreground.
You can also take a look at the following section "Save Your Activity State", but I would recommend you this link here.
I have developed one application in which i need to register a user in SIP account as well as Chat account. To register the user i need pass through three classes: setting.java (sharedpreferences), sipchat.java (registeration to SIP account), xmppclient.java (to register in chat).
Now i am calling settings.class to register user in application in settings.java I am calling siochat.java and in sipchat.java i am calling xmppclient.java.
This is the way the user registers in application:
code:
Intent i = new Intent(Welcome_screen.this, Settings.class);
startActivity(i);
finish();
When using this application, it takes lots of time to register and it blinks as it passes through different activities.
So how do i call all three of these classes in a single activity? (Because it very weird that at the main screen the application blinks thrice.)
Thanks
I give you some solutions:
Use startActivityForResult(), pass through 3 activities and handle the result in your main activty.Example: http://rahulonblog.blogspot.com/2010/05/android-startactivityforresult-example.html
Change the content view of one activity. In this case, we have 3 views. Example: How to use view flipper with three layouts?
I don't understand why you need to start all these activities. Isn't it possible to just call static functions in the target activities?
Otherwise could you further explain why it's crucial that you start all these different activities and not just handle the functions in one activity?
i am making an andriod application in which i need to go from one page to another on a button click. i have tried several things but nothing worked out.
Okay, so given these are different .java files, and each has it own Activity (so, different Activities) what you want to do is call an intent as such:
Intent myActivity = new Intent(class1.this,class2.class);
main.this.startActivity(myActivity);
If its in the same Activity, (which I dont recomend) just call setContentView() again