├── .gitignore ├── CHANGELOG.md ├── CorePage.iml ├── README.md ├── bintray.gradle ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── cn │ │ └── edu │ │ └── zafu │ │ └── corepage │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── assets │ └── page.json.template │ ├── java │ └── cn │ │ └── edu │ │ └── zafu │ │ └── corepage │ │ ├── base │ │ ├── BaseActivity.java │ │ └── BaseFragment.java │ │ └── core │ │ ├── CoreAnim.java │ │ ├── CoreConfig.java │ │ ├── CorePage.java │ │ ├── CorePageManager.java │ │ ├── CoreSwitchBean.java │ │ └── CoreSwitcher.java │ └── res │ ├── anim │ ├── alpha_in.xml │ ├── alpha_out.xml │ ├── push_in_down.xml │ ├── push_no_ani.xml │ ├── push_out_down.xml │ ├── slide_in_left.xml │ ├── slide_in_right.xml │ ├── slide_out_left.xml │ ├── slide_out_right.xml │ ├── zoom_in.xml │ └── zoom_out.xml │ ├── layout │ └── activity_base.xml │ └── values │ ├── strings.xml │ └── themes.xml ├── sample ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── cn │ │ └── edu │ │ └── zafu │ │ └── corepage │ │ └── samplle │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── assets │ └── page.json │ ├── java │ └── cn │ │ └── edu │ │ └── zafu │ │ └── corepage │ │ └── sample │ │ ├── BaseApplication.java │ │ ├── MainActivity.java │ │ ├── MainFragment.java │ │ ├── TestFragment1.java │ │ ├── TestFragment2.java │ │ ├── TestFragment3.java │ │ └── TestFragment4.java │ └── res │ ├── layout │ ├── fragment_main.xml │ ├── fragment_test1.xml │ ├── fragment_test2.xml │ ├── fragment_test3.xml │ └── fragment_test4.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | .idea 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | /library/library.iml 10 | /sample/sample.iml 11 | /AllDemo.iml 12 | /sample/build 13 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | version 0.0.1 2 | ------------- 3 | 4 | - complete basic functions 5 | 6 | version 0.0.2 7 | ------------- 8 | 9 | - support custom application 10 | 11 | version 0.0.3 12 | ------------- 13 | 14 | - support [OpenAtlas](https://github.com/bunnyblue/OpenAtlas) 15 | 16 | version 0.0.4 17 | ------------- 18 | 19 | - fixed a bug caused by **CoreConfig.setIsOpenAtlas(boolean flag)** 20 | 21 | version 0.0.5 22 | ------------- 23 | 24 | - support custom page config 25 | - add a page switch animation called zoom -------------------------------------------------------------------------------- /CorePage.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Corepage is a page switch framework based on Fragment 2 | ==================================== 3 | 4 | [ ![Download](https://api.bintray.com/packages/lizhangqu/maven/corepage/images/download.svg) ](https://bintray.com/lizhangqu/maven/corepage/_latestVersion) 5 | A page switch framework based on Fragment, you can open a page use just a method.It can be used in both Activity andFragment if you make your custom Activity extends BaseActivity and your custom Fragment extends BaseFragment.And use BaseApplication as your Application. 6 | 7 | Changelog 8 | --------- 9 | 10 | Current version 0.0.5 released on 21th August 2015 11 | 12 | See details in [CHANGELOG](https://github.com/lizhangqu/CorePage/blob/master/CHANGELOG.md) 13 | 14 | 15 | Notices 16 | -------- 17 | - All your Activity must **extends BaseActivity**,All your Fragment must **extends BaseFragment**,use **package.YourApplication** as your Application name and use **CoreConfig.init()** in the override method **onCreate()** to init. 18 | - Your entry Activity doesn't need use setContentView method to set a view,all your views should be set in your Fragments. 19 | - Page jump based on method **openpage**,you can set an animation type ,weather add to back stack,open a new activity or not. 20 | - If you need the page return a result ,you should use **openPageForResult** to open page,then use **setFragmentResult** to set result and use **popToBack** to retuen. Override **onFragmentResult** method to get the return result. 21 | - All your fragment pages need configed in **assets/page.json**,**page name** and **class** is **required**,**page params** is **option**,the params will be used through **Bundle** 22 | - Full details and documentation can be found in the library project 23 | 24 | Examples 25 | -------- 26 | 27 | I have provided a sample . 28 | See samples [here on Github](https://github.com/lizhangqu/CorePage/tree/master/sample) 29 | To run Sample application, simply clone the repository and use android studio to compile, install it on connected device 30 | 31 | 32 | 33 | Usage 34 | ----- 35 | 36 | 37 | **Gradle** 38 | 39 | ``` 40 | dependencies { 41 | compile 'cn.edu.zafu:corepage:0.0.5' 42 | } 43 | ``` 44 | 45 | **config in assets/page.json** 46 | 47 | ``` 48 | [ 49 | { 50 | "name": "page1", 51 | "class": "cn.edu.zafu.corepage.sample.PageFragment1", 52 | "params": "" 53 | }, 54 | { 55 | "name": "page2", 56 | "class": "cn.edu.zafu.corepage.sample.PageFragment2", 57 | "params": { 58 | "key1":"value1", 59 | "key2":"value2" 60 | } 61 | } 62 | ] 63 | ``` 64 | 65 | **init config** 66 | ``` 67 | public class YourApplication extends Application { 68 | @Override 69 | public void onCreate() { 70 | super.onCreate(); 71 | CoreConfig.init(this); 72 | } 73 | } 74 | ``` 75 | 76 | ``` 77 | 83 | 84 | ``` 85 | 86 | 87 | **open a page** 88 | 89 | ``` 90 | openPage(pageName,bundle,coreAnim); 91 | // CoreAnim.slide, CoreAnim.fade, CoreAnim.present, CoreAnim.none 92 | openPage(pageName,bundle,coreAnim,isAddToBackStack); 93 | openPage(pageName,bundle,coreAnim,isAddToBackStack,isNewActivity); 94 | ``` 95 | 96 | **open a page for result** 97 | 98 | ``` 99 | //call this in the opener fragment 100 | openPageForResult(pageName,bundle,coreAnim,requestCode); 101 | 102 | //call this in the opened fragment to set result and return 103 | setFragmentResult(500,intent); 104 | popToBack(); 105 | 106 | //Override this method in the opener fragment to get the result 107 | public void onFragmentResult(int requestCode, int resultCode, Intent data) { 108 | super.onFragmentResult(requestCode, resultCode, data); 109 | } 110 | ``` 111 | 112 | **support OpenAtlas** 113 | 114 | if you need to support OpenAtlas,before you call the method such as **openPage**,you need to set a bundle classloader 115 | ``` 116 | ClassLoader bundleClassLoader = Atlas.getInstance().getBundleClassLoader(bundlepkgName); 117 | CoreConfig.setIsOpenAtlas(true); 118 | CoreConfig.setBundleClassLoader(bundleClassLoader); 119 | ``` 120 | 121 | 122 | **custom animation** 123 | 124 | if you need to support custom animation,you can use it this way 125 | ``` 126 | int anim[]= {R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right}; 127 | openPage("test1",null, anim); 128 | ``` 129 | 130 | **read extra page json** 131 | 132 | ``` 133 | private String pageJson="[" + 134 | " {" + 135 | " 'name': 'test4'," + 136 | " 'class': 'cn.edu.zafu.corepage.sample.TestFragment4'," + 137 | " 'params': ''" + 138 | " }]"; 139 | CoreConfig.readConfig(pageJson); 140 | ``` 141 | 142 | or init with the json,this will read both the **page.json** in directory assets and the String you given 143 | 144 | ``` 145 | private String pageJson="[" + 146 | " {" + 147 | " 'name': 'test4'," + 148 | " 'class': 'cn.edu.zafu.corepage.sample.TestFragment4'," + 149 | " 'params': ''" + 150 | " }]"; 151 | CoreConfig.init(this, pageJson); 152 | ``` 153 | 154 | 155 | ## License 156 | 157 | Copyright 2015 ZhangQu Li 158 | 159 | Licensed under the Apache License, Version 2.0 (the "License"); 160 | you may not use this file except in compliance with the License. 161 | You may obtain a copy of the License at 162 | 163 | http://www.apache.org/licenses/LICENSE-2.0 164 | 165 | Unless required by applicable law or agreed to in writing, software 166 | distributed under the License is distributed on an "AS IS" BASIS, 167 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 168 | See the License for the specific language governing permissions and 169 | limitations under the License. 170 | 171 | -------------------------------------------------------------------------------- /bintray.gradle: -------------------------------------------------------------------------------- 1 | group = PROJ_GROUP 2 | version = PROJ_VERSION 3 | project.archivesBaseName = PROJ_ARTIFACTID 4 | 5 | apply plugin: 'com.jfrog.bintray' 6 | apply plugin: 'maven-publish' 7 | 8 | task sourcesJar(type: Jar) { 9 | //archiveName = PROJ_NAME+"-"+PROJ_VERSION+"-source.jar" 10 | //设置生成名字 11 | from android.sourceSets.main.java.srcDirs 12 | classifier = 'sources' 13 | } 14 | 15 | task javadoc(type: Javadoc) { 16 | source = android.sourceSets.main.java.srcDirs 17 | classpath += configurations.compile 18 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 19 | } 20 | 21 | task javadocJar(type: Jar, dependsOn: javadoc) { 22 | classifier = 'javadoc' 23 | from javadoc.destinationDir 24 | } 25 | 26 | javadoc { 27 | options { 28 | encoding "UTF-8" 29 | charSet 'UTF-8' 30 | author true 31 | version true 32 | links "http://docs.oracle.com/javase/7/docs/api" 33 | title PROJ_ARTIFACTID 34 | } 35 | } 36 | 37 | artifacts { 38 | archives javadocJar 39 | archives sourcesJar 40 | } 41 | 42 | def pomConfig = { 43 | licenses { 44 | license { 45 | name "The Apache Software License, Version 2.0" 46 | url "http://www.apache.org/licenses/LICENSE-2.0.txt" 47 | distribution "repo" 48 | } 49 | } 50 | developers { 51 | developer { 52 | id DEVELOPER_ID 53 | name DEVELOPER_NAME 54 | email DEVELOPER_EMAIL 55 | } 56 | } 57 | 58 | //这里有引号,并且有个空格,否则生成不了依赖,主要是这里的dependencies并不是build.gradle里的dependencies,而gradle会把他当成自身的dependencies处理 59 | "dependencies " { 60 | dependency { 61 | groupId "com.alibaba" 62 | artifactId "fastjson" 63 | "version " "1.2.6" 64 | //同dependencies 65 | } 66 | } 67 | 68 | } 69 | 70 | publishing { 71 | publications { 72 | mavenJava(MavenPublication) { 73 | artifactId PROJ_ARTIFACTID 74 | 75 | pom { 76 | packaging 'aar' 77 | } 78 | pom.withXml { 79 | def root = asNode() 80 | root.appendNode('description', PROJ_DESCRIPTION) 81 | root.children().last() + pomConfig 82 | } 83 | } 84 | } 85 | } 86 | 87 | bintray { 88 | Properties properties = new Properties() 89 | properties.load(project.rootProject.file('local.properties').newDataInputStream()) 90 | 91 | user = properties.getProperty('BINTRAY_USER'); 92 | key = properties.getProperty('BINTRAY_KEY'); 93 | 94 | 95 | 96 | configurations = ['archives'] 97 | publications = ['mavenJava'] 98 | publish = true 99 | 100 | pkg { 101 | repo = 'maven' 102 | name = PROJ_NAME 103 | desc = PROJ_DESCRIPTION 104 | websiteUrl = PROJ_WEBSITEURL 105 | issueTrackerUrl = PROJ_ISSUETRACKERURL 106 | vcsUrl = PROJ_VCSURL 107 | licenses = ['Apache-2.0'] 108 | publicDownloadNumbers = true 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:1.2.3' 9 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0' 10 | classpath 'com.github.dcendents:android-maven-plugin:1.2' 11 | // NOTE: Do not place your application dependencies here; they belong 12 | // in the individual module build.gradle files 13 | } 14 | } 15 | 16 | allprojects { 17 | repositories { 18 | jcenter() 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | PROJ_GROUP=cn.edu.zafu 2 | PROJ_VERSION=0.0.5 3 | PROJ_NAME=corepage 4 | PROJ_WEBSITEURL=https://github.com/lizhangqu/CorePage 5 | PROJ_ISSUETRACKERURL=https://github.com/lizhangqu/CorePage/issues 6 | PROJ_VCSURL=https://github.com/lizhangqu/CorePage.git 7 | PROJ_DESCRIPTION=A page jump framework based fragment 8 | PROJ_ARTIFACTID=corepage 9 | 10 | DEVELOPER_ID=lizhangqu 11 | DEVELOPER_NAME=lizhangqu 12 | DEVELOPER_EMAIL=513163535@qq.com 13 | 14 | 15 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lizhangqu/CorePage/768c6279e326bad42dab0d315bab971c5baa0906/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Jul 20 09:51:43 CST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 22 5 | buildToolsVersion "22.0.1" 6 | defaultConfig { 7 | minSdkVersion 15 8 | targetSdkVersion 22 9 | } 10 | } 11 | 12 | dependencies { 13 | compile fileTree(dir: 'libs', include: ['*.jar']) 14 | compile 'com.android.support:appcompat-v7:22.2.0' 15 | compile 'com.alibaba:fastjson:1.2.6' 16 | } 17 | apply from: '../bintray.gradle' -------------------------------------------------------------------------------- /library/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\adt-bundle-windows-x64-20140702\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /library/src/androidTest/java/cn/edu/zafu/corepage/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package cn.edu.zafu.corepage; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 8 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /library/src/main/assets/page.json.template: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "name": "test1", 4 | "class": "com.kltz88.alldemo.TestFragment1", 5 | "params": { 6 | "param1": "value1", 7 | "param2": "value2" 8 | } 9 | }, 10 | { 11 | "name": "test2", 12 | "class": "com.kltz88.alldemo.TestFragment2", 13 | "params": "" 14 | } 15 | ] 16 | -------------------------------------------------------------------------------- /library/src/main/java/cn/edu/zafu/corepage/base/BaseActivity.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 ZhangQu Li 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.edu.zafu.corepage.base; 17 | 18 | import android.content.BroadcastReceiver; 19 | import android.content.Context; 20 | import android.content.Intent; 21 | import android.content.IntentFilter; 22 | import android.os.Bundle; 23 | import android.os.Handler; 24 | import android.os.Parcelable; 25 | import android.support.v4.app.Fragment; 26 | import android.support.v4.app.FragmentActivity; 27 | import android.support.v4.app.FragmentManager; 28 | import android.support.v4.app.FragmentTransaction; 29 | import android.util.Log; 30 | import android.view.KeyEvent; 31 | 32 | import java.io.Serializable; 33 | import java.lang.annotation.Annotation; 34 | import java.lang.annotation.ElementType; 35 | import java.lang.annotation.Retention; 36 | import java.lang.annotation.RetentionPolicy; 37 | import java.lang.annotation.Target; 38 | import java.lang.ref.WeakReference; 39 | import java.lang.reflect.Field; 40 | import java.util.ArrayList; 41 | import java.util.List; 42 | 43 | import cn.edu.zafu.corepage.R; 44 | import cn.edu.zafu.corepage.core.CoreAnim; 45 | import cn.edu.zafu.corepage.core.CoreConfig; 46 | import cn.edu.zafu.corepage.core.CorePageManager; 47 | import cn.edu.zafu.corepage.core.CoreSwitchBean; 48 | import cn.edu.zafu.corepage.core.CoreSwitcher; 49 | 50 | /** 51 | * 页面跳转都通过BaseActivity 嵌套Fragment来实现,动态替换fragment只需要指定相应的参数。 避免Activity 需要再manifest中注册的问题。 52 | * 1.管理应用中所有BaseActivity 实例。 2.管理BaseActivity 实例和fragment的跳转 53 | * User:lizhangqu(513163535@qq.com) 54 | * Date:2015-07-22 55 | * Time: 09:32 56 | */ 57 | public class BaseActivity extends FragmentActivity implements CoreSwitcher { 58 | private static final String TAG = BaseActivity.class.getSimpleName(); 59 | //日志TAG 60 | private static List> mActivities = new ArrayList>(); 61 | //应用中所有BaseActivity的引用 62 | protected CoreSwitchBean mFirstCoreSwitchBean; 63 | //记录首个CoreSwitchBean,用于页面切换 64 | private Handler mHandler = null; 65 | //主线程Handler 66 | private WeakReference mCurrentInstance = null; 67 | //当前activity的引用 68 | private BaseFragment mFragmentForResult = null; 69 | //forresult 的fragment 70 | private int mFragmentRequestCode = -1; 71 | //请求码,必须大于等于0 72 | /** 73 | * 仅用于接受应用退出广播,程序退出时有机会做一些必要的清理工作 74 | */ 75 | private BroadcastReceiver mExitReceiver = new BroadcastReceiver() { 76 | 77 | @Override 78 | public void onReceive(Context context, Intent intent) { 79 | String action = intent.getAction(); 80 | if (action.equals(CoreConfig.ACTION_EXIT_APP)) { 81 | Log.d(TAG, "exit from broadcast"); 82 | finish(); 83 | } 84 | } 85 | }; 86 | 87 | /** 88 | * 返回最上层的activity 89 | * 90 | * @return 栈顶Activity 91 | */ 92 | public static BaseActivity getTopActivity() { 93 | if (mActivities != null) { 94 | int size = mActivities.size(); 95 | if (size >= 1) { 96 | WeakReference ref = mActivities.get(size - 1); 97 | if (ref != null) { 98 | return ref.get(); 99 | } 100 | } 101 | } 102 | return null; 103 | } 104 | 105 | /** 106 | * 广播退出时清理activity列表 107 | */ 108 | public static void unInit() { 109 | if (mActivities != null) { 110 | mActivities.clear(); 111 | } 112 | } 113 | 114 | /** 115 | * 获得当前活动页面名 116 | * @return 当前页名 117 | */ 118 | protected String getPageName() { 119 | BaseFragment frg = getActiveFragment(); 120 | if (frg != null) { 121 | return frg.getPageName(); 122 | } 123 | return ""; 124 | } 125 | 126 | /** 127 | * 弹出页面 128 | */ 129 | @Override 130 | public void popPage() { 131 | popOrFinishActivity(); 132 | 133 | } 134 | 135 | /** 136 | * 保证在主线程操作 137 | */ 138 | private void popOrFinishActivity() { 139 | if (this.isFinishing()) { 140 | return; 141 | } 142 | if (this.getSupportFragmentManager().getBackStackEntryCount() > 1) { 143 | if (isMainThread()) { 144 | this.getSupportFragmentManager().popBackStackImmediate(); 145 | } else { 146 | this.mHandler.post(new Runnable() { 147 | @Override 148 | public void run() { 149 | getSupportFragmentManager().popBackStackImmediate(); 150 | } 151 | }); 152 | } 153 | } else { 154 | finishActivity(this, true); 155 | } 156 | 157 | } 158 | 159 | /** 160 | * 是否是主线程 161 | * @return 是否是主线程 162 | */ 163 | private boolean isMainThread() { 164 | return Thread.currentThread() == this.getMainLooper().getThread(); 165 | } 166 | 167 | /** 168 | * 是否位于栈顶 169 | * @param fragmentTag fragment的tag 170 | * @return 指定Fragment是否位于栈顶 171 | */ 172 | @Override 173 | public boolean isFragmentTop(String fragmentTag) { 174 | int size = mActivities.size(); 175 | if (size > 0) { 176 | WeakReference ref = mActivities.get(size - 1); 177 | BaseActivity item = ref.get(); 178 | if (item != null && item == this) { 179 | FragmentActivity activity = item; 180 | FragmentManager manager = activity.getSupportFragmentManager(); 181 | if (manager != null) { 182 | int count = manager.getBackStackEntryCount(); 183 | if (count >= 1) { 184 | FragmentManager.BackStackEntry entry = manager.getBackStackEntryAt(count - 1); 185 | if (entry.getName().equalsIgnoreCase(fragmentTag)) { 186 | return true; 187 | } 188 | } 189 | } 190 | } 191 | } 192 | return false; 193 | } 194 | 195 | /** 196 | * 查找fragment 197 | * @param pageName page的名字 198 | * @return 是否找到对应Fragment 199 | */ 200 | @Override 201 | public boolean findPage(String pageName) { 202 | int size = mActivities.size(); 203 | int j = size - 1; 204 | boolean hasFind = false; 205 | for (; j >= 0; j--) { 206 | WeakReference ref = mActivities.get(j); 207 | if (ref != null) { 208 | BaseActivity item = ref.get(); 209 | if (item == null) { 210 | Log.d(TAG, "item is null"); 211 | continue; 212 | } 213 | FragmentManager manager = item.getSupportFragmentManager(); 214 | int count = manager.getBackStackEntryCount(); 215 | for (int i = count - 1; i >= 0; i--) { 216 | String name = manager.getBackStackEntryAt(i).getName(); 217 | if (name.equalsIgnoreCase(pageName)) { 218 | hasFind = true; 219 | break; 220 | } 221 | } 222 | if (hasFind) { 223 | break; 224 | } 225 | } 226 | } 227 | return hasFind; 228 | } 229 | 230 | /** 231 | * 弹出并用bundle刷新数据,在onFragmentDataReset中回调 232 | * @param page page的名字 233 | * @return 跳转到对应的fragment的对象 234 | */ 235 | @Override 236 | public Fragment gotoPage(CoreSwitchBean page) { 237 | if (page == null) { 238 | Log.e(TAG, "page name empty"); 239 | return null; 240 | } 241 | String pageName = page.getPageName(); 242 | if (!findPage(pageName)) { 243 | Log.d(TAG, "Be sure you have the right pageName" + pageName); 244 | return this.openPage(page); 245 | } 246 | 247 | int size = mActivities.size(); 248 | int i = size - 1; 249 | for (; i >= 0; i--) { 250 | WeakReference ref = mActivities.get(i); 251 | if (ref != null) { 252 | BaseActivity item = ref.get(); 253 | if (item == null) { 254 | Log.d(TAG, "item null"); 255 | continue; 256 | } 257 | 258 | boolean findInActivity = popFragmentInActivity(pageName, page.getBundle(), item); 259 | if (findInActivity) { 260 | break; 261 | } else { 262 | item.finish(); 263 | // 找不到就弹出 264 | } 265 | } 266 | } 267 | return null; 268 | } 269 | 270 | /** 271 | * 当前activiti中弹fragment 272 | * @param pageName page的名字 273 | * @param bundle 传递的参数 274 | * @param findAcitivity 当前activity 275 | * @return 是否弹出成功 276 | */ 277 | protected boolean popFragmentInActivity(final String pageName, Bundle bundle, BaseActivity findAcitivity) { 278 | if (pageName == null || findAcitivity == null || findAcitivity.isFinishing()) { 279 | return false; 280 | } else { 281 | final FragmentManager fragmentManager = findAcitivity.getSupportFragmentManager(); 282 | if (fragmentManager != null) { 283 | Fragment frg = fragmentManager.findFragmentByTag(pageName); 284 | if (frg != null && frg instanceof BaseFragment) { 285 | if (fragmentManager.getBackStackEntryCount() > 1 && mHandler != null) { 286 | mHandler.postDelayed(new Runnable() { 287 | @Override 288 | public void run() { 289 | fragmentManager.popBackStack(pageName, 0); 290 | } 291 | }, 100); 292 | } 293 | ((BaseFragment) frg).onFragmentDataReset(bundle); 294 | return true; 295 | } 296 | } 297 | } 298 | return false; 299 | } 300 | 301 | /** 302 | * 根据Switchpage打开activity 303 | * @param page CoreSwitchBean对象 304 | */ 305 | public void startActivity(CoreSwitchBean page) { 306 | try { 307 | Intent intent = new Intent(this, BaseActivity.class); 308 | intent.putExtra("SwitchBean", page); 309 | 310 | this.startActivity(intent); 311 | int[] animations = page.getAnim(); 312 | if (animations != null && animations.length >= 2) { 313 | this.overridePendingTransition(animations[0], animations[1]); 314 | } 315 | } catch (Exception e) { 316 | e.printStackTrace(); 317 | Log.e(TAG, e.getMessage()); 318 | } 319 | } 320 | 321 | @Override 322 | public void startActivity(Intent intent) { 323 | try { 324 | super.startActivity(intent); 325 | } catch (Exception e) { 326 | Log.d(TAG, "startActivity" + e.getMessage()); 327 | } 328 | } 329 | 330 | /** 331 | * 根据SwitchBean打开fragment 332 | * @param page CoreSwitchBean对象 333 | * @return 打开的Fragment对象 334 | */ 335 | @Override 336 | public Fragment openPage(CoreSwitchBean page) { 337 | boolean addToBackStack = page.isAddToBackStack(); 338 | boolean newActivity = page.isNewActivity(); 339 | Bundle bundle = page.getBundle(); 340 | 341 | int[] animations = page.getAnim(); 342 | if (newActivity) { 343 | startActivity(page); 344 | return null; 345 | } else { 346 | String pageName = page.getPageName(); 347 | return CorePageManager.getInstance().openPageWithNewFragmentManager(getSupportFragmentManager(), pageName, bundle, animations, addToBackStack); 348 | } 349 | 350 | } 351 | 352 | /** 353 | * 移除无用fragment 354 | * @param fragmentLists 移除的fragment列表 355 | */ 356 | @Override 357 | public void removeUnlessFragment(List fragmentLists) { 358 | if (this.isFinishing()) { 359 | return; 360 | } 361 | FragmentManager manager = getSupportFragmentManager(); 362 | if (manager != null) { 363 | FragmentTransaction transaction = manager.beginTransaction(); 364 | for (String tag : fragmentLists) { 365 | Fragment fragment = manager.findFragmentByTag(tag); 366 | if (fragment != null) { 367 | transaction.remove(fragment); 368 | } 369 | } 370 | transaction.commitAllowingStateLoss(); 371 | int count = manager.getBackStackEntryCount(); 372 | if (count == 0) { 373 | this.finish(); 374 | } 375 | } 376 | } 377 | 378 | /** 379 | * 给BaseFragment调用 380 | * @param page CoreSwitchBean对象 381 | * @param fragment 要求返回结果的BaseFragment对象 382 | * @return 打开的fragment对象 383 | */ 384 | @Override 385 | public Fragment openPageForResult(CoreSwitchBean page, BaseFragment fragment) { 386 | if (page != null) { 387 | if (page.isNewActivity()) { 388 | Log.d(TAG, "openPageForResult start new activity-----" + fragment.getPageName()); 389 | mFragmentForResult = fragment; 390 | mFragmentRequestCode = page.getRequestCode(); 391 | startActivityForResult(page); 392 | return null; 393 | } else { 394 | String pageName = page.getPageName(); 395 | Bundle bundle = page.getBundle(); 396 | int[] animations = page.getAnim(); 397 | boolean addToBackStack = page.isAddToBackStack(); 398 | BaseFragment frg = (BaseFragment) CorePageManager.getInstance().openPageWithNewFragmentManager(getSupportFragmentManager(), pageName, bundle, animations, addToBackStack); 399 | if (frg == null) { 400 | return null; 401 | } 402 | final BaseFragment opener = fragment; 403 | frg.setRequestCode(page.getRequestCode()); 404 | frg.setFragmentFinishListener(new BaseFragment.OnFragmentFinishListener() { 405 | @Override 406 | public void onFragmentResult(int requestCode, int resultCode, Intent intent) { 407 | opener.onFragmentResult(requestCode, resultCode, intent); 408 | } 409 | }); 410 | return frg; 411 | } 412 | } else { 413 | Log.d(TAG, "openPageForResult.SwitchBean is null"); 414 | } 415 | return null; 416 | } 417 | 418 | /** 419 | * 420 | * @param page CoreSwitchBean对象 421 | */ 422 | public void startActivityForResult(CoreSwitchBean page) { 423 | try { 424 | Intent intent = new Intent(this, BaseActivity.class); 425 | intent.putExtra("SwitchBean", page); 426 | intent.putExtra("startActivityForResult", "true"); 427 | this.startActivityForResult(intent, page.getRequestCode()); 428 | 429 | int[] animations = page.getAnim(); 430 | if (animations != null && animations.length >= 2) { 431 | this.overridePendingTransition(animations[0], animations[1]); 432 | } 433 | } catch (Exception e) { 434 | e.printStackTrace(); 435 | } 436 | } 437 | 438 | /** 439 | * 打开fragment,并设置是否新开activity,设置是否添加到返回栈 440 | * 441 | * @param pageName 页面名 442 | * @param bundle 参数 443 | * @param coreAnim 动画 444 | * @param addToBackStack 返回栈 445 | * @param newActivity 新activity 446 | * @return 打开的fragment对象 447 | */ 448 | public Fragment openPage(String pageName, Bundle bundle, CoreAnim coreAnim, boolean addToBackStack, boolean newActivity) { 449 | CoreSwitchBean page = new CoreSwitchBean(pageName, bundle, coreAnim, addToBackStack, newActivity); 450 | return openPage(page); 451 | } 452 | 453 | /** 454 | * 打开fragment,并设置是否新开activity,设置是否添加到返回栈 455 | * 456 | * @param pageName 页面名 457 | * @param bundle 参数 458 | * @param anim 动画 459 | * @param addToBackStack 返回栈 460 | * @param newActivity 新activity 461 | * @return 打开的fragment对象 462 | */ 463 | public Fragment openPage(String pageName, Bundle bundle, int[] anim, boolean addToBackStack, boolean newActivity) { 464 | CoreSwitchBean page = new CoreSwitchBean(pageName, bundle, anim, addToBackStack, newActivity); 465 | return openPage(page); 466 | } 467 | 468 | /** 469 | * 打开fragment,并设置是否添加到返回栈 470 | * 471 | * @param pageName 页面名 472 | * @param bundle 参数 473 | * @param coreAnim 动画 474 | * @param addToBackStack 返回栈 475 | * @return 打开的fragment对象 476 | */ 477 | public Fragment openPage(String pageName, Bundle bundle, CoreAnim coreAnim, boolean addToBackStack) { 478 | CoreSwitchBean page = new CoreSwitchBean(pageName, bundle, coreAnim, addToBackStack); 479 | return openPage(page); 480 | } 481 | 482 | /** 483 | * 打开fragment,并设置是否添加到返回栈 484 | * 485 | * @param pageName 页面名 486 | * @param bundle 参数 487 | * @param anim 动画 488 | * @param addToBackStack 返回栈 489 | * @return 打开的fragment对象 490 | */ 491 | public Fragment openPage(String pageName, Bundle bundle, int[] anim, boolean addToBackStack) { 492 | CoreSwitchBean page = new CoreSwitchBean(pageName, bundle, anim, addToBackStack); 493 | return openPage(page); 494 | } 495 | 496 | /** 497 | * 打开fragment 498 | * 499 | * @param pageName 页面名 500 | * @param bundle 参数 501 | * @param coreAnim 动画 502 | * @return 打开的fragment对象 503 | */ 504 | public Fragment openPage(String pageName, Bundle bundle, CoreAnim coreAnim) { 505 | CoreSwitchBean page = new CoreSwitchBean(pageName, bundle, coreAnim); 506 | return openPage(page); 507 | } 508 | 509 | /** 510 | * 打开fragment 511 | * 512 | * @param pageName 页面名 513 | * @param bundle 参数 514 | * @param anim 动画 515 | * @return 打开的fragment对象 516 | */ 517 | public Fragment openPage(String pageName, Bundle bundle, int[] anim) { 518 | CoreSwitchBean page = new CoreSwitchBean(pageName, bundle, anim); 519 | return openPage(page); 520 | } 521 | 522 | /** 523 | * 如果是fragment发起的由fragment处理,否则默认处理 524 | * @param requestCode 请求码 525 | * @param resultCode 结果码 526 | * @param data 返回数据 527 | */ 528 | @Override 529 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 530 | Log.d(TAG, "onActivityResult from baseActivity" + requestCode + " " + resultCode); 531 | if (mFragmentRequestCode == requestCode && mFragmentForResult != null) { 532 | mFragmentForResult.onFragmentResult(mFragmentRequestCode, resultCode, data); 533 | 534 | } 535 | super.onActivityResult(requestCode, resultCode, data); 536 | } 537 | 538 | /** 539 | * 如果当前activity中只有一个activity,则关闭activity,否则父类处理 540 | */ 541 | @Override 542 | public void onBackPressed() { 543 | if (this.getSupportFragmentManager().getBackStackEntryCount() == 1) { 544 | this.finishActivity(this, true); 545 | 546 | } else { 547 | super.onBackPressed(); 548 | } 549 | } 550 | 551 | @Override 552 | public void onCreate(Bundle savedInstanceState) { 553 | super.onCreate(savedInstanceState); 554 | setContentView(R.layout.activity_base); 555 | Intent mNewIntent = getIntent(); 556 | //处理新开activity的情况 557 | if (null != savedInstanceState) { 558 | loadActivitySavedData(savedInstanceState); 559 | //恢复数据 560 | //需要用注解SaveWithActivity 561 | } 562 | mHandler = new Handler(getMainLooper()); 563 | //获得主线程handler 564 | mCurrentInstance = new WeakReference(this); 565 | //当前activity弱引用 566 | mActivities.add(mCurrentInstance); 567 | //当前activity增加到activity列表中 568 | printAllActivities(); 569 | //打印所有activity情况 570 | 571 | init(mNewIntent); 572 | //处理新开activity跳转 573 | IntentFilter filter = new IntentFilter(); 574 | filter.addAction(CoreConfig.ACTION_EXIT_APP); 575 | filter.addCategory(Intent.CATEGORY_DEFAULT); 576 | CoreConfig.getLocalBroadcastManager().registerReceiver(mExitReceiver, filter); 577 | //注册本地广播,接收程序退出广播 578 | } 579 | 580 | @Override 581 | protected void onDestroy() { 582 | super.onDestroy(); 583 | //解决内存泄露 584 | CoreConfig.getLocalBroadcastManager().unregisterReceiver(mExitReceiver); 585 | } 586 | 587 | /** 588 | * 如果fragment中处理了则fragment处理否则activity处理 589 | * @param keyCode keyCode码 590 | * @param event KeyEvent对象 591 | * @return 是否处理时间 592 | */ 593 | @Override 594 | public boolean onKeyDown(int keyCode, KeyEvent event) { 595 | BaseFragment activeFragment = getActiveFragment(); 596 | boolean isHanlde = false; 597 | if (activeFragment != null) { 598 | isHanlde = activeFragment.onKeyDown(keyCode, event); 599 | } 600 | if (!isHanlde) { 601 | return super.onKeyDown(keyCode, event); 602 | } else { 603 | return isHanlde; 604 | } 605 | } 606 | 607 | /** 608 | * 获得当前活动fragmnet 609 | * 610 | * @return 当前活动Fragment对象 611 | */ 612 | public BaseFragment getActiveFragment() { 613 | if (this.isFinishing()) { 614 | return null; 615 | } 616 | FragmentManager manager = this.getSupportFragmentManager(); 617 | if (manager != null) { 618 | int count = manager.getBackStackEntryCount(); 619 | if (count > 0) { 620 | String tag = manager.getBackStackEntryAt(count - 1).getName(); 621 | return (BaseFragment) manager.findFragmentByTag(tag); 622 | } 623 | } 624 | return null; 625 | } 626 | 627 | /** 628 | * 保存数据 629 | * 630 | * @param outState Bundle对象 631 | */ 632 | @Override 633 | protected void onSaveInstanceState(Bundle outState) { 634 | Field[] fields = this.getClass().getDeclaredFields(); 635 | Field.setAccessible(fields, true); 636 | Annotation[] ans; 637 | for (Field f : fields) { 638 | ans = f.getDeclaredAnnotations(); 639 | for (Annotation an : ans) { 640 | if (an instanceof SaveWithActivity) { 641 | try { 642 | Object o = f.get(this); 643 | if (o == null) { 644 | continue; 645 | } 646 | String fieldName = f.getName(); 647 | if (o instanceof Integer) { 648 | outState.putInt(fieldName, f.getInt(this)); 649 | } else if (o instanceof String) { 650 | outState.putString(fieldName, (String) f.get(this)); 651 | } else if (o instanceof Long) { 652 | outState.putLong(fieldName, f.getLong(this)); 653 | } else if (o instanceof Short) { 654 | outState.putShort(fieldName, f.getShort(this)); 655 | } else if (o instanceof Boolean) { 656 | outState.putBoolean(fieldName, f.getBoolean(this)); 657 | } else if (o instanceof Byte) { 658 | outState.putByte(fieldName, f.getByte(this)); 659 | } else if (o instanceof Character) { 660 | outState.putChar(fieldName, f.getChar(this)); 661 | } else if (o instanceof CharSequence) { 662 | outState.putCharSequence(fieldName, (CharSequence) f.get(this)); 663 | } else if (o instanceof Float) { 664 | outState.putFloat(fieldName, f.getFloat(this)); 665 | } else if (o instanceof Double) { 666 | outState.putDouble(fieldName, f.getDouble(this)); 667 | } else if (o instanceof String[]) { 668 | outState.putStringArray(fieldName, (String[]) f.get(this)); 669 | } else if (o instanceof Parcelable) { 670 | outState.putParcelable(fieldName, (Parcelable) f.get(this)); 671 | } else if (o instanceof Serializable) { 672 | outState.putSerializable(fieldName, (Serializable) f.get(this)); 673 | } else if (o instanceof Bundle) { 674 | outState.putBundle(fieldName, (Bundle) f.get(this)); 675 | } 676 | } catch (IllegalArgumentException e) { 677 | e.printStackTrace(); 678 | } catch (IllegalAccessException e) { 679 | e.printStackTrace(); 680 | } 681 | } 682 | } 683 | } 684 | 685 | super.onSaveInstanceState(outState); 686 | } 687 | 688 | @Override 689 | public void startActivityForResult(Intent intent, int requestCode) { 690 | try { 691 | super.startActivityForResult(intent, requestCode); 692 | } catch (Exception e) { 693 | Log.d(TAG, "startActivityForResult" + e.getMessage()); 694 | } 695 | } 696 | 697 | /** 698 | * 恢复数据 699 | * 700 | * @param savedInstanceState Bundle对象 701 | */ 702 | private void loadActivitySavedData(Bundle savedInstanceState) { 703 | Field[] fields = this.getClass().getDeclaredFields(); 704 | Field.setAccessible(fields, true); 705 | Annotation[] ans; 706 | for (Field f : fields) { 707 | ans = f.getDeclaredAnnotations(); 708 | for (Annotation an : ans) { 709 | if (an instanceof SaveWithActivity) { 710 | try { 711 | String fieldName = f.getName(); 712 | @SuppressWarnings("rawtypes") 713 | Class cls = f.getType(); 714 | if (cls == int.class || cls == Integer.class) { 715 | f.setInt(this, savedInstanceState.getInt(fieldName)); 716 | } else if (String.class.isAssignableFrom(cls)) { 717 | f.set(this, savedInstanceState.getString(fieldName)); 718 | } else if (Serializable.class.isAssignableFrom(cls)) { 719 | f.set(this, savedInstanceState.getSerializable(fieldName)); 720 | } else if (cls == long.class || cls == Long.class) { 721 | f.setLong(this, savedInstanceState.getLong(fieldName)); 722 | } else if (cls == short.class || cls == Short.class) { 723 | f.setShort(this, savedInstanceState.getShort(fieldName)); 724 | } else if (cls == boolean.class || cls == Boolean.class) { 725 | f.setBoolean(this, savedInstanceState.getBoolean(fieldName)); 726 | } else if (cls == byte.class || cls == Byte.class) { 727 | f.setByte(this, savedInstanceState.getByte(fieldName)); 728 | } else if (cls == char.class || cls == Character.class) { 729 | f.setChar(this, savedInstanceState.getChar(fieldName)); 730 | } else if (CharSequence.class.isAssignableFrom(cls)) { 731 | f.set(this, savedInstanceState.getCharSequence(fieldName)); 732 | } else if (cls == float.class || cls == Float.class) { 733 | f.setFloat(this, savedInstanceState.getFloat(fieldName)); 734 | } else if (cls == double.class || cls == Double.class) { 735 | f.setDouble(this, savedInstanceState.getDouble(fieldName)); 736 | } else if (String[].class.isAssignableFrom(cls)) { 737 | f.set(this, savedInstanceState.getStringArray(fieldName)); 738 | } else if (Parcelable.class.isAssignableFrom(cls)) { 739 | f.set(this, savedInstanceState.getParcelable(fieldName)); 740 | } else if (Bundle.class.isAssignableFrom(cls)) { 741 | f.set(this, savedInstanceState.getBundle(fieldName)); 742 | } 743 | } catch (IllegalArgumentException e) { 744 | e.printStackTrace(); 745 | } catch (IllegalAccessException e) { 746 | e.printStackTrace(); 747 | } 748 | } 749 | } 750 | } 751 | } 752 | 753 | /** 754 | * 打印,调试用 755 | */ 756 | private void printAllActivities() { 757 | Log.d(TAG, "------------BaseActivity print all------------activities size:" + mActivities.size()); 758 | for (WeakReference ref : mActivities) { 759 | if (ref != null) { 760 | BaseActivity item = ref.get(); 761 | if (item != null) { 762 | Log.d(TAG, item.toString()); 763 | } 764 | } 765 | } 766 | } 767 | 768 | /** 769 | * 初始化intent 770 | * 771 | * @param mNewIntent Intent对象 772 | */ 773 | private void init(Intent mNewIntent) { 774 | try { 775 | CoreSwitchBean page = mNewIntent.getParcelableExtra("SwitchBean"); 776 | String startActivityForResult = mNewIntent.getStringExtra("startActivityForResult"); 777 | this.mFirstCoreSwitchBean = page; 778 | if (page != null) { 779 | BaseFragment fragment = null; 780 | boolean addToBackStack = page.isAddToBackStack(); 781 | String pageName = page.getPageName(); 782 | Bundle bundle = page.getBundle(); 783 | fragment = (BaseFragment) CorePageManager.getInstance().openPageWithNewFragmentManager(getSupportFragmentManager(), pageName, bundle, null, addToBackStack); 784 | if (fragment != null) { 785 | if ("true".equalsIgnoreCase(startActivityForResult)) { 786 | fragment.setRequestCode(page.getRequestCode()); 787 | fragment.setFragmentFinishListener(new BaseFragment.OnFragmentFinishListener() { 788 | @Override 789 | public void onFragmentResult(int requestCode, int resultCode, Intent intent) { 790 | BaseActivity.this.setResult(resultCode, intent); 791 | } 792 | }); 793 | } 794 | } else { 795 | this.finish(); 796 | } 797 | } 798 | } catch (Exception e) { 799 | e.printStackTrace(); 800 | Log.d(TAG, e.getMessage()); 801 | this.finish(); 802 | } 803 | } 804 | 805 | /** 806 | * 结束activity,设置是否显示动画 807 | * 808 | * @param activity BaseActivity对象 809 | * @param showAnimation 是否显示动画 810 | */ 811 | private void finishActivity(BaseActivity activity, boolean showAnimation) { 812 | if (activity != null) { 813 | activity.finish(); 814 | mActivities.remove(mCurrentInstance); 815 | //从activity列表中移除当前实例 816 | } 817 | if (showAnimation) { 818 | //动画 819 | int[] animations = null; 820 | if (activity.mFirstCoreSwitchBean != null && activity.mFirstCoreSwitchBean.getAnim() != null) { 821 | animations = activity.mFirstCoreSwitchBean.getAnim(); 822 | } 823 | if (animations != null && animations.length >= 4) { 824 | overridePendingTransition(animations[2], animations[3]); 825 | } 826 | } 827 | } 828 | 829 | /** 830 | * 注解了该注解数据会被保存 831 | */ 832 | @Retention(RetentionPolicy.RUNTIME) 833 | @Target(ElementType.FIELD) 834 | public @interface SaveWithActivity { 835 | } 836 | } 837 | -------------------------------------------------------------------------------- /library/src/main/java/cn/edu/zafu/corepage/base/BaseFragment.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 ZhangQu Li 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.edu.zafu.corepage.base; 17 | 18 | import android.app.Activity; 19 | import android.content.Intent; 20 | import android.os.Bundle; 21 | import android.support.v4.app.Fragment; 22 | import android.util.Log; 23 | import android.view.KeyEvent; 24 | 25 | import cn.edu.zafu.corepage.core.CoreSwitchBean; 26 | import cn.edu.zafu.corepage.core.CoreSwitcher; 27 | import cn.edu.zafu.corepage.core.CoreAnim; 28 | 29 | /** 30 | * 全局基类BaseFragment 31 | * User:lizhangqu(513163535@qq.com) 32 | * Date:2015-07-22 33 | * Time: 09:33 34 | */ 35 | public class BaseFragment extends Fragment { 36 | private static final String TAG = BaseFragment.class.getSimpleName(); 37 | //日志tag 38 | protected Activity mActivity; 39 | //所在activity 40 | private String mPageName; 41 | //页面名 42 | private int mRequestCode; 43 | //用于startForResult的requestCode 44 | private CoreSwitcher mPageCoreSwitcher; 45 | //openPageForResult接口,用于传递返回结果 46 | private OnFragmentFinishListener mFragmentFinishListener; 47 | 48 | /** 49 | * 设置该接口用于返回结果 50 | * @param listener OnFragmentFinishListener对象 51 | */ 52 | public void setFragmentFinishListener(OnFragmentFinishListener listener) { 53 | this.mFragmentFinishListener = listener; 54 | } 55 | 56 | /** 57 | * 设置openPageForResult打开的页面的返回结果 58 | * @param resultCode 返回结果码 59 | * @param intent 返回的intent对象 60 | */ 61 | public void setFragmentResult(int resultCode, Intent intent) { 62 | if (mFragmentFinishListener != null) { 63 | mFragmentFinishListener.onFragmentResult(mRequestCode, resultCode, intent); 64 | } 65 | } 66 | 67 | /** 68 | * 得到requestCode 69 | * @return 请求码 70 | */ 71 | public int getRequestCode() { 72 | return this.mRequestCode; 73 | } 74 | 75 | /** 76 | * 设置requestCode 77 | * @param code 请求码 78 | */ 79 | public void setRequestCode(int code) { 80 | this.mRequestCode = code; 81 | } 82 | 83 | /** 84 | * 将Activity中onKeyDown在Fragment中实现, 85 | * @param keyCode keyCode码 86 | * @param event KeyEvent对象 87 | * @return 是否处理 88 | */ 89 | public boolean onKeyDown(int keyCode, KeyEvent event) { 90 | return false; 91 | } 92 | 93 | /** 94 | * 数据设置,回调 95 | * @param bundle 刷新数据的Bundle对象 96 | */ 97 | public void onFragmentDataReset(Bundle bundle) { 98 | 99 | } 100 | 101 | 102 | 103 | public interface PopCallback{ 104 | void run(); 105 | } 106 | /** 107 | * 用于openPageForResult,获得返回内容后需要再次调openPage的场景,只适合不新开Activity的情况,如果新开activity请像Activity返回结果那样操作 108 | * @param callback 返回码 109 | */ 110 | public void popToBackForResult(PopCallback callback) { 111 | this.popToBack(null, null); 112 | callback.run(); 113 | } 114 | /** 115 | * 弹出栈顶的Fragment。如果Activity中只有一个Fragemnt时,Acitivity也退出。 116 | */ 117 | public void popToBack() { 118 | this.popToBack(null, null); 119 | } 120 | 121 | /** 122 | * 如果在fragment栈中找到,则跳转到该fragment中去,否则弹出栈顶 123 | * @param pageName 页面名 124 | * @param bundle 参数 125 | */ 126 | public final void popToBack(String pageName, Bundle bundle) { 127 | CoreSwitcher coreSwitcher = getSwitcher(); 128 | if (coreSwitcher != null) { 129 | if (pageName == null) { 130 | coreSwitcher.popPage(); 131 | } else { 132 | if (this.findPage(pageName)) { 133 | CoreSwitchBean page = new CoreSwitchBean(pageName, bundle); 134 | coreSwitcher.gotoPage(page); 135 | } else { 136 | coreSwitcher.popPage(); 137 | } 138 | } 139 | } else { 140 | Log.d(TAG, "pageSwitcher null"); 141 | } 142 | } 143 | 144 | /** 145 | * 得到页面切换Switcher 146 | * @return 页面切换Switcher 147 | */ 148 | public CoreSwitcher getSwitcher() { 149 | synchronized (BaseFragment.this) {// 加强保护,保证pageSwitcher 不为null 150 | if (mPageCoreSwitcher == null) { 151 | if (this.mActivity != null && this.mActivity instanceof CoreSwitcher) { 152 | mPageCoreSwitcher = (CoreSwitcher) this.mActivity; 153 | } 154 | if (mPageCoreSwitcher == null) { 155 | BaseActivity topActivity = BaseActivity.getTopActivity(); 156 | if (topActivity != null && topActivity instanceof CoreSwitcher) { 157 | mPageCoreSwitcher = (CoreSwitcher) topActivity; 158 | } 159 | } 160 | } 161 | } 162 | return mPageCoreSwitcher; 163 | } 164 | 165 | /** 166 | * 设置Switcher 167 | * @param pageCoreSwitcher CoreSwitcher对象 168 | */ 169 | public void setSwitcher(CoreSwitcher pageCoreSwitcher) { 170 | this.mPageCoreSwitcher = pageCoreSwitcher; 171 | } 172 | 173 | /** 174 | * 查找fragment是否存在,通过Switcher查找 175 | * @param pageName 页面名 176 | * @return 是否找到 177 | */ 178 | public boolean findPage(String pageName) { 179 | if (pageName == null) { 180 | Log.d(TAG, "pageName is null"); 181 | return false; 182 | } 183 | CoreSwitcher coreSwitcher = getSwitcher(); 184 | if (coreSwitcher != null) { 185 | return coreSwitcher.findPage(pageName); 186 | } else { 187 | Log.d(TAG, "pageSwitch is null"); 188 | return false; 189 | } 190 | 191 | } 192 | 193 | /** 194 | * 对应fragment是否位于栈顶,通过Switcher查找 195 | * @param fragmentTag fragment的tag 196 | * @return 是否位于栈顶 197 | */ 198 | public boolean isFragmentTop(String fragmentTag) { 199 | CoreSwitcher pageCoreSwitcher = this.getSwitcher(); 200 | if (pageCoreSwitcher != null) { 201 | return pageCoreSwitcher.isFragmentTop(fragmentTag); 202 | 203 | } else { 204 | Log.d(TAG, "pageSwitcher is null"); 205 | return false; 206 | } 207 | } 208 | 209 | /** 210 | * 重新该方法用于获得返回的数据 211 | * @param requestCode 请求码 212 | * @param resultCode 返回结果码 213 | * @param data 返回数据 214 | */ 215 | public void onFragmentResult(int requestCode, int resultCode, Intent data) { 216 | Log.d(TAG, "onFragmentResult from baseFragment:requestCode-" + requestCode + " resultCode-" + resultCode); 217 | } 218 | 219 | /** 220 | * 在当前activity中打开一个fragment,并添加到返回栈中 221 | * @param pageName Fragemnt 名,在page.json中配置。 222 | * @param bundle 页面跳转时传递的参数 223 | * @param coreAnim 指定的动画理性 none/slide(左右平移)/present(由下向上)/fade(fade 动画) 224 | * @return 打开的fragment对象 225 | */ 226 | public final Fragment openPage(String pageName, Bundle bundle, CoreAnim coreAnim) { 227 | return this.openPage(pageName, bundle, CoreSwitchBean.convertAnimations(coreAnim), true); 228 | } 229 | 230 | /** 231 | * 在当前activity中打开一个fragment,并设置是否添加到返回栈 232 | * @param pageName Fragemnt 名,在page.json中配置。 233 | * @param bundle 页面跳转时传递的参数 234 | * @param anim 指定的动画理性 none/slide(左右平移)/present(由下向上)/fade(fade 动画) 235 | * @param addToBackStack 是否添加到用户操作栈中 236 | * @return 打开的fragment对象 237 | */ 238 | public final Fragment openPage(String pageName, Bundle bundle, int[] anim, boolean addToBackStack) { 239 | return this.openPage(pageName, bundle, anim, addToBackStack, false); 240 | } 241 | 242 | /** 243 | * 打开一个fragment并设置是否新开activity,设置是否添加返回栈 244 | * @param pageName Fragemnt 名,在page.json中配置。 245 | * @param bundle 页面跳转时传递的参数 246 | * @param anim 指定的动画理性 none/slide(左右平移)/present(由下向上)/fade(fade 动画) 247 | * @param addToBackStack 是否添加到用户操作栈中 248 | * @param newActivity 该页面是否新建一个Activity 249 | * @return 打开的fragment对象 250 | */ 251 | public final Fragment openPage(String pageName, Bundle bundle, int[] anim, boolean addToBackStack, boolean newActivity) { 252 | if (pageName == null) { 253 | Log.d(TAG, "pageName is null"); 254 | return null; 255 | } 256 | CoreSwitcher coreSwitcher = this.getSwitcher(); 257 | if (coreSwitcher != null) { 258 | CoreSwitchBean page = new CoreSwitchBean(pageName, bundle, anim, addToBackStack, newActivity); 259 | return coreSwitcher.openPage(page); 260 | } else { 261 | Log.d(TAG, "pageSwitcher is null"); 262 | return null; 263 | } 264 | } 265 | 266 | /** 267 | * 在当前activity中打开一个fragment,并添加到返回栈中 268 | * 269 | * @param pageName Fragemnt 名,在page.json中配置。 270 | * @param bundle 页面跳转时传递的参数 271 | * @param anim 指定的动画理性 none/slide(左右平移)/present(由下向上)/fade(fade 动画) 272 | * @return 打开的fragment对象 273 | */ 274 | public final Fragment openPage(String pageName, Bundle bundle, int[] anim) { 275 | return this.openPage(pageName, bundle, anim, true); 276 | } 277 | 278 | /** 279 | * 在当前activity中打开一个fragment,并设置是否添加到返回栈 280 | * 281 | * @param pageName Fragemnt 名,在page.json中配置。 282 | * @param bundle 页面跳转时传递的参数 283 | * @param coreAnim 指定的动画理性 none/slide(左右平移)/present(由下向上)/fade(fade 动画) 284 | * @param addToBackStack 是否添加到用户操作栈中 285 | * @return 打开的fragment对象 286 | */ 287 | public final Fragment openPage(String pageName, Bundle bundle, CoreAnim coreAnim, boolean addToBackStack) { 288 | return this.openPage(pageName, bundle, CoreSwitchBean.convertAnimations(coreAnim), addToBackStack, false); 289 | } 290 | 291 | /** 292 | * 打开一个fragment并设置是否新开activity,设置是否添加返回栈 293 | * @param pageName Fragemnt 名,在page.json中配置。 294 | * @param bundle 页面跳转时传递的参数 295 | * @param coreAnim 指定的动画理性 none/slide(左右平移)/present(由下向上)/fade(fade 动画) 296 | * @param addToBackStack 是否添加到用户操作栈中 297 | * @param newActivity 该页面是否新建一个Activity 298 | * @return 打开的fragment对象 299 | */ 300 | public final Fragment openPage(String pageName, Bundle bundle, CoreAnim coreAnim, boolean addToBackStack, boolean newActivity) { 301 | return this.openPage(pageName, bundle, CoreSwitchBean.convertAnimations(coreAnim), addToBackStack, newActivity); 302 | } 303 | 304 | /** 305 | * @param pageName 页面名 306 | * @param bundle 参数 307 | * @param coreAnim 动画 308 | * @return 打开的fragment对象 309 | */ 310 | public Fragment gotoPage(String pageName, Bundle bundle, CoreAnim coreAnim) { 311 | return this.gotoPage(pageName, bundle, coreAnim,false); 312 | 313 | } 314 | 315 | /** 316 | * 新建或跳转到一个页面(Fragment)。找不到pageName Fragment时,就新建Fragment。找到pageName 317 | * Fragment时,则弹出该Fragement到栈顶上的所有actvity和fragment 318 | * 319 | * @param pageName Fragemnt 名,在在page.json中配置。 320 | * @param bundle 页面跳转时传递的参数 321 | * @param coreAnim 指定的动画理性 none/slide(左右平移)/present(由下向上)/fade(fade 动画) 322 | * @param newActivity 该页面是否新建一个Activity 323 | * @return 打开的fragment对象 324 | */ 325 | public Fragment gotoPage(String pageName, Bundle bundle, CoreAnim coreAnim, boolean newActivity) { 326 | CoreSwitcher pageCoreSwitcher = this.getSwitcher(); 327 | if (pageCoreSwitcher != null) { 328 | CoreSwitchBean page = new CoreSwitchBean(pageName, bundle, coreAnim, true, newActivity); 329 | return pageCoreSwitcher.gotoPage(page); 330 | } else { 331 | 332 | Log.d(TAG, "pageSwitcher is null"); 333 | return null; 334 | } 335 | } 336 | 337 | /** 338 | * 打开fragment并请求获得返回值 339 | * @param pageName 页面名 340 | * @param bundle 参数 341 | * @param coreAnim 动画 342 | * @param requestCode 请求码 343 | * @return 打开的fragment对象 344 | */ 345 | public final Fragment openPageForResult(String pageName, Bundle bundle, CoreAnim coreAnim, int requestCode) { 346 | return this.openPageForResult(false, pageName, bundle, coreAnim, requestCode); 347 | } 348 | 349 | /** 350 | * 打开fragment并请求获得返回值,并设置是否在新activity中打开 351 | * @param newActivity 是否新开activity 352 | * @param pageName 页面名 353 | * @param bundle 参数 354 | * @param coreAnim 动画 355 | * @param requestCode 请求码 356 | * @return 打开的fragment对象 357 | */ 358 | public final Fragment openPageForResult(boolean newActivity, String pageName, Bundle bundle, CoreAnim coreAnim, int requestCode) { 359 | 360 | CoreSwitcher pageCoreSwitcher = this.getSwitcher(); 361 | if (pageCoreSwitcher != null) { 362 | CoreSwitchBean page = new CoreSwitchBean(pageName, bundle, coreAnim, true, newActivity); 363 | page.setRequestCode(requestCode); 364 | 365 | return pageCoreSwitcher.openPageForResult(page, this); 366 | } else { 367 | Log.d(TAG, "pageSwitcher is null"); 368 | return null; 369 | } 370 | } 371 | 372 | @Override 373 | public void onAttach(Activity activity) { 374 | super.onAttach(activity); 375 | mActivity = activity; 376 | } 377 | 378 | 379 | @Override 380 | public void onCreate(Bundle savedInstanceState) { 381 | super.onCreate(savedInstanceState); 382 | if (getPageName() != null) { 383 | Log.d(TAG, "====Fragment.onCreate====" + getPageName()); 384 | } 385 | } 386 | 387 | /** 388 | * 获得页面名 389 | * @return 页面名 390 | */ 391 | public String getPageName() { 392 | return mPageName; 393 | } 394 | 395 | /** 396 | * 设置页面名 397 | * @param pageName 页面名 398 | */ 399 | public void setPageName(String pageName) { 400 | mPageName = pageName; 401 | } 402 | 403 | @Override 404 | public void onDetach() { 405 | super.onDetach(); 406 | mActivity = null; 407 | } 408 | 409 | /** 410 | * 页面跳转接口 411 | */ 412 | public interface OnFragmentFinishListener { 413 | void onFragmentResult(int requestCode, int resultCode, Intent intent); 414 | } 415 | 416 | 417 | } 418 | -------------------------------------------------------------------------------- /library/src/main/java/cn/edu/zafu/corepage/core/CoreAnim.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 ZhangQu Li 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.edu.zafu.corepage.core; 17 | 18 | /** 19 | * 页面切换动画类别 20 | * User:lizhangqu(513163535@qq.com) 21 | * Date:2015-07-22 22 | * Time: 09:42 23 | */ 24 | public enum CoreAnim { 25 | none, /* 没有动画 */ 26 | present, /*由下到上动画 */ 27 | slide,/* 从左到右动画 */ 28 | fade,/*渐变 */ 29 | zoom;/*放大 */ 30 | } 31 | -------------------------------------------------------------------------------- /library/src/main/java/cn/edu/zafu/corepage/core/CoreConfig.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 ZhangQu Li 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.edu.zafu.corepage.core; 17 | 18 | import android.content.Context; 19 | import android.content.Intent; 20 | import android.support.v4.content.LocalBroadcastManager; 21 | 22 | import cn.edu.zafu.corepage.base.BaseActivity; 23 | 24 | /** 25 | * 全局配置类 26 | * User:lizhangqu(513163535@qq.com) 27 | * Date:2015-07-22 28 | * Time: 12:24 29 | */ 30 | public class CoreConfig { 31 | /** 32 | * Atlas支持 start 33 | */ 34 | private static boolean isOpenAtlas=false; 35 | private static ClassLoader mBundleClassLoader =null; 36 | 37 | public static boolean isOpenAtlas() { 38 | return isOpenAtlas; 39 | } 40 | 41 | public static void setIsOpenAtlas(boolean isOpenAtlasFlag) { 42 | isOpenAtlas = isOpenAtlasFlag; 43 | } 44 | 45 | public static ClassLoader getBundleClassLoader() { 46 | return mBundleClassLoader; 47 | } 48 | 49 | public static void setBundleClassLoader(ClassLoader classLoader) { 50 | mBundleClassLoader = classLoader; 51 | } 52 | /** 53 | * Atlas支持 end 54 | */ 55 | public final static String ACTION_EXIT_APP = "cn.edu.zafu.library.exit"; 56 | //本地广播退出 57 | 58 | private static LocalBroadcastManager mLocalBroadcatManager; 59 | private static Context mContext; 60 | private boolean hasInited=false; 61 | 62 | /** 63 | * 默认初始化,配置文件在assets/page.json 64 | * @param context 上下文 65 | */ 66 | public static void init(Context context){ 67 | mContext=context.getApplicationContext(); 68 | CorePageManager.getInstance().init(mContext); 69 | } 70 | 71 | /** 72 | * 自定义初始化,配置文件信息由外部传入。 73 | * @param context 上下文 74 | * @param pageJson 配置的json 75 | */ 76 | public static void init(Context context,String pageJson){ 77 | mContext=context.getApplicationContext(); 78 | CorePageManager.getInstance().init(mContext,pageJson); 79 | } 80 | public static void unInit(){ 81 | Intent intent = new Intent(); 82 | intent.setAction(CoreConfig.ACTION_EXIT_APP); 83 | intent.addCategory(Intent.CATEGORY_DEFAULT); 84 | getLocalBroadcastManager().sendBroadcast(intent); 85 | BaseActivity.unInit(); 86 | mLocalBroadcatManager=null; 87 | } 88 | public static void readConfig(String pageJson){ 89 | CorePageManager.getInstance().readConfig(pageJson); 90 | } 91 | /** 92 | * 发送本地广播退出程序 93 | */ 94 | public void exitApp() { 95 | Intent intent = new Intent(); 96 | intent.setAction(CoreConfig.ACTION_EXIT_APP); 97 | intent.addCategory(Intent.CATEGORY_DEFAULT); 98 | getLocalBroadcastManager().sendBroadcast(intent); 99 | BaseActivity.unInit(); 100 | } 101 | /** 102 | * 获得LocalBroadcastManager对象 103 | * @return LocalBroadcastManager对象 104 | */ 105 | public static LocalBroadcastManager getLocalBroadcastManager() { 106 | if (mLocalBroadcatManager == null) { 107 | mLocalBroadcatManager = LocalBroadcastManager.getInstance(mContext); 108 | } 109 | return mLocalBroadcatManager; 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /library/src/main/java/cn/edu/zafu/corepage/core/CorePage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 ZhangQu Li 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.edu.zafu.corepage.core; 17 | 18 | import java.io.Serializable; 19 | 20 | /** 21 | * assets/page.json 页面属性类 22 | * User:lizhangqu(513163535@qq.com) 23 | * Date:2015-07-22 24 | * Time: 09:34 25 | */ 26 | public class CorePage implements Serializable { 27 | private static final long serialVersionUID = 3736359137726536495L; 28 | 29 | private String mName; 30 | //页面名 31 | private String mClazz; 32 | //页面class 33 | private String mParams; 34 | //传入参数,json object结构 35 | 36 | public CorePage(String name, String clazz, String params) { 37 | mName = name; 38 | mClazz = clazz; 39 | mParams = params; 40 | } 41 | 42 | public String getClazz() { 43 | return mClazz; 44 | } 45 | 46 | public void setClazz(String clazz) { 47 | mClazz = clazz; 48 | } 49 | 50 | public String getName() { 51 | return mName; 52 | } 53 | 54 | public void setName(String name) { 55 | mName = name; 56 | } 57 | 58 | public String getParams() { 59 | return mParams; 60 | } 61 | 62 | public void setParams(String params) { 63 | mParams = params; 64 | } 65 | 66 | @Override 67 | public String toString() { 68 | return "Page{" + 69 | "mName='" + mName + '\'' + 70 | ", mClazz='" + mClazz + '\'' + 71 | ", mParams='" + mParams + '\'' + 72 | '}'; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /library/src/main/java/cn/edu/zafu/corepage/core/CorePageManager.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 ZhangQu Li 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.edu.zafu.corepage.core; 17 | 18 | import android.content.Context; 19 | import android.os.Bundle; 20 | import android.support.v4.app.Fragment; 21 | import android.support.v4.app.FragmentManager; 22 | import android.support.v4.app.FragmentTransaction; 23 | import android.text.TextUtils; 24 | import android.util.Log; 25 | 26 | import com.alibaba.fastjson.JSON; 27 | import com.alibaba.fastjson.JSONArray; 28 | import com.alibaba.fastjson.JSONObject; 29 | 30 | import java.io.BufferedReader; 31 | import java.io.InputStreamReader; 32 | import java.util.HashMap; 33 | import java.util.Iterator; 34 | import java.util.Map; 35 | import java.util.Set; 36 | 37 | import cn.edu.zafu.corepage.R; 38 | import cn.edu.zafu.corepage.base.BaseFragment; 39 | 40 | /** 41 | * 跳转页面管理 42 | * User:lizhangqu(513163535@qq.com) 43 | * Date:2015-07-22 44 | * Time: 09:34 45 | */ 46 | public class CorePageManager { 47 | private static final String TAG = CorePageManager.class.getSimpleName(); 48 | //日志TAG 49 | private volatile static CorePageManager mInstance = null; 50 | //单例 51 | private Context mContext; 52 | //Context上下文 53 | private Map mPageMap = new HashMap(); 54 | //保存page的map 55 | 56 | /** 57 | * 构造函数私有化 58 | */ 59 | private CorePageManager() { 60 | 61 | } 62 | 63 | /** 64 | * 获得单例 65 | * 66 | * @return PageManager 单例 67 | */ 68 | public static CorePageManager getInstance() { 69 | if (mInstance == null) { 70 | synchronized (CorePageManager.class) { 71 | if (mInstance == null) { 72 | mInstance = new CorePageManager(); 73 | } 74 | } 75 | } 76 | return mInstance; 77 | } 78 | 79 | /** 80 | * 初始化配置 81 | * 82 | * @param context 上下文 83 | */ 84 | public void init(Context context) { 85 | try { 86 | mContext = context.getApplicationContext(); 87 | 88 | String content = readFileFromAssets(mContext, "page.json"); 89 | readConfig(content); 90 | } catch (Exception e) { 91 | e.printStackTrace(); 92 | } 93 | } 94 | public void init(Context context,String pageJson) { 95 | this.init(context); 96 | readConfig(pageJson); 97 | } 98 | /** 99 | * 从配置文件中读取page 100 | */ 101 | 102 | public void readConfig(String content) { 103 | Log.d(TAG, "readConfig from json"); 104 | JSONArray jsonArray = JSON.parseArray(content); 105 | Iterator iterator = jsonArray.iterator(); 106 | JSONObject jsonPage = null; 107 | String pageName = null; 108 | String pageClazz = null; 109 | String pageParams = null; 110 | while (iterator.hasNext()) { 111 | jsonPage = (JSONObject) iterator.next(); 112 | pageName = jsonPage.getString("name"); 113 | pageClazz = jsonPage.getString("class"); 114 | pageParams = jsonPage.getString("params"); 115 | if (TextUtils.isEmpty(pageName) || TextUtils.isEmpty(pageClazz)) { 116 | Log.d(TAG, "page Name is null or pageClass is null"); 117 | return; 118 | } 119 | mPageMap.put(pageName, new CorePage(pageName, pageClazz, pageParams)); 120 | Log.d(TAG, "put a page:" + pageName); 121 | } 122 | Log.d(TAG, "finished read pages,page size:" + mPageMap.size()); 123 | } 124 | 125 | /** 126 | * 从assets目录下读取文件 127 | * 128 | * @param context 上下文 129 | * @param fileName 文件名 130 | * @return 131 | */ 132 | private String readFileFromAssets(Context context, String fileName) { 133 | String result = ""; 134 | try { 135 | InputStreamReader inputReader = new InputStreamReader(context.getResources().getAssets().open(fileName)); 136 | BufferedReader bufReader = new BufferedReader(inputReader); 137 | String line = ""; 138 | while ((line = bufReader.readLine()) != null) 139 | result += line; 140 | } catch (Exception e) { 141 | e.printStackTrace(); 142 | } 143 | return result; 144 | } 145 | 146 | /** 147 | * 新增新页面 148 | * 149 | * @param name 页面名 150 | * @param clazz 页面class 151 | * @param params 页面参数 152 | * @return 是否新增成功 153 | */ 154 | public boolean putPage(String name, Class clazz, Map params) { 155 | if (TextUtils.isEmpty(name) || clazz == null) { 156 | Log.d(TAG, "page Name is null or pageClass is null"); 157 | return false; 158 | } 159 | if (mPageMap.containsKey(name)) { 160 | Log.d(TAG, "page has already put!"); 161 | return false; 162 | } 163 | CorePage corePage = new CorePage(name, clazz.getName(), buildParams(params)); 164 | Log.d(TAG, "put a page:" + name); 165 | return true; 166 | } 167 | 168 | /** 169 | * 从hashMap中得到参数的json格式 170 | * 171 | * @param params 页面map形式参数 172 | * @return json格式参数 173 | */ 174 | private String buildParams(Map params) { 175 | if (params == null) { 176 | return ""; 177 | } 178 | String result = JSON.toJSONString(params); 179 | Log.d(TAG, "params:" + result); 180 | return result; 181 | } 182 | 183 | /** 184 | * 页面跳转核心函数之一 185 | * 打开一个Fragement,如果返回栈中有则出栈,否则新建 186 | * 187 | * @param fragmentManager FragmentManager管理类 188 | * @param pageName 页面别名 189 | * @param bundle 参数 190 | * @param animations 动画 191 | * @return 成功跳转到的fragment 192 | */ 193 | public Fragment gotoPage(FragmentManager fragmentManager, String pageName, Bundle bundle, int[] animations) { 194 | Log.d(TAG, "gotoPage:" + pageName); 195 | Fragment fragment = null; 196 | if (fragmentManager != null) { 197 | fragment = fragmentManager.findFragmentByTag(pageName); 198 | } 199 | if (fragment != null) { 200 | fragmentManager.popBackStackImmediate(pageName, 0); 201 | } else { 202 | fragment = this.openPageWithNewFragmentManager(fragmentManager, pageName, bundle, animations, true); 203 | } 204 | return fragment; 205 | 206 | } 207 | 208 | /** 209 | * 页面跳转核心函数之一 210 | * 打开一个fragemnt 211 | * 212 | * @param fragmentManager FragmentManager管理类 213 | * @param pageName 页面名 214 | * @param bundle 参数 215 | * @param animations 动画类型 216 | * @param addToBackStack 是否添加到返回栈 217 | * @return 打开的Fragment对象 218 | */ 219 | public Fragment openPageWithNewFragmentManager(FragmentManager fragmentManager, String pageName, Bundle bundle, int[] animations, boolean addToBackStack) { 220 | BaseFragment fragment = null; 221 | try { 222 | CorePage corePage = this.mPageMap.get(pageName); 223 | if (corePage == null) { 224 | Log.d(TAG, "Page:" + pageName + " is null"); 225 | return null; 226 | } 227 | /** 228 | * Atlas的支持 start 229 | */ 230 | if (CoreConfig.isOpenAtlas()){ 231 | ClassLoader bundleClassLoader = CoreConfig.getBundleClassLoader(); 232 | if(bundleClassLoader==null){ 233 | Log.d(TAG, "OpenAtlas bundle ClassLoader is null!"); 234 | return null; 235 | } 236 | fragment = (BaseFragment) CoreConfig.getBundleClassLoader().loadClass(corePage.getClazz()).newInstance(); 237 | }else{ 238 | fragment = (BaseFragment) Class.forName(corePage.getClazz()).newInstance(); 239 | } 240 | /** 241 | * Atlas的支持 end 242 | */ 243 | 244 | Bundle pageBundle = buildBundle(corePage); 245 | if (bundle != null) { 246 | pageBundle.putAll(bundle); 247 | } 248 | fragment.setArguments(pageBundle); 249 | fragment.setPageName(pageName); 250 | 251 | FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 252 | if (animations != null && animations.length >= 4) { 253 | fragmentTransaction.setCustomAnimations(animations[0], animations[1], animations[2], animations[3]); 254 | } 255 | Fragment fragmentContainer = fragmentManager.findFragmentById(R.id.fragment_container); 256 | if (fragmentContainer != null) { 257 | fragmentTransaction.hide(fragmentContainer); 258 | } 259 | 260 | 261 | fragmentTransaction.add(R.id.fragment_container, fragment, pageName); 262 | if (addToBackStack) { 263 | fragmentTransaction.addToBackStack(pageName); 264 | } 265 | 266 | fragmentTransaction.commitAllowingStateLoss(); 267 | //fragmentTransaction.commit(); 268 | 269 | } catch (Exception e) { 270 | e.printStackTrace(); 271 | Log.d(TAG, "Fragment.error:" + e.getMessage()); 272 | return null; 273 | } 274 | return fragment; 275 | } 276 | /** 277 | * 根据page,从pageParams中获得bundle 278 | * 279 | * @param corePage 页面 280 | * @return 页面的参数 281 | */ 282 | private Bundle buildBundle(CorePage corePage) { 283 | Bundle bundle = new Bundle(); 284 | String key = null; 285 | Object value = null; 286 | if (corePage != null && corePage.getParams() != null) { 287 | JSONObject j = JSON.parseObject(corePage.getParams()); 288 | if (j != null) { 289 | Set keySet = j.keySet(); 290 | if (keySet != null) { 291 | Iterator ite = keySet.iterator(); 292 | while (ite.hasNext()) { 293 | key = ite.next(); 294 | value = j.get(key); 295 | bundle.putString(key, value.toString()); 296 | } 297 | } 298 | } 299 | } 300 | return bundle; 301 | } 302 | 303 | /** 304 | * 判断fragment是否位于栈顶 305 | * 306 | * @param context 上下文 307 | * @param fragmentTag fragment的tag 308 | * @return 是否是栈顶Fragment 309 | */ 310 | /* public boolean isFragmentTop(Context context, String fragmentTag) { 311 | if (context != null && context instanceof Switcher) { 312 | return ((Switcher) context).isFragmentTop(fragmentTag); 313 | } else { 314 | BaseActivity topActivity = BaseActivity.getTopActivity(); 315 | if (topActivity != null) { 316 | return topActivity.isFragmentTop(fragmentTag); 317 | } else { 318 | return false; 319 | } 320 | } 321 | }*/ 322 | } 323 | -------------------------------------------------------------------------------- /library/src/main/java/cn/edu/zafu/corepage/core/CoreSwitchBean.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 ZhangQu Li 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.edu.zafu.corepage.core; 17 | 18 | import android.os.Bundle; 19 | import android.os.Parcel; 20 | import android.os.Parcelable; 21 | 22 | import java.util.Arrays; 23 | 24 | import cn.edu.zafu.corepage.R; 25 | 26 | 27 | /** 28 | * 页面跳转控制参数 29 | * User:lizhangqu(513163535@qq.com) 30 | * Date:2015-07-22 31 | * Time: 09:34 32 | */ 33 | public class CoreSwitchBean implements Parcelable { 34 | public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { 35 | @Override 36 | public CoreSwitchBean createFromParcel(Parcel in) { 37 | return new CoreSwitchBean(in); 38 | } 39 | 40 | @Override 41 | public CoreSwitchBean[] newArray(int size) { 42 | return new CoreSwitchBean[size]; 43 | } 44 | }; 45 | private String mPageName; 46 | //页面名 47 | private Bundle mBundle; 48 | //相关数据 49 | private int[] mAnim = null; 50 | //动画类型 51 | private boolean mAddToBackStack = true; 52 | //是否添加到栈中 53 | private boolean mNewActivity = false; 54 | //是否起新的Activity 55 | private int requestCode = -1; 56 | 57 | //fragment跳转 58 | public CoreSwitchBean(String pageName) { 59 | this.mPageName = pageName; 60 | } 61 | 62 | public CoreSwitchBean(String pageName, Bundle bundle) { 63 | this.mPageName = pageName; 64 | this.mBundle = bundle; 65 | } 66 | 67 | public CoreSwitchBean(String pageName, Bundle bundle, CoreAnim coreAnim) { 68 | this.mPageName = pageName; 69 | this.mBundle = bundle; 70 | this.setAnim(coreAnim); 71 | } 72 | 73 | public void setAnim(CoreAnim anim) { 74 | mAnim = convertAnimations(anim); 75 | } 76 | 77 | /** 78 | * 动画转化,根据枚举类返回int数组 79 | * 80 | * @param coreAnim 动画枚举 81 | * @return 转化后的动画数组 82 | */ 83 | public static int[] convertAnimations(CoreAnim coreAnim) { 84 | if (coreAnim == CoreAnim.present) { 85 | int[] animations = {R.anim.push_in_down, R.anim.push_no_ani, R.anim.push_no_ani, R.anim.push_out_down}; 86 | return animations; 87 | } else if (coreAnim == CoreAnim.fade) { 88 | int[] animations = {R.anim.alpha_in, R.anim.alpha_out, R.anim.alpha_in, R.anim.alpha_out}; 89 | return animations; 90 | } else if (coreAnim == CoreAnim.slide) { 91 | int[] animations = {R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right}; 92 | return animations; 93 | }else if (coreAnim == CoreAnim.zoom) { 94 | int[] animations = {R.anim.zoom_in, R.anim.zoom_out, R.anim.zoom_in, R.anim.zoom_out}; 95 | return animations; 96 | } 97 | return null; 98 | } 99 | 100 | public CoreSwitchBean(String pageName, Bundle bundle, int[] anim) { 101 | this.mPageName = pageName; 102 | this.mBundle = bundle; 103 | this.mAnim = anim; 104 | } 105 | 106 | public CoreSwitchBean(String pageName, Bundle bundle, CoreAnim coreAnim, boolean addToBackStack) { 107 | this.mPageName = pageName; 108 | this.mBundle = bundle; 109 | this.setAnim(coreAnim); 110 | this.mAddToBackStack = addToBackStack; 111 | } 112 | 113 | public CoreSwitchBean(String pageName, Bundle bundle, int[] anim, boolean addToBackStack) { 114 | this.mPageName = pageName; 115 | this.mBundle = bundle; 116 | this.mAnim = anim; 117 | this.mAddToBackStack = addToBackStack; 118 | } 119 | 120 | public CoreSwitchBean(String pageName, Bundle bundle, CoreAnim coreAnim, boolean addToBackStack, boolean newActivity) { 121 | this.mPageName = pageName; 122 | this.mBundle = bundle; 123 | this.setAnim(coreAnim); 124 | this.mAddToBackStack = addToBackStack; 125 | this.mNewActivity = newActivity; 126 | } 127 | 128 | public CoreSwitchBean(String pageName, Bundle bundle, int[] anim, boolean addToBackStack, boolean newActivity) { 129 | this.mPageName = pageName; 130 | this.mBundle = bundle; 131 | this.mAnim = anim; 132 | this.mAddToBackStack = addToBackStack; 133 | this.mNewActivity = newActivity; 134 | } 135 | 136 | public CoreSwitchBean(String pageName, Bundle bundle, int[] anim, boolean addToBackStack, boolean newActivity, int requestCode) { 137 | this.mPageName = pageName; 138 | this.mBundle = bundle; 139 | this.mAnim = anim; 140 | this.mAddToBackStack = addToBackStack; 141 | this.mNewActivity = newActivity; 142 | this.requestCode = requestCode; 143 | } 144 | 145 | 146 | protected CoreSwitchBean(Parcel in) { 147 | mPageName = in.readString(); 148 | mBundle = in.readBundle(); 149 | int[] a = {in.readInt(), in.readInt(), in.readInt(), in.readInt()}; 150 | mAnim = a; 151 | mAddToBackStack = in.readInt() == 1 ? true : false; 152 | mNewActivity = in.readInt() == 1 ? true : false; 153 | requestCode = in.readInt(); 154 | } 155 | 156 | public String getPageName() { 157 | return mPageName; 158 | } 159 | 160 | public void setPageName(String pageName) { 161 | mPageName = pageName; 162 | } 163 | 164 | public boolean isNewActivity() { 165 | return mNewActivity; 166 | } 167 | 168 | public void setNewActivity(boolean newActivity) { 169 | mNewActivity = newActivity; 170 | } 171 | 172 | public boolean isAddToBackStack() { 173 | return mAddToBackStack; 174 | } 175 | 176 | public void setAddToBackStack(boolean addToBackStack) { 177 | mAddToBackStack = addToBackStack; 178 | } 179 | 180 | public int[] getAnim() { 181 | return mAnim; 182 | } 183 | 184 | public void setAnim(int[] anim) { 185 | mAnim = anim; 186 | } 187 | 188 | public Bundle getBundle() { 189 | return mBundle; 190 | } 191 | 192 | public void setBundle(Bundle bundle) { 193 | mBundle = bundle; 194 | } 195 | 196 | public int getRequestCode() { 197 | return requestCode; 198 | } 199 | 200 | public void setRequestCode(int requestCode) { 201 | this.requestCode = requestCode; 202 | } 203 | @Override 204 | public String toString() { 205 | return "SwitchBean{" + 206 | "mPageName='" + mPageName + '\'' + 207 | ", mBundle=" + mBundle + 208 | ", mAnim=" + Arrays.toString(mAnim) + 209 | ", mAddToBackStack=" + mAddToBackStack + 210 | ", mNewActivity=" + mNewActivity + 211 | ", requestCode=" + requestCode + 212 | '}'; 213 | } 214 | 215 | @Override 216 | public int describeContents() { 217 | return 0; 218 | } 219 | 220 | @Override 221 | public void writeToParcel(Parcel out, int flags) { 222 | if (mPageName == null) { 223 | mPageName = ""; 224 | } 225 | if (mBundle == null) { 226 | mBundle = new Bundle(); 227 | } 228 | if (mAnim == null) { 229 | int[] a = {-1, -1, -1, -1}; 230 | mAnim = a; 231 | } 232 | out.writeString(mPageName); 233 | mBundle.writeToParcel(out, flags); 234 | if (mAnim != null && mAnim.length == 4) { 235 | out.writeInt(mAnim[0]); 236 | out.writeInt(mAnim[1]); 237 | out.writeInt(mAnim[2]); 238 | out.writeInt(mAnim[3]); 239 | } else { 240 | out.writeInt(-1); 241 | out.writeInt(-1); 242 | out.writeInt(-1); 243 | out.writeInt(-1); 244 | } 245 | out.writeInt(mAddToBackStack ? 1 : 0); 246 | out.writeInt(mNewActivity ? 1 : 0); 247 | out.writeInt(requestCode); 248 | } 249 | 250 | 251 | } 252 | -------------------------------------------------------------------------------- /library/src/main/java/cn/edu/zafu/corepage/core/CoreSwitcher.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2015 ZhangQu Li 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | *

8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | *

10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.edu.zafu.corepage.core; 17 | 18 | import android.support.v4.app.Fragment; 19 | 20 | import java.util.List; 21 | 22 | import cn.edu.zafu.corepage.base.BaseFragment; 23 | 24 | /** 25 | * 页面跳转接口,用于控制页面跳转或启动新的activity 26 | * User:lizhangqu(513163535@qq.com) 27 | * Date:2015-07-22 28 | * Time: 09:34 29 | */ 30 | public interface CoreSwitcher { 31 | /** 32 | * 返回到某一个页面(只有一个fragment时会关闭Activityt) 33 | */ 34 | void popPage(); 35 | 36 | /** 37 | * fragmentTag 是否在当前顶上activity上的最顶上的fragment 38 | * 39 | * @param fragmentTag fragment的tag 40 | * @return 是否位于栈顶 41 | */ 42 | boolean isFragmentTop(String fragmentTag); 43 | 44 | 45 | /** 46 | * 是否查找到某个page 47 | * 48 | * @param pageName 页面名 49 | * @return 是否找到 50 | */ 51 | boolean findPage(final String pageName); 52 | 53 | /** 54 | * 跳转到某一个页面。 55 | * 56 | * @param bean CoreSwitchBean对象 57 | * @return 打开的页面Fragment对象 58 | */ 59 | Fragment gotoPage(CoreSwitchBean bean); 60 | 61 | /** 62 | * 打开一个新的页面 63 | * 64 | * @param bean CoreSwitchBean对象 65 | * @return 打开的页面Fragment对象 66 | */ 67 | Fragment openPage(CoreSwitchBean bean); 68 | 69 | /** 70 | * 移除当前Acitivity不需要的fragment 71 | * 72 | * @param fragmentLists 无用fragment列表 73 | */ 74 | void removeUnlessFragment(List fragmentLists); 75 | 76 | /** 77 | * 页面跳转,支持跨Activity进行传递数据 78 | * 79 | * @param page CoreSwitchBean对象 80 | * @param fragment BaseFragment对象 81 | * @return 打开的页面Fragment对象 82 | */ 83 | public Fragment openPageForResult(final CoreSwitchBean page, final BaseFragment fragment); 84 | 85 | } 86 | -------------------------------------------------------------------------------- /library/src/main/res/anim/alpha_in.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | -------------------------------------------------------------------------------- /library/src/main/res/anim/alpha_out.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | -------------------------------------------------------------------------------- /library/src/main/res/anim/push_in_down.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | -------------------------------------------------------------------------------- /library/src/main/res/anim/push_no_ani.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | -------------------------------------------------------------------------------- /library/src/main/res/anim/push_out_down.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | -------------------------------------------------------------------------------- /library/src/main/res/anim/slide_in_left.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 8 | 9 | -------------------------------------------------------------------------------- /library/src/main/res/anim/slide_in_right.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 8 | 9 | -------------------------------------------------------------------------------- /library/src/main/res/anim/slide_out_left.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 8 | 9 | -------------------------------------------------------------------------------- /library/src/main/res/anim/slide_out_right.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 8 | 9 | -------------------------------------------------------------------------------- /library/src/main/res/anim/zoom_in.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 25 | 26 | 28 | 29 | 30 | 37 | 38 | 41 | 42 | -------------------------------------------------------------------------------- /library/src/main/res/anim/zoom_out.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 27 | 30 | 31 | 38 | 39 | 42 | 43 | -------------------------------------------------------------------------------- /library/src/main/res/layout/activity_base.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | -------------------------------------------------------------------------------- /library/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | library 3 | 4 | -------------------------------------------------------------------------------- /library/src/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 22 5 | buildToolsVersion "22.0.1" 6 | 7 | defaultConfig { 8 | applicationId "cn.edu.zafu.corepage.sample" 9 | minSdkVersion 15 10 | targetSdkVersion 22 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(include: ['*.jar'], dir: 'libs') 24 | compile 'com.android.support:appcompat-v7:22.2.0' 25 | compile project(':library') 26 | debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1' 27 | releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1' 28 | } 29 | -------------------------------------------------------------------------------- /sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\adt-bundle-windows-x64-20140702\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /sample/src/androidTest/java/cn/edu/zafu/corepage/samplle/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package cn.edu.zafu.corepage.samplle; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 12 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /sample/src/main/assets/page.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "name": "main", 4 | "class": "cn.edu.zafu.corepage.sample.MainFragment", 5 | "params": "" 6 | }, 7 | { 8 | "name": "test1", 9 | "class": "cn.edu.zafu.corepage.sample.TestFragment1", 10 | "params": "" 11 | }, 12 | { 13 | "name": "test2", 14 | "class": "cn.edu.zafu.corepage.sample.TestFragment2", 15 | "params": "" 16 | }, 17 | { 18 | "name": "test3", 19 | "class": "cn.edu.zafu.corepage.sample.TestFragment3", 20 | "params": { 21 | "key1":"value1", 22 | "key2":"value2" 23 | } 24 | } 25 | ] 26 | -------------------------------------------------------------------------------- /sample/src/main/java/cn/edu/zafu/corepage/sample/BaseApplication.java: -------------------------------------------------------------------------------- 1 | package cn.edu.zafu.corepage.sample; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | 6 | import com.squareup.leakcanary.LeakCanary; 7 | import com.squareup.leakcanary.RefWatcher; 8 | 9 | import cn.edu.zafu.corepage.core.CoreConfig; 10 | 11 | /** 12 | * 全局Application基类,用于初始化Page 13 | * User:lizhangqu(513163535@qq.com) 14 | * Date:2015-07-22 15 | * Time: 09:35 16 | */ 17 | public class BaseApplication extends Application { 18 | private String pageJson="[" + 19 | " {" + 20 | " 'name': 'test4'," + 21 | " 'class': 'cn.edu.zafu.corepage.sample.TestFragment4'," + 22 | " 'params': ''" + 23 | " }]"; 24 | private RefWatcher refWatcher; 25 | 26 | @Override 27 | public void onCreate() { 28 | super.onCreate(); 29 | CoreConfig.init(this, pageJson); 30 | refWatcher=LeakCanary.install(this); 31 | // or such as this 32 | //CoreConfig.init(this); 33 | //CoreConfig.readConfig(pageJson); 34 | } 35 | 36 | public static RefWatcher getRefWatcher(Context context) { 37 | BaseApplication application = (BaseApplication) context.getApplicationContext(); 38 | return application.refWatcher; 39 | } 40 | 41 | 42 | } 43 | -------------------------------------------------------------------------------- /sample/src/main/java/cn/edu/zafu/corepage/sample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package cn.edu.zafu.corepage.sample; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | 6 | import cn.edu.zafu.corepage.base.BaseActivity; 7 | import cn.edu.zafu.corepage.core.CoreAnim; 8 | 9 | 10 | public class MainActivity extends BaseActivity { 11 | @Override 12 | public void onCreate(Bundle savedInstanceState) { 13 | super.onCreate(savedInstanceState); 14 | openPage("main", null, CoreAnim.none); 15 | 16 | 17 | } 18 | 19 | @Override 20 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 21 | super.onActivityResult(requestCode, resultCode, data); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /sample/src/main/java/cn/edu/zafu/corepage/sample/MainFragment.java: -------------------------------------------------------------------------------- 1 | package cn.edu.zafu.corepage.sample; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.Button; 9 | import android.widget.Toast; 10 | 11 | import com.squareup.leakcanary.RefWatcher; 12 | 13 | import cn.edu.zafu.corepage.base.BaseFragment; 14 | import cn.edu.zafu.corepage.core.CoreAnim; 15 | 16 | 17 | /** 18 | * User:lizhangqu(513163535@qq.com) 19 | * Date:2015-07-20 20 | * Time: 16:29 21 | */ 22 | public class MainFragment extends BaseFragment implements View.OnClickListener { 23 | private Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9; 24 | 25 | 26 | @Override 27 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 28 | View view = inflater.inflate(R.layout.fragment_main, container, false); 29 | return view; 30 | } 31 | 32 | @Override 33 | public void onViewCreated(View view, Bundle savedInstanceState) { 34 | super.onViewCreated(view, savedInstanceState); 35 | btn1= (Button) view.findViewById(R.id.btn1); 36 | btn2= (Button) view.findViewById(R.id.btn2); 37 | btn3= (Button) view.findViewById(R.id.btn3); 38 | btn4= (Button) view.findViewById(R.id.btn4); 39 | btn5= (Button) view.findViewById(R.id.btn5); 40 | btn6= (Button) view.findViewById(R.id.btn6); 41 | btn7= (Button) view.findViewById(R.id.btn7); 42 | btn8= (Button) view.findViewById(R.id.btn8); 43 | btn9= (Button) view.findViewById(R.id.btn9); 44 | btn1.setOnClickListener(this); 45 | btn2.setOnClickListener(this); 46 | btn3.setOnClickListener(this); 47 | btn4.setOnClickListener(this); 48 | btn5.setOnClickListener(this); 49 | btn6.setOnClickListener(this); 50 | btn7.setOnClickListener(this); 51 | btn8.setOnClickListener(this); 52 | btn9.setOnClickListener(this); 53 | } 54 | 55 | @Override 56 | public void onClick(View v) { 57 | switch (v.getId()) { 58 | case R.id.btn1: 59 | //int anim[]= {R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right}; 60 | //openPage("test1",null, anim); 61 | openPage("test1",null, CoreAnim.slide); 62 | break; 63 | case R.id.btn2: 64 | openPage("test1",null,CoreAnim.none); 65 | break; 66 | case R.id.btn3: 67 | openPage("test1",null,CoreAnim.fade,true); 68 | break; 69 | case R.id.btn4: 70 | openPage("test1",null,CoreAnim.fade,false); 71 | break; 72 | case R.id.btn5: 73 | openPage("test1",null,CoreAnim.fade,true,true); 74 | break; 75 | case R.id.btn6: 76 | int requestCode=1; 77 | Bundle bundle=new Bundle(); 78 | openPageForResult("test2",bundle,CoreAnim.fade,requestCode); 79 | break; 80 | case R.id.btn7: 81 | Bundle params=new Bundle(); 82 | params.putString("test","hello i'm from main"); 83 | openPage("test3", params, CoreAnim.slide); 84 | break; 85 | case R.id.btn8: 86 | popToBack(); 87 | break; 88 | case R.id.btn9: 89 | openPage("test4",null, CoreAnim.present); 90 | break; 91 | 92 | } 93 | } 94 | 95 | @Override 96 | public void onFragmentResult(int requestCode, int resultCode, Intent data) { 97 | super.onFragmentResult(requestCode, resultCode, data); 98 | if(data!=null) { 99 | Bundle extras = data.getExtras(); 100 | Toast.makeText(getActivity(), "requestCode:" + requestCode + " result:" + resultCode+" data:"+extras.getString("data"), Toast.LENGTH_LONG).show(); 101 | } 102 | } 103 | @Override 104 | public void onDestroy() { 105 | super.onDestroy(); 106 | RefWatcher refWatcher = BaseApplication.getRefWatcher(getActivity()); 107 | refWatcher.watch(this); 108 | } 109 | 110 | } 111 | -------------------------------------------------------------------------------- /sample/src/main/java/cn/edu/zafu/corepage/sample/TestFragment1.java: -------------------------------------------------------------------------------- 1 | package cn.edu.zafu.corepage.sample; 2 | 3 | import android.os.Bundle; 4 | import android.view.LayoutInflater; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | 8 | import com.squareup.leakcanary.RefWatcher; 9 | 10 | import cn.edu.zafu.corepage.base.BaseFragment; 11 | 12 | 13 | /** 14 | * User:lizhangqu(513163535@qq.com) 15 | * Date:2015-07-20 16 | * Time: 16:29 17 | */ 18 | public class TestFragment1 extends BaseFragment { 19 | @Override 20 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 21 | View view=inflater.inflate(R.layout.fragment_test1, container, false); 22 | 23 | return view; 24 | } 25 | 26 | 27 | 28 | @Override 29 | public void onCreate(Bundle savedInstanceState) { 30 | super.onCreate(savedInstanceState); 31 | } 32 | @Override 33 | public void onDestroy() { 34 | super.onDestroy(); 35 | RefWatcher refWatcher = BaseApplication.getRefWatcher(getActivity()); 36 | refWatcher.watch(this); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /sample/src/main/java/cn/edu/zafu/corepage/sample/TestFragment2.java: -------------------------------------------------------------------------------- 1 | package cn.edu.zafu.corepage.sample; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | 9 | import com.squareup.leakcanary.RefWatcher; 10 | 11 | import cn.edu.zafu.corepage.base.BaseFragment; 12 | 13 | 14 | /** 15 | * User:lizhangqu(513163535@qq.com) 16 | * Date:2015-07-20 17 | * Time: 16:29 18 | */ 19 | public class TestFragment2 extends BaseFragment { 20 | @Override 21 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 22 | View view=inflater.inflate(R.layout.fragment_test2, container, false); 23 | view.findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() { 24 | @Override 25 | public void onClick(View v) { 26 | Intent intent=new Intent(); 27 | Bundle bundle=new Bundle(); 28 | bundle.putString("data","this is a result"); 29 | intent.putExtras(bundle); 30 | setFragmentResult(500,intent); 31 | popToBack(); 32 | } 33 | }); 34 | return view; 35 | } 36 | 37 | 38 | 39 | @Override 40 | public void onCreate(Bundle savedInstanceState) { 41 | super.onCreate(savedInstanceState); 42 | } 43 | @Override 44 | public void onDestroy() { 45 | super.onDestroy(); 46 | RefWatcher refWatcher = BaseApplication.getRefWatcher(getActivity()); 47 | refWatcher.watch(this); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /sample/src/main/java/cn/edu/zafu/corepage/sample/TestFragment3.java: -------------------------------------------------------------------------------- 1 | package cn.edu.zafu.corepage.sample; 2 | 3 | import android.os.Bundle; 4 | import android.view.LayoutInflater; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.Toast; 8 | 9 | import com.squareup.leakcanary.RefWatcher; 10 | 11 | import cn.edu.zafu.corepage.base.BaseFragment; 12 | 13 | 14 | /** 15 | * User:lizhangqu(513163535@qq.com) 16 | * Date:2015-07-20 17 | * Time: 16:29 18 | */ 19 | public class TestFragment3 extends BaseFragment { 20 | @Override 21 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 22 | View view=inflater.inflate(R.layout.fragment_test3, container, false); 23 | 24 | return view; 25 | } 26 | 27 | @Override 28 | public void onCreate(Bundle savedInstanceState) { 29 | super.onCreate(savedInstanceState); 30 | Bundle arguments = getArguments(); 31 | if (arguments!=null) { 32 | Toast.makeText(getActivity(),"get params:"+arguments.getString("key1")+" "+arguments.getString("key2")+" "+arguments.getString("test"),Toast.LENGTH_LONG ).show(); 33 | } 34 | } 35 | @Override 36 | public void onDestroy() { 37 | super.onDestroy(); 38 | RefWatcher refWatcher = BaseApplication.getRefWatcher(getActivity()); 39 | refWatcher.watch(this); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /sample/src/main/java/cn/edu/zafu/corepage/sample/TestFragment4.java: -------------------------------------------------------------------------------- 1 | package cn.edu.zafu.corepage.sample; 2 | 3 | import android.os.Bundle; 4 | import android.view.LayoutInflater; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | 8 | import com.squareup.leakcanary.RefWatcher; 9 | 10 | import cn.edu.zafu.corepage.base.BaseFragment; 11 | 12 | 13 | /** 14 | * User:lizhangqu(513163535@qq.com) 15 | * Date:2015-07-20 16 | * Time: 16:29 17 | */ 18 | public class TestFragment4 extends BaseFragment { 19 | @Override 20 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 21 | View view=inflater.inflate(R.layout.fragment_test4, container, false); 22 | 23 | return view; 24 | } 25 | 26 | @Override 27 | public void onDestroy() { 28 | super.onDestroy(); 29 | RefWatcher refWatcher = BaseApplication.getRefWatcher(getActivity()); 30 | refWatcher.watch(this); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/fragment_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 |