I am trying to attach a Fragment to my MainActivity programmatically by using the following code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MainFragment fragment = new MainFragment();
fragmentTransaction.add(R.id.activity_main,fragment);
fragmentTransaction.commit();
}
it gives me an error: cannot resolve add method
The application can be found here
Replace your code:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MainFragment fragment = new MainFragment();
fragmentTransaction.add(R.id.activity_main,fragment);
fragmentTransaction.commit();
By this:
Fragment fragment = new MainFragment();
getSupportFragmentManager.begintransaction.replace(R.id.container,fragment).commit;
first of all your take framelayout in activity_main.after that
fragmentTransaction.add(R.id.framelayout_id,fragment);
than its working fine
Make a function like that
public void openFragment(Fragment fragment) {
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.fragmentContainer, fragment).commit();
}
and call this in your onCreate Method
openFragment(new yourFragment());
In your beginTransaction().replace method you have to pass the view from which you want to replace your fragment with
Related
I'm using a navigation bottom with 5 fragments.
Each fragment makes HTTP call and downloads content, but while replacing the fragment the view is destroyed and downloads it again.
In my search I found out that I should use setOffscreenPageLimit this method
but I don't use viewpager.
How could I implement something like that?
Instead of replacing fragments, add fragments to the fragment manager and show/hide them.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
addFragmentsToManager();
}
private void addFragmentsToManager() {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
fragmentTransaction.add(R.id.flContent, tripFragment,
tripFragment.getClass().getSimpleName());
fragmentTransaction.add(R.id.flContent, notificationFragment,
notificationFragment.getClass().getSimpleName());
fragmentTransaction.add(R.id.flContent, searchFragment,
searchFragment.getClass().getSimpleName());
fragmentTransaction.add(R.id.flContent, profileFragment,
profileFragment.getClass().getSimpleName());
fragmentTransaction.hide(tripFragment);
fragmentTransaction.hide(notificationFragment);
fragmentTransaction.hide(searchFragment);
fragmentTransaction.hide(profileFragment);
fragmentTransaction.commit();
}
private void changeTab(int position) {
Fragment fragment;
switch (position) {
fragment = .....// get framgnet from position
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.fade_in,
R.anim.fade_out);
fragmentTransaction.hide(prvFragment);
fragmentTransaction.show(fragment).commit();
prvFragment = fragment;
}
My current Android application has two Fragments:-
ListFragment
Detailfragment
My Layout XML resembles:-
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_anchorGravity="bottom"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</FrameLayout>
I display the ListFragment first as follows:-
final FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(containerId, mListFragment, LIST_FRAGMENT_TAG);
if (mLandscape) {
} else {
fragmentTransaction.addToBackStack(LIST_FRAGMENT_TAG);
}
fragmentTransaction.commit();
When the user clicks on a List item, I want to hide the List so that I keep the current list position etc.. and display the detailFragment.
Heres the code I use to perform this UI change:-
mDetailFragment = new DetailFragment();
final Bundle fragmentArguments = new Bundle();
fragmentArguments.putString(ITEM_KEY, item.getKey());
mDetailFragment.setArguments(fragmentArguments);
final FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (mLandscape) {
fragmentTransaction.replace(containerId, mDetailFragment, DETAIL_FRAGMENT_TAG);
} else {
fragmentTransaction.hide(mListFragment);
fragmentTransaction.add(containerId, mDetailFragment, DETAIL_FRAGMENT_TAG);
}
fragmentTransaction.commit();
The above code all works fine and I can transition between the List and Detail Fragments successfully.
The issue I have is that when the user presses the BACK BUTTON on the detail Fragment to return to the ListFragment they return to a blank screen.
I have this code in my Activity to remove the detail fragment and show the hidden List fragment:-
#Override
public void onBackPressed() {
if (mLandscape) {
} else {
if (mListFragment.isHidden()) {
final FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(mDetailFragment);
fragmentTransaction.show(mListFragment);
fragmentTransaction.commit();
}
}
super.onBackPressed();
}
Why is fragmentTransaction.show(mListFragment); not showing the hidden ListFragment?
NOTE: So that I always rebuild my ui completely on orientation changes I have passed a null bundle to super.onCreate(savedInstanceStateNull);
private final Bundle savedInstanceStateNull = null;
private boolean mLandscape = false;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceStateNull);
setContentView(R.layout.activity_article_list);
mLandscape = getResources().getBoolean(R.bool.landscape);
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
manageFragments();
}
Heres how I fixed this issue:-
First remove my overriden onBackPressed()
Change display ListFragment to this:-
final FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(containerId, mListFragment, LIST_FRAGMENT_TAG);
fragmentTransaction.commit();
Change display detailFragment to this:-
mDetailFragment = new DetailFragment();
final Bundle fragmentArguments = new Bundle();
fragmentArguments.putString(ITEM_KEY, item.getKey());
mDetailFragment.setArguments(fragmentArguments);
final FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (mLandscape) {
fragmentTransaction.replace(containerId, mDetailFragment, DETAIL_FRAGMENT_TAG);
} else {
fragmentTransaction.hide(mListFragment);
fragmentTransaction.add(containerId, mDetailFragment, DETAIL_FRAGMENT_TAG);
fragmentTransaction.addToBackStack(DETAIL_FRAGMENT_TAG);
}
fragmentTransaction.commit();
I am calling a one fragment from a java class, on click of one textview I am calling one fragment this is my code of onclick.
returnTheProduct.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fragmentManager = context.getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ProductReturnFragment productReturnFragment = new ProductReturnFragment();
fragmentTransaction.replace(R.id.layout_marketplace_forms, new MarketPlaceFormsActivity());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
Add like :
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (isBackPressed)
fragmentTransaction.addToBackStack(null);
fragmentTransaction.replace(R.id.framelayout,attachedfragmentHere);
fragmentTransaction.setCustomAnimations(R.anim.fade_in,R.anim.fade_out);
fragmentTransaction.commit();
Just import the following two statements:
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
And,
private FragmentTransaction mFragmentTransaction;
private FragmentManager mFragmentManager;
.....
Then on your click listener you can write below code :
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.layout_marketplace_forms, new ProductReturnFragment());
mFragmentTransaction.commit();
Hope you get your answer. :D
From class call Activity on which you are calling fragments like below code.
Intent intent = new Intent(context, CallingActivity.class);
intent.putExtra("key", "value");
context.startActivity(returnFormIntent);
In your CallingActivity define below code.
private FragmentManager fragmentManager = getSupportFragmentManager();
private YourFragment yourFragment;
if (getIntent().hasExtra("key")) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
yourFragment = new YourFragment();
fragmentTransaction.add(R.id.layout_your_fragment, yourFragment);
fragmentTransaction.commit();
}
like above we can call fragments from other nonActivity classes.
I am making a simple demo project using Fragments, in which i am calling SecondFragment from FirstFragment on button click.
And i called SecondFragment without any issue, but i getting view of both the Fragments SecondFragment and FirstFragment
So where i am doing mistake ?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new FirstFragment()).commit();
}
}
public static class FirstFragment extends Fragment {
Button buttonCallSecondFragment;
public FirstFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_first, container,
false);
buttonCallSecondFragment = (Button) rootView.findViewById(R.id.button1);
buttonCallSecondFragment.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentManager fm = getFragmentManager();
SecondFragment fragment = new SecondFragment();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.container, fragment);
ft.commit();
}
});
return rootView;
}
}
public static class SecondFragment extends Fragment {
public SecondFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_second, container,
false);
return rootView;
}
}
}
You need to remove the first fragment, you can do that either by usingreplace or first calling remove then add
To be able to press the back button add the transaction to the back stack,you do that by calling addToBackStack(tag) on your fragment manager. Tag may be null.
You are adding fragment on already displaying fragment in your android app.
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.container, fragment);
ft.commit();
Do not add fragment but replace fragment when already one fragment is loaded on activity.
So for implementing that :
Please add your code in OnCreate() and add below code to your click listener :
FragmentManager fm = getFragmentManager();
SecondFragment fragment = new SecondFragment();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, fragment);
ft.addToBackStack(null);
ft.commit();
Thank you.!!
if you want to replace fragment you should call replace in place of add :
buttonCallSecondFragment = (Button) rootView.findViewById(R.id.button1);
buttonCallSecondFragment.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentManager fm = getFragmentManager();
SecondFragment fragment = new SecondFragment();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}
});
You are adding the fragment so it will come on top of another fragment give background color to rootview of scecond fragment
you can also load that as Nested fragment
public void addFragB() {
FragmentManager childFragMan = getChildFragmentManager();
FragmentTransaction childFragTrans = childFragMan.beginTransaction();
SecondFragment 2ndfrag = new SecondFragment();
childFragTrans.add(R.id.fragA_LinearLayout, 2ndfrag);
//childFragTrans.addToBackStack("");
childFragTrans.commit();
}
No, you cannot communicate between fragments like this. You need to communicate between fragments via the container activity.
I suggest, you make a navigation method in your activity and switch/call between fragments from there.
Below is a small snippet:
In your container activity:
Fragment fragment;
public void navigateTo(Fragment newFragment,boolean addToBackStack) {
this.fragment = newFragment;
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.fragment_container, newFragment);
if(addToBackStack)
ft.addToBackStack(fragment.getClass().getSimpleName());
ft.commit();
}
If you are going from Fragment1 to Fragment2 then in your Fragment1 do the following:
((YourContainerActivity) getActivity()).navigateTo(new Fragment2(),false); //if you want to add to back stack make 2nd argument true.
private void selectItem(int position) {
selectedPosition = position;
// update the main content by replacing fragments
fragment = null;
fragmentStack = new Stack<Fragment>();
switch (position) {
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
}
//redirect the screen
if (fragment != null) {
fragmentManager = getSupportFragmentManager();
redirectScreen(fragment);
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(title[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
I have to call a fragment class from baseAdapter class. I have created a method in main fragment class like this
public void switchContent(Fragment fragment) {
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new Reply_ozoneFragment()).commit();
}
and calling this method in adapter class like this.
holder.bt_reply.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
fragment.switchContent();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.frame_container, new Reply_ozoneFragment());
fragmentTransaction.commit();
/*Intent reintent = new Intent(context,Reply_ozoneFragment.class);
//reintent.putExtra("userid", S);
context.startActivity(reintent);
*/
}
});
but this is not working properly and my app crashes every time. Please tell me what mistake I am doing here.
remove fragment.switchContent(); from your adapter button onclick() method and change
FragmentManager fm = getFragmentManager();
to
FragmentManager fm = getActivity().getSupportFragmentManager()