Hi everyone, having a large amount of trouble with fragments in my first attempt at creating an Android app and am in dire need of some help. I'll post the code below immediately with an explanation. I have experience with Java but am new to Android applications. Have been trying to build a mass e-mailing app to allow a user to email every politician in the country with one email. But, I'm having a ton of trouble with my email fragment. I had originally planned it to be an activity but as I need to use it twice (once for a guest and once for a registered user) I went with a fragment as I know they can be re-used. The problem I'm having is to do with Bundles/Intents. I have gotten intents to work correctly but through the videos and tutorials I've watched it seems like you can't use intents to pass data back to the activity from a fragment and have to use a bundle. if this is wrong please correct me. I'm attempting to store the user inputted e-mail and subject line. This is what I have so far. I'll post the fragment and the activity, if you need anything else please let me know
And the Activity (it's very bare bones, trying to get the fragment to work first)
This is currently giving me an illegal state exception due to, specifically, this ( java.lang.IllegalStateException: Could not find method storeEmail(View) in a parent or ancestor Context for android:onClick attribute defined on view class com.google.android.material.button.MaterialButton with id 'confirmFrag'). I searched online and the closest suggestion I found was to make the declared variables public but that didn't help at all. The same exception occured.
There is an extremely high possibility this is wrong because I've been doing the wrong thing. TBH, the different classes I've seen on fragments have made me extremely muddled due to using multiple different ways of doing the same thing. I don't know if I'm sending correctly. I had not written how to recieve it yet, I wanted to check if the bundle was carrying the messages in debug mode. But when I hit the confirmFrag button I get the crash.
If I've done the complete wrong thing, then please could someone tell me what I'm doing wrong. I'm very lost with this mistake, thought I had finally got it.
A fragment and an Activity are a lot alike. An activity acts almost like it's an independent program, unrelated to any other Activity. So you pass data between Activities using Bundles.
But ther are bundles and there are Bundles.
Unfortunately, I'm quite muddled these days since Android has gone through - and occasionally discarded - a lot of mutations and I have no clean or authoritative documentation to pull from, but according to my latest project, the Bundle supplied to the onCreate() method doesn't appear to contain the necessary parameters.
Instead for my working code, I see this statement used to obtain passed data for Activities:
And this statement used to obtain passed data for Fragments:
Fair warning: the project I used for reference is a VERY old project that I've been trying to bring up to date and I've had a lot of trouble with stuff that was deprecated by other stuff that was also deprecated etc. and among others that I could fault I include the "android extras". So I don't claim that my code is clean or consistent, but (so far!) it does work.
Sources may include data from the Fakebook Research Foundation with support from Gargle University
Tim, thanks very much for the response. As it's a beginner Android project, I just decided to abandon the fragment I was trying to fix and start it again with an activity. I realize it's non optimal but I got the results I was looking for so I'm fairly happy with the results. Understnading bundles a little better now but still far more comfortable with intents
Tick check! Okay, I guess that was just an itch. Oh wait! Just a tiny ad: