Archer2486
Archer2486

Reputation: 409

How to trigger an okclick event in a Fragment from a button located in a FragmentActivity

I'm trying in add Google+ sign in to my app. The example I'm following shows how to set everything up in an Activity, but I'm trying to have everything regarding the sign-in logic in a fragment while having the Sign In button in my main FragmentActivity. I'm new to Android so I'm not sure how do to this but I wanna know how can I trigger the onClick() event that I have in the fragment (that handles all the sign in stuff) from the Sign In button I have in my FragmentActivity?

I've tried to add this to the onCreate on my Activity:

googleAuthButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            googlePlusFragment.onClick(v);
        }
    });

This, of course, doesn't work. I'll appreciate some help with this.

Upvotes: 0

Views: 723

Answers (1)

Rarw
Rarw

Reputation: 7663

You're not wrong for wanting to keep your Activity code simple and readable. But the Fragment approach is weird. Background fragments have their place but this is not one of them. I think all you need to do is make a new class for each log in function. You can then call that class when the right button is pressed and outsource the actual loggin in that way. The basic set up would be something like this

googleAuthButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        GoogleLogIn logIn = new GoogleLogIn(new LoggedInListener(){

            @Override
            public void loggedIn(String whatever){
                      //if you need info from the log in use it here
            }

        });
        logIn.logMeIn(value);
    }
});

Then in your GoogleLogInClass

    public class GoogleLogIn{

        private LoggedInListener listener;       

        public GoogleLogIn(LoggedInListener listener){
            this.listener = listener; 
        }

        public void logMeIn(String info){
           //whatever you need to do to log in
           if(someConditionTrue){
               listener.loggedIn(someValueReturned);
           } else {
               listener.loggedIn(someValueThatIsFalse);
           }
        }
    }

My general idea is just to move the logic into a class which you call onClick of the button. You can then call some method to do whatever you want. You get feedback from the log in class using a callback which will return whaver you need to the host activity so you can use it later.

I don't know what's required to log into google+ or facebook so for now this is the best I can do. If all you need to do is pass some data and return a response, you can simplify things further by just making you log in class extend AsyncTask and implement the log in that way. Just pass your values in as params when you call execute().

Edit - call method from XML If the above code looks too crazy because of the nested callabcks you can make this even simpler. In the XML where you declare the button, you can add an onClick option. This allows you to specify a public method in the activity that is called when the button is pressed. No call back. No nonsense. If you followed this approach you would just add the following

public void nameThisMethodWhateverYouWant(View v){
    /call your log in class here
}

This is even easier to read. I feel like the XML onclick function was only added after a certain API level but with the support package I have personally run this as low as 2.3.6 without any issue.

Upvotes: 2

Related Questions