Android programming uses the fragment interface to jump down and return level by level

This paper describes the implementation method of Android programming using fragment interface to jump down and return level by level. Share with you for your reference, as follows:

1. First paste the project structure diagram:

2. First add an interface file backhandledinterface.java and define a setselectedfragment method to set the currently loaded fragment at the top of the stack. The mainactivity of the main interface must implement this interface. The code is as follows:

3. Define an abstract class backhandledfragment, which inherits from the fragment, and the fragment interface that jumps later should inherit from backhandledfragment. An onbackpressed method with a return value of boolean type is defined in the abstract class backhandledfragment, which is used to process the logic when clicking the return key (physical back key). If the method returns false, it means that the current fragment does not consume the return event, but the fragment activity to which the fragment belongs will process the event. The code is as follows:

4. The main interface mainactivity must inherit the fragmentactivity before calling getsupportfragmentmanager() method to process fragments. Mainactivity also needs to override the onbackpressed method to capture the back key event. The code is as follows:

5. Add two child fragments, firstfragment.java and secondfragment.java, respectively. The codes are as follows:

FirstFragment.java:

SecondFragment.java:

6. The codes of the three layout documents are as follows:

activity_ main.xml:

fragment_ first.xml:

fragment_ second.xml:

7. Finally, we offer the example link:

Click here to download the complete example code.

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>