Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

android - how to move from one fragment to another fragment on button click

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's simple Only three line code...

Fragment fragment = new SalesFragment();

FragmentManager fragmentManager = getFragmentManager();

fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...