Fragment fragment = null;
fragment = new Frag2();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
I tried above code to move from one fragment to another fragment. It works fine.But when I move to next fragment i need to go to the previous fragment.Can u help me to achieve that
public void name() {
Fragment fragment=new Frag2();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).addToBackStack(null).commit();
}
Add this lines in your fragment. hen Add this line on your next fragment
public void goback() {
getFragmentManager().popBackStack();
}
Create a OnClickListener like this
OnClickListener backClick = new OnClickListener() {
#Override
public void onClick(View v) {
getActivity().onBackPressed();
}
};
in onCreateView method of your Fragment set OnClickListener like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view;
view = inflater.inflate(R.layout.fragment_second, null);
btnBack = (Button) view.findViewById(R.id.btnBack);
btnBack.setOnClickListener(backClick);
return view;
}
Related
I am trying to make navigation drawer with viewpager because i want the navigation drawer to be full screen. i have created a fragment as and added to viewpager. from the navigation fragment on button click i want to open another fragment. i added a code to open another fragment but it crashes the app. how can i do it.
My Button Code
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab2, container, false);
Button btnOpenSlideShow = (Button)v.findViewById(R.id.btnOpenSlideShow);
btnOpenSlideShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AllFragment allFragment = new AllFragment();
FragmentManager manager = getFragmentManager();
manager.beginTransaction().replace(R.id.mainLayout,allFragment,allFragment.getTag())
.commit();
}
});
return v;
}
}
Use getSupportFragmentManager()
as FragmentManager manager = getActivity().getSupportFragmentManager();
AllFragment allFragment = new AllFragment();
manager.beginTransaction()
.replace(R.id.frameLayout, allFragment )
.addToBackStack(null)
.attach(myFragment)
.commit();
Hope this helps.
instead of getFragmentManager use getChildFragmentManager
AllFragment allFragment = new AllFragment();
FragmentManager manager = getChildFragmentManager();
manager.beginTransaction().replace(R.id.mainLayout,allFragment,allFragment.getTag())
.commit();
I'm using some fragments programmatically in activity. There is one button in my first fragment and when i click to this button, it replaces to second fragment.My second fragment's background is 90% transparent, and when it starts, i can see button which is situated in first fragment, and it also works. I want to stop or do something, because i dont want to see first fragment features and use it.
First Fragment
public class RegistrationFirstFragment extends Fragment {
RegistrationSecondFragment rf;
ImageButton btnNewUser,btnNewAgent;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//return super.onCreateView(inflater, container, savedInstanceState);
View v =inflater.inflate(R.layout.fragment_registration_first,container,false);
rf = new RegistrationSecondFragment();
btnNewUser = (ImageButton)v.findViewById(R.id.btnNewUserRegistrationFirstFragment);
btnNewAgent = (ImageButton)v.findViewById(R.id.btnNewAgentRegistrationFirstFragment);
btnNewUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Transaction completed succesfully", Toast.LENGTH_LONG).show();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.flRegistrationFirst, rf);
ft.commit();
}
});
return v;
}
}
Second Fragment
public class RegistrationSecondFragment extends Fragment {
RegistrationFirstFragment rtl;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rtl = new RegistrationFirstFragment();
//return super.onCreateView(inflater, container, savedInstanceState);
View v =inflater.inflate(R.layout.fragment_registration_second,container,false);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
// ft.replace(R.id.flRegistrationFirst, rf);
ft.remove(rtl);
ft.commit();
return v;
}
}
Main Activity
public class RegistrationActivity extends AppCompatActivity{
RegistrationFirstFragment fr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
fr = new RegistrationFirstFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.flRegistrationFragment,fr);
ft.commit();
}
}
You can put
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
(the parameter fragment would be an instance of your second fragment)
into your onClick(View view){...} method to change the fragment instead of adding it.
Next time code for understanding your problem btw ;)
Add to fragment layout android:clickable="true". Int his way fragment will catch event so the click will not be caught by "main fragment".
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
Give android:clickable="true" for Second Fragment root layout parent, when ever fragment opens It catches the click event of root and ignored previous click events.
Second One: If u use replace Fragment it's better than add fragment.
getActivity().getSupportFragmentManager().beginTransaction().replace
(R.id.YOUR_CONTAINER, 'FragmentObject').addToBackStack("TAG").commitAllowingStateLoss();
I have an ImageView. I want to move from one fragment to another fragment on a click of an Imageview, the same way like we can move from one activity to another using
Intent i=new Intent(MainActivity.this,SecondActivity.class);
startActivity(i);
How can I do this? Can anyone explain to me step by step?
My codes are as follows:
mycontacts.class
public class mycontacts extends Fragment {
public mycontacts() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = super.getView(position, convertView, parent);
ImageView purple=(ImageView)v.findViewById(R.id.imageView1);
purple.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//how to go to tasks fragment from here???
}
});
return view;
}
}
tasks.class
public class tasks extends Fragment {
public tasks() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout_one, container,
false);
return view;
}
}
purple.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = new tasks();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
You write the above code...there we are replacing R.id.content_frame with our fragment.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_profile, container, false);
notification = (ImageView)v.findViewById(R.id.notification);
notification.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.container,new NotificationFragment());
fr.commit();
}
});
return v;
}
Add this code where you want to click and load Fragment.
Fragment fragment = new yourfragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
When you are inside an activity and need to go to a fragment use below
getFragmentManager().beginTransaction().replace(R.id.*TO_BE_REPLACED_LAYOUT_ID*, new tasks()).commit();
But when you are inside a fragment and need to go to a fragment then just add a getActivity(). before, so it would become
getActivity().getFragmentManager().beginTransaction().replace(R.id.*TO_BE_REPLACED_LAYOUT_ID*, new tasks()).commit();
as simple as that.
The *TO_BE_REPLACED_LAYOUT_ID* can be the entire page of activity or a part of it, just make sure to put an id to the layout to be replaced. It is general practice to put the replaceable layout in a FrameLayout .
inside your onClickListener.onClick, put
getFragmentManager().beginTransaction().replace(R.id.container, new tasks()).commit();
In another word, in your mycontacts.class
public class mycontacts extends Fragment {
public mycontacts() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = super.getView(position, convertView, parent);
ImageView purple = (ImageView) v.findViewById(R.id.imageView1);
purple.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
getFragmentManager()
.beginTransaction()
.replace(R.id.container, new tasks())
.commit();
}
});
return view;
}
}
now, remember R.id.container is the container (FrameLayout or other layouts) for the activity that calls the fragment
You can move to another fragment by using the FragmentManager transactions. Fragment can not be called like activities,. Fragments exists on the existence of activities.
You can call another fragment by writing the code below:
FragmentTransaction t = this.getFragmentManager().beginTransaction();
Fragment mFrag = new MyFragment();
t.replace(R.id.content_frame, mFrag);
t.commit();
here "R.id.content_frame" is the id of the layout on which you want to replace the fragment.
You can also add the other fragment incase of replace.
If you're looking for the Kotlin version of the above code, you can do it in this way, and you call replaceFragment(RequiredFragment()) at onClickListener or wherever you want.
private fun replaceFragment(fragment: Fragment) {
val transaction = activity!!.supportFragmentManager.beginTransaction()
transaction.replace(R.id.frame, fragment)
transaction.commit()
}
private boolean loadFragment(Fragment fragment) {
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fl_fragment_container, fragment)
.commit();
return true;
}
return false;
}
in kotlin, put inside of your current running fragment button.setOnClickListener
val bpfragment = TwoFragment()
activity?.supportFragmentManager?.beginTransaction()?.replace(R.id.fragment_container, bpfragment)?.commit()
val fragment = YourFragment3()
val fm : FragmentManager= requireActivity().supportFragmentManager
val ft: FragmentTransaction = fm.beginTransaction()
ft.replace(R.id.container, fragment)
ft.commit()
(activity as MainActivity).binding.viewPager.setCurrentItem(2)
We can use this one-
purple.setOnClickListener(view1 -> {
Fragment fragment = new task();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.frame, fragment);
transaction.addToBackStack(null);
transaction.commit();
});
I want to transact from one fragment to another... below given is my snippet...
I want transaction on button click...
public class Main extends Fragment {
// final View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main, container, false);
Button camera=(Button)rootView.findViewById(R.id.button1);
camera.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.layout.main, new CameraActivity());
ft.commit();
}
});
return rootView;
}
so please Assist me in solving this...
[1]<--->[2]<--->[3]<--->[4]<--->[5]
where []=fragments...
[1]- has buttons a,b,c,d,e
How to move from 1 to 3 OnClick of c ...
Here is the Camera Activity
public class CameraActivity extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.camera, container, false);
return rootView;
}
}
first of all this cant be activity
ft.replace(R.layout.main, new CameraActivity());
it has to be fragment
put something like this in your onclick listener in FirstFragment:
Bundle bundle = new Bundle();
bundle.putString("key", value); //if you want to pass parameter to second fragment
SecondFragment fragment = new SecondFragment();
fragment.setArguments(bundle);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment).addToBackStack(null);
ft.commit();
than in your SecondFragment onclick listener:
Bundle bundle = new Bundle();
bundle.putString("key", value); //if you want to pass parameter to second fragment
ThirdFragment fragment = new ThirdFragment();
fragment.setArguments(bundle);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment).addToBackStack(null);
ft.commit();
and so on ...
method addToBackStack(null) is providing you back as simple as that. You could also pass some tag in that method if you need it, but I think from question you want be needing it.
Also if you need to start Activity than dont use this method, use Intent instead.
hope it helps
I'm trying out some fragments right now. I've got a fragment with a button and when I click that button I switch to another fragment. Now when I push on the back button I return to the first fragment that's good. Now when I click again on that button a new fragment is started. So I always start a new fragment. I think thats not the way it needs to be done. Is there a better way to for example resume the already created fragment?
My code:
public static class PlaceholderFragment extends Fragment{
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button test = (Button)rootView.findViewById(R.id.button);
test.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
Log.d("Test", "Button clicked.");
TestFrag newFragment = new TestFrag();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
return rootView;
}
}
public static class TestFrag extends Fragment {
public TestFrag() {
Log.d("Test","New fragment");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main2, container, false);
return rootView;
}
}
When you use the fragment transaction you can specify a tag for the fragment
fragmentTransaction.add(R.id.main_fragment, newFragment, fragTag);
fragmentTransaction.commit();
Then later you can find a fragment by tag from the fragmentManager
newFragment = fragMan.findFragmentByTag(fragTag);
if newFragment then is null you should create the Fragment since it wasnt found by the tag
Thanks to Joakim:
fragmentTransaction.add(R.id.main_fragment, newFragment, fragTag); fragmentTransaction.commit();
Then later you can find a fragment by tag from the fragmentManager
newFragment = fragMan.findFragmentByTag(fragTag); if newFragment then is null you should create the Fragment since it wasnt found by the tag. If the tag isn't null you can just add and commit the new one.