1

I am using the SherlockFragments library for the sliding menu.I have list of items as menu when I click on item fragment get opened as an activity but it is a fragment.Now I am new to fragments.i don't know how to move from one fragment to another fragment.As in activity, we have intent to move to another activity.but in fragment I don't how to move to another fragment.I have a button in fragmentA.when I click on this button it moves to fragment B. By googling I came to know that it has different cycles but anyhow I get toast msg when I click button here is following code

public class Fragment2 extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.actionnetworklogin, container, false);
        Button login = (Button)view.findViewById(R.id.login);

        login.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(v.getContext().getApplicationContext(),"login clicked", 5000).show();
            }
        });
        return view;
    }   
}

Can someone please tell me how can I move one fragment to another fragment?

Is there any other method that I can use activities instead of fragments?

I have written code for all activities and java files but I don't know that sliding menu has fragmented and now I have to write all the code fragments.

Nacimota
  • 22,395
  • 6
  • 42
  • 44
Sanjeev
  • 292
  • 2
  • 5
  • 24

6 Answers6

3

It's simple Only three line code...

Fragment fragment = new SalesFragment();

FragmentManager fragmentManager = getFragmentManager();

fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
Bruce
  • 1,647
  • 4
  • 20
  • 22
Daulat Bachhav
  • 356
  • 1
  • 15
2

This is the code I use to switch fragments inside a view:

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace([viewId], fragment, tag);
fragmentTransaction.addToBackStack(tag);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.commit();
Thiago
  • 337
  • 2
  • 12
0

You can try this also:-

public void ButtonClick(View view) {
  Fragment mFragment = new YourNextFragment(); 
 getSupportFragmentManager().beginTransaction()
            .replace(R.id.content_frame, mFragment ).commit();
 }
Amit Jayaswal
  • 1,725
  • 2
  • 19
  • 36
0
FragmentManager fragmentManager = getFragmentManager();
                fragmentManager
                        .beginTransaction()
                        .replace(R.id.frame_container,
                                new TeacherFragment()).commit();
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
  • This is just equivalent to [this existing answer](http://stackoverflow.com/questions/20784516/how-to-move-from-one-fragment-to-another-fragment-on-button-click/26337764#26337764). – Pang Jul 11 '15 at 02:05
0
 //Below is the example 

 //In first Fragment 

Fragment fragment = new YourFragmentClassName();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();                       android.support.v4.app.FragmentTransaction ft= fragmentManager.beginTransaction();
ft.replace(R.id.flContent, fragment);
Bundle args = new Bundle();
// Pass the values what you want to send to next fragment 
args.putInt("Year", rYear);
args.putString("Month", rMonth);
args.putInt("Industry", rIndustry);
fragment.setArguments(args);
ft.commit();

//In Second Fragment

//In onCreateView Method get the values



int strYear= getArguments().getInt("Year");
String strMonth = getArguments().getString("Month");
strIndustry= getArguments().getInt("Industry");

//That's it very simple
mix3d
  • 4,122
  • 2
  • 25
  • 47
Sanjeev
  • 292
  • 2
  • 5
  • 24
0
 FragmentTransaction fragmenttransaction = getSupportFragmentManager().beginTransaction();
 FirstFragment regcomplainfragment = new FirstFragment();
 fragmenttransaction.replace(R.id.content_frame,   regcomplainfragment).addToBackStack("tag");
 fragmenttransaction.commit();
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57