Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call fragment from non activity class in android

I have a Login Fragment and a Class named CServerResponse.

I'd like to call LoginFragment from CServerResponse class.

How can I do that?

Here is the CServerResponse class code:

public class CServerResponse {
    public static CServerResponse s_m_oServerResponse;
    public Context m_Context;

    private CServerResponse(Context m_Context) {
        this.m_Context = m_Context;
    }

    public static CServerResponse getInstance() {
        if (s_m_oServerResponse == null) {
            s_m_oServerResponse = new CServerResponse();
        }
        return s_m_oServerResponse;
    }

    public void getLoginResponse() throws JSONException {
        final Fragment activity = (Fragment) m_Context;
        if (CLoginScreen.m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Transaction Successful")) {

            CLoginScreen.m_oLoginSession.setLoginData(
                CLoginScreen.s_szResponseMobile, CLoginScreen.s_szResponsePassword);

            getActivity().getSupportFragmentManager()
                         .beginTransaction()
                         .replace(R.id.container, new CDealMainListing()).commit();

            CToastMessage.getInstance().showToast(getActivity(), "You are successfully Logged In");

        } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Agentcode Can Not Be Empty")) {

            CToastMessage.getInstance().showToast(getActivity(), "Please Enter Valid Mobile Number");

        } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Pin Can Not Be Empty")) {

            CToastMessage.getInstance().showToast(getActivity(), "Please Enter Password");

        } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Invalid PIN")) {

            CToastMessage.getInstance().showToast(getActivity(), "Please enter correct Password");

        } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Subscriber/Agent Blocked due to Wrong Attempts")) {

            CToastMessage.getInstance().showToast(getActivity(), "You are blocked as You finished your all attempt");

        } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Connection Not Available")) {

            CToastMessage.getInstance().showToast(getActivity(), "Connection Lost ! Please Try Again");

        } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Subscriber/Agent Not Found")) {

            CToastMessage.getInstance().showToast(getActivity(), "User not found ! Kindly Regiter before Login");

        } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("OTP not verify")) {

            CToastMessage.getInstance().showToast(getActivity(), "Otp not Verify ! Kindly Generate Otp on Sign Up");
        }
    }
}
like image 818
Nitin Avatar asked Dec 24 '22 07:12

Nitin


1 Answers

You'll need the access to the context of the Activity for which the fragment needs to be called.

Lets suppose you have the context of the respective Activity in a method named startLoginFragment(Context context). The code for this method will be as follows:

public void startLoginFragmemt(Context context) {
    Activity activity = (Activity) context;
    FragmentManager fragmentManager = activity.getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    LoginFragment fragment = new LoginFragment();
    fragmentTransaction.add(R.id.fragment_container, fragment);
    fragmentTransaction.commit();
}
like image 135
Varun Kumar Avatar answered Dec 27 '22 09:12

Varun Kumar