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.
Related
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 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
Hello I want to start a Fragment on back-press of Simple Activity
I have done like this
public boolean onKeyDown(int keyCode, KeyEvent event) {
Fragment fragment = null;
fragment = new MainActivity();
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.commit();
return true;
}
return super.onKeyDown(keyCode, event);
}
But not working..
Try this:
#Override
public void onBackPressed() {
android.support.v4.app.FragmentManager mFragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.fragmentView, new Fragment()).commit();
}
Try to use below code.
#Override
public void onBackPressed() {
Fragment fragment = null;
fragment = new MainActivity();
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.commit();
}
Hope it will work.
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()
I am new to android working first time on fragment.I am creating an app in which I want to add a fragment on frame layout.I am able to do this but now what I want is to remove the same fragment which i added by that same button click I tried but couldn't. here is my code.
public void onClick(View v) {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
if(v.getId() == R.id.clickme){
if(getSupportFragmentManager().findFragmentById(R.layout.fragment_one) != null){
// getSupportFragmentManager().beginTransaction().remove(getSupportFragmentManager().findFragmentById(R.layout.fragment_one)).commit();
Fragment fragment = new FragmenOne();
fragmentManager.beginTransaction().remove(fragment).commit();
}else{
Fragment fragment = new FragmenOne();
// android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.my_frame, fragment)
.commit();
}
}
}
You need to do in this manner
Fragment f = fragmentManager.findFragmentById(R.id.my_frame);
if(f instanceof FragmenOne) {
FragmenOne oneFragment = (FragmenOne) f;
FragmentTransaction trans = manager.beginTransaction();
trans.remove(oneFragment);
trans.commit();
fragmentManager.popBackStack();
}else{
Fragment fragment = new FragmenOne();
fragmentManager.beginTransaction()
.replace(R.id.my_frame, fragment)
.commit();
}