skip to Main Content

I setup a facebook login product inside my fragment where i put a Graph request to
get some data from graph api after receiving the data i want to goto a different fragment to handle the result but on clicking on login button i remain in same activity only login button get changed to log out button it means click listener is
working .if i try to go in an activity using the intent it working fine new activity get displayed but i need to go to my fragment how can i do that here is my code ?

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

    loginButton?.setOnClickListener({

        callbackManager = CallbackManager.Factory.create()
        loginButton?.setFragment(this)
        //  loginButton =  view.findViewById(R.id.log)

        loginButton?.setReadPermissions("email")
        loginButton?.registerCallback(callbackManager, object : FacebookCallback<LoginResult> {
            override fun onSuccess(loginResult: LoginResult) {

                val request: GraphRequest = GraphRequest.newMeRequest(loginResult.getAccessToken()
                        , GraphRequest.GraphJSONObjectCallback { `object`, response ->
                    // Override fun onCompleted( `object`:JSONObject,  response:GraphResponse) {
                    Log.e(TAG, `object`.toString())
                    Log.e(TAG, response.toString())

                    try {
                        userId = `object`.getString("id");
                        profilePicture = URL("https://graph.facebook.com/" + userId + "/picture?width=500&height=500");
                        if (`object`.has("first_name"))
                            firstName = `object`.getString("first_name");
                        if (`object`.has("last_name"))
                            lastName = `object`.getString("last_name");
                        if (`object`.has("email"))
                            email = `object`.getString("email");
                        if (`object`.has("birthday"))
                            birthday = `object`.getString("birthday");
                        if (`object`.has("gender")) {
                            gender = `object`.getString("gender")
                        }


                        /*     var main:Intent =  Intent(this@MainActivity, Display::class.java)
                        main.putExtra("name", firstName)
                        main.putExtra("surname", lastName)
                        main.putExtra("imageUrl",profilePicture.toString())
                        startActivity(main);
                        finish();  */
                        mainFrameFragment = MainFrameFragment()
                        parameters?.putString("name", firstName)
                        parameters?.putString("surname", lastName)
                        parameters?.putString("imageUrl",profilePicture.toString())
                        mainFrameFragment?.arguments = parameters
                        fragmentManager?.beginTransaction()
                                ?.replace(R.id.displayFragment, mainFrameFragment as MainFrameFragment,"MainFrameFragment")
                                ?.commit()

                     //   moveToNewActivity()

                    } catch (e: JSONException) {
                        e.printStackTrace();
                    } catch (e: MalformedURLException) {
                        e.printStackTrace();
                    }// App code
                })
                //        mainFrameFragment = MainFrameFragment()
                parameters = Bundle()
                parameters?.putString("fields", "id, first_name, last_name, email, birthday, gender")
                request.setParameters(parameters)
                request.executeAsync()
            }

            override fun onCancel() {
                // App code
            }

            override fun onError(exception: FacebookException) {
                // App code
            }
        })
    })
} 

2

Answers


  1. If you want to replace the entire Fragment1 with Fragment2, you need to do it inside MainActivity, by using:

    Fragment1 fragment2 = new Fragment2();
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(android.R.id.content, fragment2);
    fragmentTransaction.commit();
    

    Just put this code inside a method in MainActivity, then call that method from Fragment1.

    Login or Signup to reply.
  2. If you have a A/B layout with master detail on the screen sporting 2 fragments, then it makes sense that you want the two fragments to communicate.

    The common practice for this is to create an interface with the items that fragment A needs to call in fragment B and visa versa. Then implement the interface in the fragments. Next in fragment A constructor pass in (fragmentBInterface) and visa versa for fragment B.

    If you want the lazy answer you can use Otto Bus and fire events to subscriber. That is a simple annotation to catch it and a simple single line to post it, but this uses reflection and many engineers don’t like this style coding.

    Lastly, if only one fragment is on the screen at one time, then your fragments probably don’t have a good reason to communicate in which case allow the activity to mediate content between the fragments. If the login button is all you are trying to do, simply put some sort of a success callback in the activity and create a mainActivity interface that is given to the fragment in it’s constructor.

    One last note, if a screen is full screen and strictly launches another screen there really is no reason to use a fragment. Some people get too focused on “everything must be a fragment”. The framework provides you great options for each use case. Login is usually a full screen activity, along with splash screen. An example of good usage for fragments would be a wizard like registration or a navigation drawer for tier 1 level content of your application. When you drill into a detail I would move into an activity with up enabled.

    Hope that helps.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search