Reputation: 163
I have an activity in which a button ,when button is clicked I want to call fragment on click logout,How can I do That.Its gives throws error like...
Here is my code
public class CLoginSessionManagement {
// User name (make variable public to access from outside)
public static final String s_szKEY_MOBILE = "agentCode";
// Email address (make variable public to access from outside)
public static final String s_szKEY_PASSWORD = "pin";
// Sharedpref file name
private static final String s_szPREF_NAME = "LoginData";
// All Shared Preferences Keys
private static final String s_szIS_LOGIN = "IsLoggedIn";
public SharedPreferences m_LoginPref;
public Editor m_Editor;
public Context m_Context;
public int n_PrivateMode = 0;
// Constructor
public CLoginSessionManagement(Context m_Context) {
this.m_Context = m_Context;
m_LoginPref = m_Context.getSharedPreferences(s_szPREF_NAME, n_PrivateMode);
m_Editor = m_LoginPref.edit();
}
// Registeration Session Management....
public void setLoginData(String mobile, String pin) {
m_Editor.putBoolean(s_szIS_LOGIN, true);
m_Editor.putString(s_szKEY_MOBILE, mobile);
m_Editor.putString(s_szKEY_PASSWORD, pin);
m_Editor.commit();
}
/**
* checkLogin() session wil check user Login status
* If false it will redirect user to Login page
* Else won't do anything
*/
public boolean checkLogin() {
if (!isLogin()) {
Intent i = new Intent(m_Context, CMainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
m_Context.startActivity(i);
return true;
}
return false;
}
/**
* Get stored Login session data
*/
public HashMap<String, String> getLoginDetails() {
HashMap<String, String> user = new HashMap<>();
// user name
user.put(s_szKEY_MOBILE, m_LoginPref.getString(s_szKEY_MOBILE, null));
// user email id
user.put(s_szKEY_PASSWORD, m_LoginPref.getString(s_szKEY_PASSWORD, null));
// return user
return user;
}
public boolean isLogin() {
return m_LoginPref.getBoolean(s_szIS_LOGIN, false);
}
/**
* Clear session details
*/
public void logoutUser() {
// Clearing all data from Shared Preferences
m_Editor.clear();
m_Editor.commit();
Fragment m_oLoginScreen = new CLoginScreen();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, m_oLoginScreen, null);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
Gives me error at getSupportfragmentManager in LogOut section. here is my code from where a click event
else if (id == R.id.LogOut) {
s_oCloginSession.checkLogin();
s_oCloginSession.logoutUser();
Fragment m_oLoginScreen = new CLoginScreen();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, m_oLoginScreen, null);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
Upvotes: 0
Views: 1065
Reputation: 427
Use this:
public static void t(Activity activity){
LayoutInflater inflater = activity.getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, null);
TextView text = (TextView) layout.findViewById(R.id.toastId);
}
Upvotes: 0
Reputation: 495
I think right way is user interface in your java class and implement that in your activity Here is example using interface https://www.dropbox.com/s/qirpdnd78xlbnzh/SamplePro_with_interface%27.zip?dl=0
Upvotes: 0
Reputation: 285
final Activity activity = (Activity) context;
FragmentManager mFragmentManager = (activity).getSupportFragmentManager();
// if getSupportFragmentManager() doesnt work try with getFragmentManager and change the import of fragment
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView, new CLoginScreen());
fragmentTransaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
fragmentTransaction.commit();
Upvotes: 0
Reputation: 531
First, your class does not have the method that you are trying to call getSupportFragmentManager.
if your context in this class is a activity that extends a class with this method you can use
((AppCompactActivity)m_Context).getSupportFragmentManager();
if not, you have two choices, make your activity class extends AppCompactActivity or use getFragmentManager instead.
Upvotes: 4
Reputation: 3234
You cannot access getSupportFragmentManager() as your Class doesn't extend Activity.If you want to access the method the method in a non Activity class. Taking that context passed is a Activity you can call the method like this:
final Activity activity = (Activity) context;
activity.getSupportFragmentManager()
ThankYou I hope this was helpful.
Upvotes: 0
Reputation: 224
You can directly call getSupportFragmnetManager only from inside activity extended classes like AppcombatActvity or FragmentActivity. To get support fragment manager from other classes make use of the context of the application. Replace
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
with
FragmentTransaction fragmentTransaction = ((<activity_name>)m_Context).getSupportFragmentManager().beginTransaction();
Upvotes: 0
Reputation: 488
Either try getFragmentManager() instead or extend AppCompatActivity class for your Activity.
Upvotes: 0