Android programming uses the fragment bounds to the next jump and the first-level return of the implementation method _android

Source: Internet
Author: User

The examples in this article describe the implementation of Android programming using fragment-oriented and first-level returns. Share to everyone for your reference, specific as follows:

1. First attach the project structure chart:

2. First add an interface file Backhandledinterface.java, define a setselectedfragment method to set the currently loaded fragment on the top of the stack, the main interface mainactivity to implement this interface, the code is as follows:

Package Com.example.testdemo;
Public interface Backhandledinterface {public
  abstract void Setselectedfragment (backhandledfragment selectedfragment);
}

3. Define an abstract class backhandledfragment inherit from the fragment, and the fragment interface after the jump will inherit from Backhandledfragment. The abstract class Backhandledfragment defines a onbackpressed method that returns a Boolean type that is used to handle the logic of clicking the return key (physical back), if the method returns False, Indicates that the current fragment does not consume the return event, and the fragmentactivity to which the fragment belongs handles the event. The code is as follows:

 package com.example.testdemo; import android.os.Bundle; import
Android.support.v4.app.Fragment;
  Public abstract class Backhandledfragment extends Fragment {protected backhandledinterface mbackhandledinterface;
  /** * All inherited backhandledfragment subclasses will implement the logical/protected abstract Boolean onbackpressed () in this method after the physical back key is pressed;
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); if (!) ( Getactivity () instanceof Backhandledinterface)) {throw new ClassCastException ("The Hosting activity must imp
    Lement backhandledinterface ");
    else {this.mbackhandledinterface = (backhandledinterface) getactivity ();
    @Override public void OnStart () {Super.onstart ();
  Tell Fragmentactivity that the current fragment is on the top of the stack mbackhandledinterface.setselectedfragment (this); }
}

4. The main interface mainactivity to inherit fragmentactivity in order to invoke the Getsupportfragmentmanager () method to handle fragment. Mainactivity also need to rewrite the onbackpressed method to catch the return key (back key) event, as follows:

Package Com.example.testdemo;
Import Android.os.Bundle;
Import android.support.v4.app.FragmentActivity;
Import Android.support.v4.app.FragmentManager;
Import android.support.v4.app.FragmentTransaction;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button; public class Mainactivity extends Fragmentactivity implements Backhandledinterface {private static mainactivity MIn
  stance;
  Private Backhandledfragment mbackhandedfragment;
  Private Button Btnsecond;
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_main);
    Btnsecond = (Button) Findviewbyid (R.id.btnsecond); Btnsecond.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {Firstfrag
        ment-New firstfragment ();
        Loadfragment (a);
      Btnsecond.setvisibility (View.gone);
  }
    }); } public static mainactivity GetinStance () {if (minstance = = null) {minstance = new mainactivity ();
  return minstance;
    public void Loadfragment (Backhandledfragment fragment) {backhandledfragment second = fragment;
    Fragmentmanager fm = Getsupportfragmentmanager ();
    Fragmenttransaction ft = fm.begintransaction ();
    Ft.replace (R.id.firstfragment, Second, "other");
    Ft.addtobackstack ("tag");
  Ft.commit (); @Override public void Setselectedfragment (Backhandledfragment selectedfragment) {this.mbackhandedfragment = sel
  Ectedfragment;  @Override public void onbackpressed () {if (mbackhandedfragment = null | | |!mbackhandedfragment.onbackpressed ())
      {if (Getsupportfragmentmanager (). Getbackstackentrycount () = = 0) {super.onbackpressed (); } else {if (Getsupportfragmentmanager (). Getbackstackentrycount () = = 1) {btnsecond.setvisibility (view.v
        isible);
      } getsupportfragmentmanager (). Popbackstack ();
 }
    } }
}

 

5. Add two children Fragment,firstfragment.java and Secondfragment.java respectively, the code is as follows:

Firstfragment.java:

Package Com.example.testdemo;
Import Android.os.Bundle;
Import android.support.annotation.Nullable;
Import Android.support.v4.app.FragmentManager;
Import android.support.v4.app.FragmentTransaction;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.ViewGroup;
Import Android.widget.Button;
  public class Firstfragment extends Backhandledfragment {private View myview;
  Private Button Btnsecond; @Override public View Oncreateview (Layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle Savedi
    nstancestate) {MyView = inflater.inflate (R.layout.fragment_first, NULL);
    Initview ();
  return myview;
    private void Initview () {Btnsecond = (Button) Myview.findviewbyid (R.id.btnsecond); Btnsecond.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {Secondfra
        gment second = new Secondfragment (); Fragmentmanager fm = GETFRAgmentmanager ();
        Fragmenttransaction ft = fm.begintransaction ();
        Ft.replace (r.id.firstfragment, second);
        Ft.addtobackstack ("tag");
      Ft.commit ();
  }
    });
  @Override protected Boolean onbackpressed () {return false;

 }
}

Secondfragment.java:

Package Com.example.testdemo;
Import Android.os.Bundle;
Import android.support.annotation.Nullable;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
public class Secondfragment extends Backhandledfragment {
  private View mview;
  @Override public
  View Oncreateview (layoutinflater inflater,
      @Nullable viewgroup container, @Nullable Bundle Savedinstancestate) {
    Mview = inflater.inflate (R.layout.fragment_second, null);
    return mview;
  }
  @Override
  protected Boolean onbackpressed () {return
    false;
  }
}

6. Three layout file codes are as follows:

Activity_main.xml:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"
  xmlns:tools= "http:// Schemas.android.com/tools "
  android:layout_width=" match_parent "
  android:layout_height=" Match_parent "
  android:orientation= "vertical" >
  <textview
    android:layout_width= "Wrap_content"
    android: layout_height= "Wrap_content"
    android:layout_centerinparent= "true"
    android:text= "fragmentactivity Parent Interface"
    android:textsize= "26sp"/>
  <button
    android:id= "@+id/btnsecond"
    android:layout_width= "Wrap_content"
    android:layout_height= "wrap_content"
    android:layout_alignparentbottom= "true"
    android:text= "Jump to Firstfragment"/>
  <framelayout
    android:id= "@+id/firstfragment"
    android: Layout_width= "Match_parent"
    android:layout_height= "match_parent" >
  </FrameLayout>
</ Relativelayout>

Fragment_first.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://schemas.android.com/apk" /res/android "
  android:layout_width=" match_parent "
  android:layout_height=" Match_parent "
  android: Background= "#e5e5e5"
  android:orientation= "vertical" >
  <textview
    android:layout_width= "WRAP_" Content "
    android:layout_height=" wrap_content "
    android:layout_centerinparent=" true "
    android:text=" Firstfragment "
    android:textcolor=" #000000 "
    android:textsize=" 26sp "/>
  <button
    android : id= "@+id/btnsecond"
    android:layout_width= "wrap_content"
    android:layout_height= "Wrap_content"
    Android:layout_alignparentbottom= "true"
    android:text= "open secondfragment"/>
</RelativeLayout>

Fragment_second.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://schemas.android.com/apk" /res/android "
  android:layout_width=" match_parent "
  android:layout_height=" Match_parent "
  android: Background= "#e5e5e5"
  android:orientation= "vertical" >
  <textview
    android:layout_width= "WRAP_" Content "
    android:layout_height=" wrap_content "
    android:layout_centerinparent=" true "
    android:text=" Secondfragment "
    android:textcolor=" #000000 "
    android:textsize=" 26sp "/>
</relativelayout >

7. Finally, the example link:

Full instance code code click here to download the site.

I hope this article will help you with the Android program.

Related Article

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.