Fragment not launched from another fragment - android

I have a fragment with two buttons: btnEdit and btnExamen.
Each button has an OnClickListener.
Here is the code:
btnEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPref = getActivity().getSharedPreferences(MISDATOS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mPref.edit();
editor.putString("id_empleo","No");
editor.putString("id_grado","No");
editor.apply();
HomeFragment firstFragment = new HomeFragment();
((MainActivity)getActivity()).getSupportFragmentManager().beginTransaction()
.replace(R.id.frame, firstFragment).commit();
}
});
btnExamen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "Iniciando examen", Toast.LENGTH_SHORT).show();
ExamenesFragment secondFragment = new ExamenesFragment();
((MainActivity)getActivity()).getSupportFragmentManager().beginTransaction()
.replace(R.id.frame, secondFragment).commit();
}
});
The issue is that the first method works fine, HomeFragment is launched.
But the second method is not launching fragment ExamFragment. The toast is launched.
I am not able to see any difference at the code that could be the problem.

Replace the current Fragment with the new Fragment and push transaction onto the backstack. This preserves back button behaviour...
Creating a new Activity really defeats the whole purpose to use fragments anyway...very counter productive.
btnEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
...
// Create new fragment and transaction
HomeFragment firstFragment = new HomeFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the frame view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.frame, firstFragment );
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
});
btnExamen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
...
ExamenesFragment secondFragment= new ExamenesFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame, secondFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});

Related

Fragment to fragment is overlapping

I want to open a fragment from my recyclerview adapter class. I am using holder class in bindviewholder, where I code to open my fragment but it overlaps on the current fragment.
holder.buys.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getcall();
}
});
and
public void getcall() {
FragmentManager fm = context.getActivity().getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Buy b = new Buy();
ft.replace(R.id.p_buy, b);
ft.addToBackStack(null);
ft.commit();
}

Fragment keeps on adding when the button is clicked

ImageButton imageButton3 =(ImageButton)view.findViewById(R.id.item_two_timer_id);
imageButton3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getFragmentManager().beginTransaction()
.replace(R.id.container2_main , new TimerFragment2())
.addToBackStack(null)
.commit();
While adding fragment add a tag to identify it.get the fragment by tag and see if the fragment is present.If it is not present create a new Instance and add it.
ImageButton imageButton3 = (ImageButton) view.findViewById(R.id.item_two_timer_id);
imageButton3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TimerFragment2 timerFragment2;
timerFragment2 = (TimerFragment2) getFragmentManager().findFragmentByTag(TimerFragment2.class.getSimpleName());
if(timerFragment2==null){
timerFragment2=new TimerFragment2();
getFragmentManager().beginTransaction()
.replace(R.id.container2_main, timerFragment2,TimerFragment2.class.getSimpleName())
.addToBackStack(TimerFragment2.class.getSimpleName())
.commit();
}else {
//Dont create fragment again
}
}
});
please try this code if you are setting one fragment
private void initFragment() {
SelectTypeFragment fragment = new SelectTypeFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right); //if you want to set animation
fragmentTransaction.replace(R.id.fl_addvehicle, fragment);
fragmentTransaction.commit();
}
if you are replacing one fragment to with another.
void goToNextFragment() {
((CreateOfferActivity) this.getActivity()).setCreateOfferModel(createOfferModel);
SelectVehicleFragment fragment = new SelectVehicleFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
fragmentTransaction.replace(R.id.fl_create_offer, fragment).
addToBackStack("select_vehicle");
fragmentTransaction.commit();
}
You can check it in Fragment but first,
You need to create an object of Fragment TimerFragment2 and check if it's visible or not in timerFragment.isVisible()
ImageButton imageButton3 =(ImageButton)view.findViewById(R.id.item_two_timer_id);
TimerFragment2 timerFragment = new TimerFragment2();
imageButton3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!timerFragment.isVisible())
getFragmentManager().beginTransaction()
.replace(R.id.container2_main , timerFragment)
.addToBackStack(null)
.commit();

Call Fragment from activity

I have a Navigation drawer activity and it contains couple of Fragments. My problem is I want to call the Navigation drawer activity class in button click which should have fragment B, by default navigation drawer has fragment A.
I am posting my code here:
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(getApplicationContext(),Navigation.class));
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.content_frame, new FragmentB());
tx.commit();
finish();
}
});
Where content_frame is the area were I want to replace fragment B view..
try this approach
in your button click send extra data to check what fragment you need
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), Navigation.class);
intent.putExtra("SELECTEDVALUE", 2);//1 for fragament A use 2 for fragment B
startActivity(intent);
}
});
Now, in your oncreate() method of Navigation Activity
Bundle extras = savedInstanceState != null ? savedInstanceState : getIntent().getExtras();
int selectedValue = extras.getInt("SELECTEDVALUE");
switch (selectedValue) {
case 1:
goToFragment(new A(), false);
break;
case 2:
goToFragment(new B(), false);
break;
finally gotoFragment is...
private void goToFragment(Fragment fragment, boolean addToBackStack) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.replace(R.id.container, fragment).commit();
}
Try below code;
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Fragment fragment = new FragmentB();
FragmentManager fm =getActivity().getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
});
Just make an instance of FragmentB like this
FragmentB fragment - new FragmentB();
and then pass it like this .
tx.replace(R.id.content_frame, fragment);
tx.commit();
now you can access all the variables and methods of fragment from your activity using instance fragment. like this fragment.methodName();
Try this code:
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getActivity().getSupportFragmentManager().beginTransaction()
.add(R.id.content_frame, new FragmentB(), "fragmentB").addToBackStack(null).commit();
}
});
hope it'll solve your problem.

Why my new fragment can't subscribe otto

I have made a post in my activity, and it works well in the first fragment BlankFragment, however, when I tried to replace BlankFragment with BlackFragment2, and do the same subscribe, it can't subscribe anymore, here is the code.
MainActivity:
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().add(R.id.fr1, new BlankFragment()).commit();
button = (Button)findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BusStation.getBus().post(new Message("hellworld"));
}
});
btn2 = (Button)findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager1 = getSupportFragmentManager();
fragmentManager1.beginTransaction().replace(R.id.fr1, new BlankFragment2()).commit();
BusStation.getBus().post(new Message("zhengzhi zhou"));
}
});
}
BlankFragment and BlankFragment2 are using the same code:
#Override
public void onResume() {
super.onResume();BusStation.getBus().register(this);
}
#Override
public void onPause() {
super.onPause();
BusStation.getBus().unregister(this);
}
#Subscribe
public void receiveMsg(Message msg){
textView.setText(msg.getMsg());
}
Can anyone help me with this?
Use commitNow() instead Of commit()
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Replace the contents of the container with the new fragment
ft.replace(R.id.fragment_place, fragment);
// or ft.add(R.id.your_placeholder, new FooFragment());
// Complete the changes added above
ft.commitNow();

Check any fragment is presented in Layout in android?

In my application.there are five button . when curresponding button is pressed the fragment will load in the linear layout.what i want is if any button is pressed the current fragment in the layout should dettach and fragment corresponding to button should placed.i need a common method to remove previously placed fragment.i know we can remove the fragment specifically using its id.i dont need that.i want a common method to remove the fragments.here is my java class.
Button b1, b2, b3, b4, b5;
// Declare Variables
ActionBar mActionBar;
ViewPager mPager;
FragmentManager fm;
setContentView(R.layout.activity_main);
fm = getSupportFragmentManager();b1 = (Button) findViewById(R.id.fbbutton1);
b2 = (Button) findViewById(R.id.fbbutton2);
b3 = (Button) findViewById(R.id.fbbutton3);
b4 = (Button) findViewById(R.id.fbbutton4);
b5 = (Button) findViewById(R.id.fbbutton5);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragmentTab1 f1 = new FragmentTab1();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragmentgroup, f1, "A");
ft.commit();
}
});
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (getSupportFragmentManager().findFragmentById(R.id.detail_screen) != null) {
getSupportFragmentManager().popBackStack();
} //to do add fragment
FragmentTab2 f2 = new FragmentTab2();
FragmentTransaction ft = fm.beginTransaction();
// fm.popBackStack(fm.getBackStackEntryCount(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
// - Use FragmentManager.getBackStackEntryCount()/getBackStackEntryAt().getId() to retrieve the ID of the first entry on the back stack, and FragmentManager.popBackStack(int id, FragmentManager.POP_BACK_STACK_INCLUSIVE).
// ft.replace(R.id.fragmentgroup, f2);
ft.add(R.id.fragmentgroup, f2, "B");
ft.commit();
}
});
b3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragmentTab3 f3 = new FragmentTab3();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragmentgroup, f3, "C");
ft.commit();
}
});
b4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragmentTab4 f4 = new FragmentTab4();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragmentgroup, f4, "D");
ft.commit();
}
});
b5.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragmentTab5 f5 = new FragmentTab5();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragmentgroup, f5, "E");
ft.commit();
}
});
the layout for showing the fragments in my xml
<LinearLayout
android:id="#+id/fragmentgroup"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#F2A4A9"
android:orientation="horizontal" >
You need a common method to replace fragments corresponding to your button click.
So, you can use replace method in FragmentTransaction to do the job in common method.
Code will be,
public void replaceAFragment(int frag_container, Fragment aFragment, String tag) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(frag_container, aFragment, tag);
transaction.commit();
}
Whenever you want to load a fragment, you no need to remove but simply replace it with your new fragment,
b3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragmentTab3 f3 = new FragmentTab3();
replaceAFragment(R.id.fragmentgroup,f3,"FRAGMENT THREE");
}
});
b4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragmentTab4 f4 = new FragmentTab4();
replaceAFragment(R.id.fragmentgroup,f4,"FRAGMENT FOUR");
}
});
b5.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragmentTab5 f5 = new FragmentTab5();
replaceAFragment(R.id.fragmentgroup,f5,"FRAGMENT FIVE");
}
});
Now you have a common method to place fragments. You may also do, detach and remove inside with the tag.

Categories

Resources