Application Fragment - android

This code shows an error :
private void selectItem(int position) {
Fragment fragment;
switch (position)
{
case 1:
fragment = new PizzaFragment();
break;
case 2:
fragment = new PastaFragment();
break;
case 3:
fragment = new StoresFragment();
break;
default:
fragment = new TopFragment();
}
android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame,fragment);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
when I initialize fragment with new PizzaFragment or PastaFragment, android studio shows an error "Incompatible types"

you have to create object of that fragment .It will work.
switch (position)
{
case 1:
PizzaFragment fragment = new PizzaFragment();
// apply FragmentTransaction
break;
case 2:
PastaFragment fragment = new PastaFragment();
// apply FragmentTransaction
break;
case 3:
StoresFragment fragment = new StoresFragment();
// apply FragmentTransaction
break;
default:
TopFragment fragment = new TopFragment();
// apply FragmentTransaction
}

I did the same once and the mistake was that the PizzaFragment and PastatFragent classes were not extending the Fragment, just look at that for once
Like
PizzaFragment extends Fragment{
and
PastaFragment extends Fragment{
And Maybe
you are importing different packages,so check your import statements

Related

Replace Fragment showing both fragments

There is problem with my fragment, they are showed together, one on the second fragment. How to disapear, and only show one of them?
Definiton:
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fr = new avc);
FragmentTransaction ft = ((TestingActivity)context).getFragmentManager().beginTransaction();
ft.replace(R.id.test, fr);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
});
And definiton of container below:
<FrameLayout
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="match_parent" />
You can use some thing like this. Make a function for showing a fragment and call each time this function with different parameter.
eg. If You want to show "HomeFragment" then call displayView(0) and if you want to show "FindPeopleFragment" then call displayView(1)
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new FindPeopleFragment();
break;
case 2:
fragment = new PhotosFragment();
break;
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
fragment = new WhatsHotFragment();
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");
}
}

Navigation Drawer Fragments

So I have my navigation drawer with 5 different options. They all open a new fragment that I have created. The first one is Home, and I am trying to find a way to bring it back to the first screen that shows up under the navigation drawer. It has the id of "container", in the main_activity.xml. I do not want to use and intent to call the entire class again to load up. Also I do not want to be able to use the back button from another intent. I am confused on how to make this happen.
#Override
public void onNavigationDrawerItemSelected(int position) {
FragmentHowItWorks fragmentHow;
FragmentSettings fragmentSettings;
FragmentTransaction transaction = getFragmentManager().beginTransaction();
switch(position){
case 0:
// should I call the layout?
// this is the "Home" option
break;
case 1:
fragmentHow = new FragmentHowItWorks();
transaction.replace(R.id.container, fragmentHow);
transaction.addToBackStack(null);
transaction.commit();
break;
case 2:
fragmentSettings = new FragmentSettings();
transaction.replace(R.id.container, fragmentSettings);
transaction.addToBackStack(null);
transaction.commit();
break
case 3:
fragment = new FragmentHowItWorks();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(null);
transaction.commit();
break;
case 4:
fragment = new FragmentHowItWorks();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(null);
transaction.commit();
break;
}
}
Use methods add,hide and show,
Step1 Prepare all your fragments
Fragment fragment1 = new FragmentOne();
Fragment fragment2 = new FragmentTwo();
Fragment fragment3 = new FragmentThree();
Fragment mCurrentFragment = null;
Step2 Show/hide your fragments
#Override
public void onNavigationDrawerItemSelected(int position) {
Fragment fragment;
switch (position) {
case 1:
fragment = fragment1;
break;
case 2:
fragment = fragment2;
break;
case 3:
fragment = fragment3;
break;
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if(mCurrentFragment == null) {
ft.add(R.id.container, fragment).commit();
mCurrentFragment = fragment;
} else if(fragment.isAdded()) {
ft.hide(mCurrentFragment).show(fragment).commit();
} else {
ft.hide(mCurrentFragment).add(R.id.container, fragment).commit();
}
mCurrentFragment = fragment;
}
You can do this ,
You can get the name of current fragment which is in the container . This will return name including the package + fragment name
String name = getFragmentManager().findFragmentById(container id ).getClass().getName();
When you click on the home index of drawer, check weather the current name id equal to the home.
If it is home, you don't need to perform any action.
I know that this was asked and answered long time ago but, I would like to show my approach on this problem, maybe it will help anyone else:
I added a Fragment Name over each Fragment that I use like:
public class MainFragment extends BaseFragment {
public static final String FRAGMENT_TAG = "main";
// ... all your fragment
}
And on the Drawer Layout:
public void methodThatSetsTheUi() {
mDrawerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
getFragmentManager().findFragmentByTag("main");
String tag = getFragmentTag(position);
replaceFragment(getOrCreateFragment(tag), tag);
mDrawerLayout.closeDrawer(mDrawerView);
}
});
}
#NonNull
private String getFragmentTag(int position) {
String tag;
switch (position) {
case MAIN_FRAGMENT_DRAWER_POSITION:
tag = MainFragment.FRAGMENT_TAG;
break;
case FAVORITE_FRAGMENT_DRAWER_POSITION:
tag = FavoriteFragment.FRAGMENT_TAG;
break;
default:
throw new AssertionError("That selection is wrong");
}
return tag;
}
private BaseFragment getOrCreateFragment(String fragmentName) {
BaseFragment fragment = (BaseFragment) getFragmentManager().findFragmentByTag(fragmentName);
if(fragment == null) {
fragment = FragmentFactory.createFragment(fragmentName);
}
return fragment;
}
and well, the FragmentFactory is just a simple Factory:
public final class FragmentFactory {
public static BaseFragment createFragment(String fragmentName) {
switch(fragmentName) {
case MainFragment.FRAGMENT_TAG:
return MainFragment.newInstance();
case FavoriteFragment.FRAGMENT_TAG:
return FavoriteFragment.newInstance();
// ... all fragments here.
default:
return null;
}
}
Hope to help someone.

Start a Fragment From An Activity?

I Recently used this to start A Fragment From Activity in Navigation Bar Activity (In The OnListItemClick() ) :
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
getActionBar().hide();
break;
case 5:
fragment = new WhatsHotFragment();
break;
case 6:
fragment = new MyFragment();
break;
case 7:
fragment = new Views();
break;
case 8:
fragment = new editText();
break;
The problem is that The fragment is not opening on top of Main Activity on Click of Button after
Instantiating the fragment with its default constructor .
But Now I'm trying to the same but its not working :
MainActivity.java
XML of MainActivity has A button As :
<com.gc.materialdesign.views.ButtonFlat
android:id="#+id/buttonflat"
android:onClick="startFrag"
android:layout_width="230dp"
android:layout_height="80dp"
android:layout_centerInParent="true"
android:textColor="#ffffff"
android:text="Button" />
and in Java Of MainActivity:(On Click Of Button )
public void startFrag(View v)
{
fragment = new Frag_FAB();
}
java of fragment:
public class Frag_FAB extends Fragment {
public Frag_FAB() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_find_people,container,false);
return view;
}
}
Xml oF Fragment :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#010008"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtLabel"
android:layout_width="wrap_content"
android:text="#string/stuff"
android:textColor="#color/highlighted_text_material_dark"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp" />
</RelativeLayout>
add the fragment transaction after the switch statement:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.container, fragment);
your code must be something like:
switch(position){
...
...
...
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
getActionBar().hide();
break;
case 5:
fragment = new WhatsHotFragment();
break;
case 6:
fragment = new MyFragment();
break;
case 7:
fragment = new Views();
break;
case 8:
fragment = new editText();
break;
...
}
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.container, fragment); //* Here you add the fragment! :)
You need to add it to the layout via a FragmentTransaction:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.container, fragment);
You have to add your fragment to the FragmentManager. Here is an example :
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();

findFragmentByTag Strange behavior on Switching Tabs

I have 5 fragments which i switch between them in ActionBar :
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
int tag = (Integer) tab.getTag();
fragmentTransaction = fragmentManager.beginTransaction();
switch(tag)
{
case 0:
FragmentBase fragBase = new FragmentBase ();
fragmentTransaction.replace(R.id.frame_order,fragBase,"FragmentBase");
fragmentTransaction.addToBackStack(null);
break;
case 1:
Fragment1 frag1 = new Fragment1();
fragmentTransaction.replace(R.id.frame_order, frag1, "Fragment1");
fragmentTransaction.addToBackStack(null);
break;
case 2:
Fragment2 frag2 = new Fragment2();
fragmentTransaction.replace(R.id.frame_order, frag2, "Fragment2");
fragmentTransaction.addToBackStack(null);
break;
case 3:
Fragment3 frag3 = new Fragment3();
fragmentTransaction.replace(R.id.frame_order, frag3, "Fragment3");
fragmentTransaction.addToBackStack(null);
break;
case 4:
Fragment4 frag4 = new Fragment4();
fragmentTransaction.replace(R.id.frame_order, frag4, "Fragment4");
fragmentTransaction.addToBackStack(null);
break;
}
fragmentTransaction.commit();
}
then in a function i want to get a value from the Fragment4 :
Fragment4 frag4 = (Fragment4)getSupportFragmentManager().findFragmentByTag("Fragment4");
String stfrag4 = frag4.getFrag4Value();
the problem is when i'm on tab 4 which is my Fragment4 contents page , the return of findFragmentByTag is always null but when i switch to another tab then find that Fragment it returns true Fragment and not null.
why is this happening? and how can i solve the issue? Thanks in Advance.
make test...
change
int tag = (Integer) tab.getTag();
by
int tag = getActionBar().getSelectedNavigationIndex();

How do I replace the fragmentactivity in the code below?

/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
FragmentActivity fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new HomeFragment();//LessonPlanFragment();
break;
case 2:
fragment = new HomeFragment();//GradeBookFragment();
break;
case 3:
fragment = new HomeFragment();//StudentFragment();
break;
case 4:
fragment = new HomeFragment();//ReportsFragment();
break;
case 5:
fragment = new HomeFragment();//AppSettingsFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.frame_container, fragment);
ft.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");
}
}
I have tried using the support library but nothing helped. The error message reads "het method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, FragmentActivity).
This won't work. You're trying to replace Fragment with FragmentActivity and this is impossible. You can only do that with Fragments. For clarification FragmetnActivity acts as host Activity for Fragments in support library.

Categories

Resources