├── .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 |
7 |
8 |
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 | [  ](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