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();
Related
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();
}
});
each time when I open a fragment, it will be added to the stack.
So, the back button returns to me according to the stack.
I think I have to delete the stack each time I open a fragment.
if so how can I do that?
here is my code
public class MainActivity extends AppCompatActivity {
private FragmentManager fragmentManager;
FragmentTransaction transaction;
LinearLayout firstColumn,secondColumn,thirdColumn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getSupportFragmentManager();
firstColumn = (LinearLayout) findViewById(R.id.first_column);
secondColumn = (LinearLayout) findViewById(R.id.second_column);
firstColumn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.main_container_frame_layout, new FirstFragment());
transaction.commit();
}
});
secondColumn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.main_container_frame_layout, new SecondFragment());
transaction.commit();
}
});
}
}
Thank you in advance.
getSupportFragmentManager().popBackStack() will return back according to the stack. below code will help you.
#Override
public void onBackPressed() {
if(getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
}else
super.onBackPressed();
}
Please help
Can anyone help me out plz
public class FragmentForScrollBar extends Fragment {
private HorizontalScrollView hsv;
Button left, right;
int currentScrollX = 0;
Toolbar Toolbar;
RelativeLayout RelativeLayout;
ImageButtonhome,player,download,playlist,stream,wish,hit,instrument,news,extras;
TextView t;
FragmentTransaction fragmentTransaction;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmentforscrollbar,null);
MyPlayer playerFragment = new MyPlayer();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, playerFragment, null);
fragmentTransaction.commit();
hsv = (HorizontalScrollView) view.findViewById(R.id.horizontalScrollView);
t = (TextView)view.findViewById(R.id.toolbartitle);
home =(ImageButton)view.findViewById(R.id.imageButtonhomea);
player =(ImageButton)view.findViewById(R.id.imageButtonaa);
playlist =(ImageButton)view.findViewById(R.id.imageButtonca);
download =(ImageButton)view.findViewById(R.id.imageButtonba);
stream =(ImageButton)view.findViewById(R.id.imageButtonda);
wish =(ImageButton)view.findViewById(R.id.imageButtonea);
hit =(ImageButton)view.findViewById(R.id.imageButtonfa);
instrument =(ImageButton)view.findViewById(R.id.imageButtonga);
news =(ImageButton)view.findViewById(R.id.imageButtonha);
extras =(ImageButton)view.findViewById(R.id.imageButtonia);
home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Home");
Fragment Home = new Home();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, Home);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
player.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Player");
Fragment Myplayer = new MyPlayer();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, Myplayer);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
playlist.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment yourPlayList = new Your_Playlist();
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.containerView, yourPlayList);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
download.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("MyDownload");
Fragment myDownload = new Downloads();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,myDownload);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
stream.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Streaming");
Fragment uzBek1 = new UzBek();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,uzBek1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
wish.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Your Wishes");
Fragment sendWishes = new wishes();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,sendWishes);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
hit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Hit Parade");
Fragment hitParade = new hits();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,hitParade);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
instrument.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Uzbek Instruments");
Fragment uzBek1 = new UzBek();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,uzBek1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
news.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("News");
Fragment news = new news();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,news);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
extras.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Extras");
Fragment extras = new extras();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, extras);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}
}
sorry code was too long so i captured the image of the xml module file
Please see the above code and let me know the solution for scrolling the mini scroll layout on left and right by image buttons.
I am using two arrow buttons and tell me this also that what to do if i want to hide them in between the scrolling. Now can someone help me out please
For anyone who might be puzzled up with the same issue:
public class FragmentForScrollBar extends Fragment {
SeekBar verticalSeekBar=null;
private HorizontalScrollView hsv, hsv2;
Toolbar Toolbar;
ImageButton home,player,download,playlist,stream,wish,hit,instrument,news,extras, bP,bN;
ImageButton homea,playera,downloada,playlista,streama,hita,wisha,instrumenta,newsa, extrasa;
TextView t;
FragmentTransaction fragmentTransaction;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmentforscrollbar,null);
MyPlayer playerFragment = new MyPlayer();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, playerFragment, null);
fragmentTransaction.commit();
hsv = (HorizontalScrollView) view.findViewById(R.id.horizontalScrollView);
t = (TextView)view.findViewById(R.id.toolbartitle);
home =(ImageButton)view.findViewById(R.id.imageButtonhomea);
player =(ImageButton)view.findViewById(R.id.imageButtonaa);
playlist =(ImageButton)view.findViewById(R.id.imageButtonca);
download =(ImageButton)view.findViewById(R.id.imageButtonba);
stream =(ImageButton)view.findViewById(R.id.imageButtonda);
wish =(ImageButton)view.findViewById(R.id.imageButtonea);
hit =(ImageButton)view.findViewById(R.id.imageButtonfa);
instrument =(ImageButton)view.findViewById(R.id.imageButtonga);
news =(ImageButton)view.findViewById(R.id.imageButtonha);
extras =(ImageButton)view.findViewById(R.id.imageButtonia);
playera =(ImageButton)view.findViewById(R.id.imageButtonaab);
playlista =(ImageButton)view.findViewById(R.id.imageButtoncab);
downloada =(ImageButton)view.findViewById(R.id.imageButtonbab);
streama =(ImageButton)view.findViewById(R.id.imageButtondab);
wisha =(ImageButton)view.findViewById(R.id.imageButtoneab);
hita =(ImageButton)view.findViewById(R.id.imageButtonfab);
instrumenta=(ImageButton)view.findViewById(R.id.imageButtongab);
newsa =(ImageButton)view.findViewById(R.id.imageButtonhab);
extrasa =(ImageButton)view.findViewById(R.id.imageButtoniab);
verticalSeekBar=(SeekBar)view.findViewById(R.id.seekBar);
ImageButton bP = (ImageButton) view.findViewById(R.id.left);
ImageButton bN = (ImageButton) view.findViewById(R.id.right);
bP.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//value 500 is arbitrarily given. if you want to achieve
//element-by-element scroll you should get the width of the
//previous element dynamically or if the elements of the
//list have uniform width just put that value instead
hsv.scrollTo(0,-500);
//if it's the first/last element you can bPrevoius.setEnabled(false)
}
});
bN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
hsv.scrollTo(500,0);
}
});
home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Home");
Fragment Home = new Home();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, Home);
fragmentTransaction.addToBackStack(null);
// fragmentTransaction.popBackStack();
fragmentTransaction.commit();
// t.setText("Player");
}
});
player.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Player");
Fragment Myplayer = new MyPlayer();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, Myplayer);
fragmentTransaction.addToBackStack(null);
// fragmentTransaction.popBackStack();
fragmentTransaction.commit();
}
});
playlist.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// DrawerNavigation mainActivity = new DrawerNavigation();
// mainActivity.drawerLayout.closeDrawers();
Fragment yourPlayList = new Your_Playlist();
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.containerView, yourPlayList);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
// t.setText("PlayList");
}
});
download.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("MyDownload");
Fragment myDownload = new Downloads();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,myDownload);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
stream.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Streaming");
Fragment uzBek1 = new UzBek();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,uzBek1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
wish.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Your Wishes");
Fragment sendWishes = new wishes();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,sendWishes);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
hit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Hit Parade");
Fragment hitParade = new hits();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,hitParade);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
instrument.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Uzbek Instruments");
Fragment uzBek1 = new UzBek();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,uzBek1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
news.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("News");
Fragment news = new news();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment,news);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
extras.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setText("Extras");
Fragment extras = new extras();
fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, extras);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}
}
Just add image buttons on both ends and seOnclickListener() on them then scroll them up to a desired value as shown in the code.
I have created a method in PlaceDetailsActivity.java given below:
public void implementFragment()
{
TestFrag testingFragmentFragment = new TestFrag();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.map_fragment, testingFragmentFragment);
fragmentTransaction.commit();
}
If I try to access this method of PlaceDetailsActivity.java from DetailsInfoActivity.java like this
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PlaceDetailsActivity placeDetailsActivity= new PlaceDetailsActivity();
placeDetailsActivity.implementFragment();
finish();
}
});
I get null value here FragmentManager fragmentManager = getFragmentManager();
But if I call it from PlaceDetailsActivity's onCreate then it works properly. Here TestFrag is a Fragment class. My question is what to do
now if I want to call implementFragment() method of PlaceDetailsActivity.java from DetailsInfoActivity.java without getting null value?
Instead of getFragmentManager() use getSupportFragmentManager().
Change your code to the following one. I assume your previous code is placed inside the PlaceDetailsActivity class.
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
implementFragment();
finish();
}
});
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.