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