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();
Related
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
I am using a Tablayout to switch between Fragments on an Activity; however, the Fragment that I originally assign to the Activity seems to not be replaced when tabs are clicked - a new fragment just seems to be added. This new fragment has the implementation I want the TabLayout to have. That is, when someone clicks a tab, the Fragment is replaced with the fragment associated with that Tab. I genuinely can not see what is causing this as I feel like I am replacing the entire fragment by referencing its ID in the XML file, and I don't think I assign the fragments anywhere else. The key snippets of code are below. I have attached pictures to help you see what I am talking about. Thank You!
Main code pertaining to TabLayout:
#Override
public void onCreate(Bundle savedInstanceState) throws IllegalArgumentException {
super.onCreate(savedInstanceState);
popup = new LoadingDialog(this);
setContentView(R.layout.main_activity_layout);
SharedPreferences sharedPref = getSharedPreferences("mysettings", Context.MODE_PRIVATE);
Boolean islogged = sharedPref.getBoolean("loggedin", false);
tab_layout = (TabLayout) findViewById(R.id.sliding_tabs);
tab_layout.setTabMode(TabLayout.MODE_SCROLLABLE);
tab_layout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
int position = tab.getPosition();
switch (position){
case 0:
FragmentTransaction FTg = FM.beginTransaction();
Fragment gen = new FragmentGeneral();
FTg.replace(R.id.ma_layout, gen);
FTg.commit();
break;
case 1:
FragmentTransaction FTs = FM.beginTransaction();
Fragment sum = new FragmentOne();
FTs.replace(R.id.ma_layout, sum);
FTs.commit();
break;
case 2:
FragmentTransaction FTf = FM.beginTransaction();
Fragment forecast = new FragmentTwo();
FTf.replace(R.id.ma_layout, forecast);
FTf.commit();
break;
case 3:
FragmentTransaction FTr = FM.beginTransaction();
Fragment rates = new FragmentThree();
FTr.replace(R.id.ma_layout, rates);
FTr.commit();
break;
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_activity"
>
<fragment
android:name="com.example.schadtj.portalapp.FragmentGeneral"
android:layout_gravity="center_horizontal"
android:id="#+id/ma_layout"
android:layout_width="match_parent"
android:layout_height="558dp"
>
</fragment>
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabItem
android:text="User Info"
android:icon="#drawable/information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<android.support.design.widget.TabItem
android:text="Summary"
android:icon="#drawable/calculator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<android.support.design.widget.TabItem
android:text="Forecast"
android:icon="#drawable/calendar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<android.support.design.widget.TabItem
android:text="Rates"
android:icon="#drawable/money_bag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</android.support.design.widget.TabLayout>
Change Like this in java:
#Override
public void onCreate(Bundle savedInstanceState) throws IllegalArgumentException {
super.onCreate(savedInstanceState);
popup = new LoadingDialog(this);
setContentView(R.layout.main_activity_layout);
SharedPreferences sharedPref = getSharedPreferences("mysettings", Context.MODE_PRIVATE);
Boolean islogged = sharedPref.getBoolean("loggedin", false);
tab_layout = (TabLayout) findViewById(R.id.sliding_tabs);
tab_layout.setTabMode(TabLayout.MODE_SCROLLABLE);
tab_layout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
int position = tab.getPosition();
switch (position){
case 0:
FragmentTransaction FTg = FM.beginTransaction();
Fragment gen = new FragmentGeneral();
FTg.replace(R.id.ma_layout, gen);
FTg.commit();
break;
case 1:
FragmentTransaction FTs = FM.beginTransaction();
Fragment sum = new FragmentOne();
FTs.replace(R.id.ma_layout, sum);
FTs.commit();
break;
case 2:
FragmentTransaction FTf = FM.beginTransaction();
Fragment forecast = new FragmentTwo();
FTf.replace(R.id.ma_layout, forecast);
FTf.commit();
break;
case 3:
FragmentTransaction FTr = FM.beginTransaction();
Fragment rates = new FragmentThree();
FTr.replace(R.id.ma_layout, rates);
FTr.commit();
break;
}
}
}
FragmentTransaction FTg1 = FM.beginTransaction();
Fragment gen = new FragmentGeneral();
FTg1.replace(R.id.ma_layout, gen);
FTg1.commit();
}
Change fragmentto FrameLayout
<FrameLayout
android:layout_gravity="center_horizontal"
android:id="#+id/ma_layout"
android:layout_width="match_parent"
android:layout_height="558dp"/>
I have a navigation drawer in my activity and add the fragments on drawer item clicked
For adding the fragment
Fragment fragment = null;
switch (position) {
case 0:
fragment = new Fragment()1;
break;
case 1:
fragment = new Fragment()2;
break;
case 2:
fragment = new Fragment()3;
break;
case 3:
fragment = new Fragment()4;
break;
case 4:
fragment = new Fragment()5;
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.frame_container, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
closeDrawer();
} else {
Logger.e("MainActivity", "Error in creating fragment");
}
Whenever I switch back from one fragment to another fragment the view is inflated again as in previous fragment was destroyed and it is being created again.
How can I stop it from being recreated?
Fragment fragment1 = null;
Fragment fragment2 = null;
Fragment fragment3 = null;
Fragment fragment4 = null;
Fragment fragment5 = null;
switch (position) {
case 0:
if (fragment1 == null) {
fragment1 = new Fragment()1;
}
break;
case 1:
if (fragment2 == null) {
fragment2 = new Fragment()2;
}
break;
case 2:
if (fragment3 == null) {
fragment3 = new Fragment()3;
}
break;
case 3:
if (fragment4 == null) {
fragment4 = new Fragment()4;
}
break;
case 4:
if (fragment5 == null) {
fragment5 = new Fragment()5;
}
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.frame_container, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
closeDrawer();
} else {
Logger.e("MainActivity", "Error in creating fragment");
}
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");
}
}
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();