Android--------jump from one fragment to another fragment

Source: Internet
Author: User

# #一, direct getactivity, replace with activity's fragmenttransation as if replaced

private void Changetoanotherfragment () {
If the V4 package is used, use Getactivity (). Getsuppoutfragmentmanager ();
Fragmentmanager fm = getactivity (). Getfragmentmanager ();
Note the use of V4 package
Fragment Fragment = new target Fragment ();
Fm.begintransaction (). Replace (container control id,fragment). commit ();
}

# #二, defining callback functions (recommended)

* Implementation steps

> # # # #定义回调, and uses callback results to implement functions

1. Define the interface and declare the methods it contains,
2. Define interface member variables,
3, the definition of interface member variables get and set methods, mainly set methods, assign values to the member variables,
4. Call to determine if the member variable is NOT NULL (NULL), the method in the interface is called.

> # # #实现回调

1. Instantiate the class that contains the callback interface, that is, define the class object
2, the object calls the Set method, the interface callback
3, implement the method in the interface, note: This implementation only works here.

# #三, examples
* Defines a class that contains an interface

Class Onefragment extends fragment{
Private Button btn;
Private OnButtonClick ONBUTTONCLICK;//2, defining interface member variables
@Override
Public View Oncreateview (layoutinflater inflater, ViewGroup container,
Bundle savedinstancestate) {
View view = Inflater.inflate (R.layout.frag_one, NULL);
BTN = (Button) View.findviewbyid (R.id.button);
Btn.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
4. If the interface member variable is not NULL, the method of the interface variable is called.
if (onbuttonclick!=null) {
Onbuttonclick.onclick (BTN);
}
}
});
Return Super.oncreateview (Inflater, container, savedinstancestate);
}
Get method for defining interface variables
Public OnButtonClick Getonbuttonclick () {
return OnButtonClick;
}
Set method for defining interface variables
public void Setonbuttonclick (OnButtonClick onbuttonclick) {
This.onbuttonclick = OnButtonClick;
}
1. Define the interface
public interface onbuttonclick{
public void OnClick (view view);
}
}

* So as long as you set the callback in Acitivty

public class Mainactivity extends Activity {
1. Instantiating class objects
Private Onefragment onefragment = new Onefragment ();
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
。。。
。。。 Omit code
2, call the object's set method, callback interface object
Onefragment.setonbuttonclick (New OnButtonClick () {
3, the method of implementing the Interface object,
@Override
public void OnClick (view view) {
Switch to Twofragment
Fm.begintransaction ()
To replace with Twofragment
. replace (r.layout.fragment_container,new twofragment ())
. commit ();
}
});
}
}

Android--------jump from one fragment to another fragment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.