How to properly initialise fragment to null? - java

So here's the code:
private void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new DoctorFragment();
break;
case 2:
fragment = new HospitalFragment();
break;
case 3:
fragment = new SpecialClinicFragment();
break;
case 4:
fragment = new PharmacyFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
//the parameter "fragment" in this line of code gives an error message.
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
getActionBar().setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
It seems like I haven't properly initialised fragment instance.
The error message was "Wrong 2nd argument type" but I'm pretty sure I put in the right parameter.

Problem is not with the argument.You have used Fragment from
android.support.v4 library but you are making transaction on android.app.fragment.
Solution:
import android.app.fragment instead of android.support.v4.fragment
Because you cannot mix up with support.v4 library and android.app.*

Related

How to replace fragment of parent layout from child

I have looked at many of the similar question But still I'm unable to replace container layout (FrameLayout) of My Main Activity from a fragment that is visible in that container.
Here is what I'm trying to do I have a main activity with navigation drawer and On navigation drawer item click I'm changing fragment accordingly, I'm successful till this. These fragments are Login, Register, Categories, Contact Us etc. I'm changing/replacing fragments with FragmentManager, FragmentTransaction its method replace. Check the code below
public void changeFragment(int position) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Fragment fragment = new Fragment();
BlankFragment blankFragment = new BlankFragment();
switch (position) {
case 0:
fragment = new FragMain();
break;
case 1:
fragment = new FragPrivacyPolicy();
break;
case 2:
fragment = new SettingsFragment();
break;
case 101:
fragment = new FragLogin();
break;
case 102:
fragment = new FragRegister();
break;
case 103:
fragment = new FragEditAccount();
break;
case 104:
fragment = new FragOrderHistory();
break;
case 105:
fragment = new FragForgotPass();
break;
case 106:
fragment = new FragOrderDetail();
break;
case 108:
fragment = new FragAboutUs();
break;
case 109:
fragment = new FragContactUs();
break;
case 110:
fragment = new FragMain();
break;
}
transaction.replace(R.id.flFragments, fragment);
transaction.commit();
}
Which is working fine as all my fragments extends Fragment. The problem comes when I try to call this method in one of the fragments visible in this mainActivity's container. Suppose I have a textView in FragLogin saying "Don't have an account Register" so on click this textView I have to open FragRegister in the same container of mainActivity. When I do it like this in my FragLogin App Crashes pointing to the transaction.commit();
MainActivity activity = new MainActivity();
activity.changeFragment(102);
And when I try like this
FragRegister fragRegister = new FragRegister();
// Fragment fragRegister = new FragRegister();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.Id.flFragments, fragRegister).commit();
It does not recognize the R.Id.flFragments as its inside the mainActivity not in FragLogin.
So how can I replace that FragRegister in mainActivity's container from FragLogin?
Dont use getChildFragmentManager() because it belongs to the fragment manager and the container that is holding current fragment is the activity container.Instead use getSupportFragmentManager().
Also see this stament
MainActivity activity = new MainActivity();
activity.changeFragment(102);
You are creating a new object of mainactivity which is already created.You have to call the activity method from fragments using getActivity().Call the changeFragment method like this.
((MainActivity)getActivity()).changeFragment(102);

How do I adapt an Activity in android from fragment transaction?

I have this code:
public void replaceFragment(int i) {
fragmentTransaction = getSupportFragmentManager().beginTransaction();
TextView headerName = (TextView) findViewById(R.id.HeaderName);
switch (i) {
// Login Fragment
case 0:
headerName.setText("Member Login");
fragmentTransaction.replace(R.id.main_container, new
LoginActivity());
break;
// Register Fragment
case 1:
headerName.setText("Sign Up");
fragmentTransaction.replace(R.id.main_container, new RegisterFragment());
break;
}
}
My problem is on the case 0, I put the other cases as examples. My login is not a Fragment, but an Activity. So how do I manage to keep it here? With an Intent?
Instead of
fragmentTransaction.replace(R.id.main_container, new
LoginActivity());
Use
Intent inntent = new Intent(this,LoginActivity.class);
startActivity(inntent);
Just build a new intent in case 0 and start your activity instead of trying to replace a fragment with activity which is impossible unless you turn your activity to fragment
If you need both you can turn your activity to fragment and then build an empty activity to contain it when necessary

Android perform button click event on navigation drawer item selected

I am using an Android material navigation drawer for my app and am trying to create a log out button as one of the navigation drawer items
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
// Toast.makeText(this, "Menu item selected -> " + position, Toast.LENGTH_SHORT).show();
Fragment fragment = null;
switch (position) {
case 0:
fragment = new AnotherFragment();
break;
case 1:
fragment = new AnotherFragment();
break;
case 2:
fragment = new AnotherFragment();
break;
case 3:
fragment = new AnotherFragment();
break;
case 4:
fragment = new AnotherFragment();
break;
case 5:
fragment = new AnotherFragment();
break;
case 6:
fragment = new AnotherFragment;
break;
case 7:
fragment = new AnotherFragment();
break;
case 8:
fragment = new AnotherFragment();
break;
case 9:
break;
default:
break;
}
if (fragment != null)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment).commit();
}
on case 9 I want to allow the user to log out,
Intent logOut = new Intent(GroupHomeActivity.this, LoginActivity.class);
getSharedPreferences("Pref", Context.MODE_PRIVATE).edit().clear().commit();
logOut.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
How can I allow the codes above to be executed on case 9?
Well here is what I think you should do :
Fragment fragment = null;
switch (position) {
case 0:
fragment = new AnotherFragment();
break;
case 1:
fragment = new AnotherFragment();
break;
case 2:
fragment = new AnotherFragment();
break;
case 3:
fragment = new AnotherFragment();
break;
case 4:
fragment = new AnotherFragment();
break;
case 5:
fragment = new AnotherFragment();
break;
case 6:
fragment = new AnotherFragment;
break;
case 7:
fragment = new AnotherFragment();
break;
case 8:
fragment = new AnotherFragment();
break;
case 9:
fragment=null;
break;
default:
break;
}
if (fragment != null)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment).commit();
}
else {
Intent logOut = new Intent(GroupHomeActivity.this, ElgarLoginActivity.class);
getSharedPreferences("Pref", Context.MODE_PRIVATE).edit().clear().commit();
logOut.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
}
Simply because the return type is expecting a non null fragment. Well just return a null fragment, and on null logout the user.
Here is how I would handle the logic for the position 9 click
#Override
public void onNavigationDrawerItemSelected(int position) {
if(position == 9){
Intent logOut = new Intent(GroupHomeActivity.this, ElgarLoginActivity.class);
getSharedPreferences("Pref", Context.MODE_PRIVATE).edit().clear().commit();
logOut.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
}else{
switchFragment(position);
}
}
Here is how you would switch the fragments.
private void switchFragment(int position){
Fragment fragment = null;
switch (position) {
case 0:
fragment = new AnotherFragment();
break;
case 1:
fragment = new AnotherFragment();
break;
case 2:
fragment = new AnotherFragment();
break;
case 3:
fragment = new AnotherFragment();
break;
case 4:
fragment = new AnotherFragment();
break;
case 5:
fragment = new AnotherFragment();
break;
case 6:
fragment = new AnotherFragment;
break;
case 7:
fragment = new AnotherFragment();
break;
case 8:
fragment = new AnotherFragment();
break;
default:
break;
}
if (fragment != null){
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment).commit();
}
}
Just put your code after case 9 before break. Like this:
Case 9:
Intent logOut = new Intent(GroupHomeActivity.this, ElgarLoginActivity.class);
getSharedPreferences("Pref", Context.MODE_PRIVATE).edit().clear().commit(); logOut.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
StartActivity(logout);
break;

How i can use .replace(Fragment , mapsFragment)? Android

I have use .replace() for replace a old Fragment with a new, but in this moment i do no how use the same of .replace() by a Fragment to a mapsFragment.
Ideas?!
I can use replace? if i can' t use it, what I can use?
This is my code in this moment. (the .replace() don' t work)
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
mapsFragment fragM = null;
Fragment frag = null;
switch (position) {
case 0: //Home
mTitle = "Maps";
fragM = new mapsFragment();
break;
case 1: //Query utente
mTitle = "Section 2";
break;
case 2: //Query Amministratore
mTitle = "Section 3";
break;
}
if (position != 3) {
if(position==0){
fragmentManager.beginTransaction()
.replace(R.id.container, fragM)
.commit();
}else {
fragmentManager.beginTransaction()
.replace(R.id.container, frag)
.commit();
}
I have find the solution:link where i have found it
This is the code in particular:
if(position==0){
FragmentTransaction mTransaction = getSupportFragmentManager()
.beginTransaction();
SupportMapFragment mFRaFragment = new mapsFragment();
mTransaction.replace(R.id.container, mFRaFragment);
mTransaction.commit();
}else {

How can I add the activity in Android Sliding Menu which is fragment

http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/
hi all, I have succeeded apply this Android Sliding Menu in my project.
however, how can i add some activity in diffferent page?
In MainActivity, displayView function controls the fragment which i have selected.you can see it only use in "Fragment fragment = null;",so , the CESDemo class is extends Fragment.But i cannot add my activity in the CESDemo, such as onTouch and so on.If i change it to FragmentActivity, it does not allow me for "ragmentManager.beginTransaction().replace(R.id.frame_container,fragment).commit();"
So, how can i apply some activity in different fragment,even i can design the layout but no any activity i can create.
MainActivity.java
private void displayView(int position) {
// update the main content by replacing fragments
//Fragment fragment = null;
Fragment fragment =null;
switch (position) {
case 0:
fragment = new CESHome();
break;
case 1:
fragment = new CESAll();
break;
case 2:
fragment = new CESPending();
break;
case 3:
fragment = new CESInProgress();
break;
case 4:
fragment = new CESCompleted();
break;
case 5:
fragment = new CESDemo();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
CESDemo.java
public class CESDemo extends Fragment {
public CESDemo(){}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_cesdemo, container, false);
return rootView;
}

Categories