0

I have created the following main class which shows users the welcome fragment. This welcome fragment has 2 buttons: signup and login. When the user clicks on signup button i want them to route to signup fragment and when they click on login button the user needs to go to login fragment. The problem I am facing is on clicking the button, nothing is happening. My main activity looks like this:

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.TypedArray;

import android.os.Bundle;
import android.support.v4.app.*;
import com.parse.Parse;
import com.parse.ParseAnalytics;
import android.util.Log;
import android.view.*;
import android.widget.*;

import java.util.ArrayList;

import android.support.v4.widget.DrawerLayout;
import com.parse.ParseObject;
import com.project.iandwe.Adaptor.NavigationDrawerListAdapter;
import com.project.iandwe.Fragments.UserLogIn;
import com.project.iandwe.Fragments.UserSignUp;
import com.project.iandwe.Fragments.WelcomePage;
import com.project.iandwe.Menu.*;
import com.project.iandwe.Navigation.NavigationDrawer;

public class MainActivity extends FragmentActivity implements Communicator {
@Override
    protected void onCreate(Bundle savedInstanceState) {
WelcomePage welcomePage = new WelcomePage();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragmentWelcome,welcomePage);
        fragmentTransaction.show(welcomePage);
        fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentLogin));
        fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentISignUp));
        fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentRegister));
        fragmentTransaction.commit();
}

@Override
    public void respond(int button) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (button == 1) {
            UserSignUp userSignUp = (UserSignUp) fragmentManager.findFragmentById(R.id.fragmentISignUp);
            fragmentTransaction.replace(R.id.fragmentISignUp, userSignUp);
            fragmentTransaction.show(userSignUp);
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentLogin));
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentWelcome));
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentRegister));
        } else if (button == 2) {
            UserLogIn userLogIn = (UserLogIn) fragmentManager.findFragmentById(R.id.fragmentLogin);
            fragmentTransaction.replace(R.id.fragmentLogin, userLogIn);
            fragmentTransaction.show(userLogIn);
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentISignUp));
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentWelcome));
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentRegister));
        }
        fragmentTransaction.commit();

    }

My fragment looks like this:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.project.iandwe.Communicator;
import com.project.iandwe.R;

/**
 * Created by NathanDrake on 5/28/2014.
 */
public class WelcomePage extends Fragment implements View.OnClickListener {
    Button login,signUp;
    int buttonFlag =0;
    Communicator communicator;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
           View layoutInflater;
           layoutInflater=inflater.inflate(R.layout.welcome_page,container, false );
      /*  login = (Button) getActivity().findViewById(R.id.buttonLogin);
        signUp = (Button) getActivity().findViewById(R.id.buttonSignUp);
        communicator = (Communicator) getActivity();

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonFlag =1;
                communicator.respond(buttonFlag);
            }
        });

        signUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonFlag =2;
                communicator.respond(buttonFlag);
            }
        });
        communicator.respond(buttonFlag); */
           return layoutInflater;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        login = (Button) getActivity().findViewById(R.id.buttonLogin);
        signUp = (Button) getActivity().findViewById(R.id.buttonSignUp);
        communicator = (Communicator) getActivity();
        login.setOnClickListener(this);
        signUp.setOnClickListener(this);
        /*

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             buttonFlag =1;
                communicator.respond(buttonFlag);
            }
        });

        signUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonFlag =2;
                communicator.respond(buttonFlag);
            }
        });

        communicator.respond(buttonFlag); */
     }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.buttonLogin:
                buttonFlag =1;
                break;
            case R.id.buttonSignUp:
                buttonFlag =2;
            break;

        }
        communicator.respond(buttonFlag);
    }
}

Communicator is a plain interface used for inter fragment communication like this :

public interface Communicator {

    public void respond(int button);
}

I tried the method given here but still no response from the fragment : How to handle button clicks using the xml onClick within Fragments

Please have a look and let me know what i am missing here.

Update:

I changed my code to include fragment reference like this:

   welcomePage = new WelcomePage();
            FragmentManager fragmentManager = getSupportFragmentManager();
            userLogIn = (UserLogIn) fragmentManager.findFragmentById(R.id.fragmentLogin);
            userSignUp = (UserSignUp) fragmentManager.findFragmentById(R.id.fragmentISignUp);
            registerUsingEmail = (RegisterUsingEmail) fragmentManager.findFragmentById(R.id.fragmentRegister);

fragmentTransaction.hide(userLogIn);
        //fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentISignUp));
        fragmentTransaction.hide(userSignUp);
        //fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentRegister));
        fragmentTransaction.hide(registerUsingEmail);
        fragmentTransaction.commit();

and updated my respond function for the communicator :

@Override
    public void respond(int button) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.hide(welcomePage);
        fragmentTransaction.remove(welcomePage);
        fragmentTransaction.commit();
        fragmentManager.executePendingTransactions();
        fragmentTransaction = fragmentManager.beginTransaction();
        if (button == 1) {
           // userSignUp = (UserSignUp) fragmentManager.findFragmentById(R.id.fragmentISignUp);

            fragmentTransaction.remove(userSignUp);
            fragmentTransaction.commit();
            fragmentManager.executePendingTransactions();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.fragmentISignUp, userSignUp);
            Toast.makeText(getApplicationContext(),"You are inside user signup Button",Toast.LENGTH_LONG).show();
            fragmentTransaction.show(userSignUp);
            //fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentLogin));
            fragmentTransaction.hide(userLogIn);
            //fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentWelcome));

            //fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentRegister));
            fragmentTransaction.hide(registerUsingEmail);
        } else if (button == 2) {
         //   userLogIn = (UserLogIn) fragmentManager.findFragmentById(R.id.fragmentLogin);

            fragmentTransaction.remove(userLogIn);
            fragmentTransaction.commit();
            fragmentManager.executePendingTransactions();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.fragmentLogin, userLogIn);
            Toast.makeText(getApplicationContext(),"You are inside user login Button",Toast.LENGTH_LONG).show();
            fragmentTransaction.show(userLogIn);
            //fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentISignUp));
            fragmentTransaction.hide(userSignUp);
            //fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentWelcome));

          //  fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentRegister));
            fragmentTransaction.hide(registerUsingEmail);
        }
        fragmentTransaction.commit();

    }

The problem i am facing now is I dont see the existing welcome fragment being replaced by the usersign or userlogin fragment.. I included the toast messages to see if the code was being run and i see these messages when the response method is called by a button but i dont see the user interface getting updated. I checked move fragments and tried everything in there but nothing is working

Community
  • 1
  • 1
nathandrake
  • 427
  • 1
  • 4
  • 19

2 Answers2

0

The problem is that you are using different Communicator interfaces that there is no respond you should only use one instance of the Communicator so you need to pass it to your fragments from your main activity..

and in the mainactivity interface will only be called.. Also dont implement Communicator to the fragments only the mainactivity will be implemented by Communicator.. and pass it to your fragments constructor

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • yeah i have done the same.. communicator is only implemented in the main activity and fragments just call the activity method – nathandrake May 29 '14 at 05:03
0

By the looks of it, you never actually add the Fragments to your Activity, you merely call the hide method.

Case and point in your MainActivity:

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragmentWelcome,welcomePage);
fragmentTransaction.show(welcomePage);

// These are never added, you just hide them
fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentLogin));
fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentISignUp));
fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentRegister));
fragmentTransaction.commit();

If we check the documentation for the hide method in FragmentTransaction, we can see that simply hiding something that isn't attached won't do anything. You should create instances of those Fragments and add them in a similar way that you did your WelcomePage, then hide them.

I also see that you're using some of the FragmentTransaction methods incorrectly. As a quick summary hide and show are used to simply change if a Fragment is still visible (in other words, it isn't detached from the Activity). The replace method, however, detaches whichever Fragment it finds at the given id and attaches the inputted `Fragment in its place.

Andrew Schuster
  • 3,229
  • 2
  • 21
  • 32