├── settings.gradle
├── image
├── 2.gif
├── 3.gif
├── 4.gif
└── 5.gif
├── fragment
├── src
│ └── main
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── yanzhenjie
│ │ └── fragment
│ │ ├── ResultCode.java
│ │ ├── CompatActivity.java
│ │ └── NoFragment.java
├── build.gradle
└── maven.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── sample
├── src
│ └── main
│ │ ├── res
│ │ ├── mipmap-hdpi
│ │ │ └── ic_launcher.png
│ │ ├── mipmap-mdpi
│ │ │ └── ic_launcher.png
│ │ ├── mipmap-xhdpi
│ │ │ └── ic_launcher.png
│ │ ├── mipmap-xxhdpi
│ │ │ └── ic_launcher.png
│ │ ├── mipmap-xxxhdpi
│ │ │ └── ic_launcher.png
│ │ ├── drawable-hdpi
│ │ │ ├── ic_back_white.png
│ │ │ ├── ic_close_white.png
│ │ │ ├── ic_more_white.png
│ │ │ ├── ic_settings_white.png
│ │ │ ├── ic_person_add_white.png
│ │ │ ├── ic_sentiment_neutral_black.png
│ │ │ ├── ic_sentiment_satisfied_black.png
│ │ │ └── ic_sentiment_dissatisfied_black.png
│ │ ├── drawable-xhdpi
│ │ │ ├── ic_back_white.png
│ │ │ ├── ic_close_white.png
│ │ │ ├── ic_more_white.png
│ │ │ ├── ic_person_add_white.png
│ │ │ ├── ic_settings_white.png
│ │ │ ├── ic_sentiment_neutral_black.png
│ │ │ ├── ic_sentiment_satisfied_black.png
│ │ │ └── ic_sentiment_dissatisfied_black.png
│ │ ├── drawable-xxhdpi
│ │ │ ├── ic_back_white.png
│ │ │ ├── ic_more_white.png
│ │ │ ├── ic_close_white.png
│ │ │ ├── ic_settings_white.png
│ │ │ ├── ic_person_add_white.png
│ │ │ ├── ic_sentiment_neutral_black.png
│ │ │ ├── ic_sentiment_satisfied_black.png
│ │ │ └── ic_sentiment_dissatisfied_black.png
│ │ ├── drawable-xxxhdpi
│ │ │ ├── ic_back_white.png
│ │ │ ├── ic_close_white.png
│ │ │ ├── ic_more_white.png
│ │ │ ├── ic_settings_white.png
│ │ │ ├── ic_person_add_white.png
│ │ │ ├── ic_sentiment_neutral_black.png
│ │ │ ├── ic_sentiment_satisfied_black.png
│ │ │ └── ic_sentiment_dissatisfied_black.png
│ │ ├── values
│ │ │ ├── colors.xml
│ │ │ ├── dimens.xml
│ │ │ ├── styles.xml
│ │ │ └── strings.xml
│ │ ├── layout
│ │ │ ├── activity_main.xml
│ │ │ ├── item.xml
│ │ │ ├── fragment_menu.xml
│ │ │ ├── fragment_argument.xml
│ │ │ ├── fragment_stack_new.xml
│ │ │ ├── fragment_stack.xml
│ │ │ ├── fragment_result.xml
│ │ │ └── fragment_main.xml
│ │ └── menu
│ │ │ ├── menu_fragment_main.xml
│ │ │ └── menu_fragment_setting.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── yanzhenjie
│ │ └── fragment
│ │ └── sample
│ │ ├── MainActivity.java
│ │ ├── adapter
│ │ └── RecyclerAdapter.java
│ │ └── fragment
│ │ ├── StackFragment.java
│ │ ├── StackNewFragment.java
│ │ ├── ArgumentFragment.java
│ │ ├── StartResultFragment.java
│ │ ├── MoreMenuFragment.java
│ │ └── MainFragment.java
└── build.gradle
├── gradle.properties
├── config.gradle
├── gradlew.bat
├── gradlew
├── README.md
└── LICENSE
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':sample', ':fragment'
2 |
--------------------------------------------------------------------------------
/image/2.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/image/2.gif
--------------------------------------------------------------------------------
/image/3.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/image/3.gif
--------------------------------------------------------------------------------
/image/4.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/image/4.gif
--------------------------------------------------------------------------------
/image/5.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/image/5.gif
--------------------------------------------------------------------------------
/fragment/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /build
5 | /.gradle/
6 | /.idea/
7 | /sample/build/
8 | /fragment/build/
9 |
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-hdpi/ic_back_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-hdpi/ic_back_white.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-hdpi/ic_close_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-hdpi/ic_close_white.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-hdpi/ic_more_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-hdpi/ic_more_white.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xhdpi/ic_back_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xhdpi/ic_back_white.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xhdpi/ic_close_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xhdpi/ic_close_white.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xhdpi/ic_more_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xhdpi/ic_more_white.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xxhdpi/ic_back_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xxhdpi/ic_back_white.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xxhdpi/ic_more_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xxhdpi/ic_more_white.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-hdpi/ic_settings_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-hdpi/ic_settings_white.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xxhdpi/ic_close_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xxhdpi/ic_close_white.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xxxhdpi/ic_back_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xxxhdpi/ic_back_white.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xxxhdpi/ic_close_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xxxhdpi/ic_close_white.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xxxhdpi/ic_more_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xxxhdpi/ic_more_white.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-hdpi/ic_person_add_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-hdpi/ic_person_add_white.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xhdpi/ic_person_add_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xhdpi/ic_person_add_white.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xhdpi/ic_settings_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xhdpi/ic_settings_white.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xxhdpi/ic_settings_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xxhdpi/ic_settings_white.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xxxhdpi/ic_settings_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xxxhdpi/ic_settings_white.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xxhdpi/ic_person_add_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xxhdpi/ic_person_add_white.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xxxhdpi/ic_person_add_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xxxhdpi/ic_person_add_white.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-hdpi/ic_sentiment_neutral_black.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-hdpi/ic_sentiment_neutral_black.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xhdpi/ic_sentiment_neutral_black.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xhdpi/ic_sentiment_neutral_black.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-hdpi/ic_sentiment_satisfied_black.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-hdpi/ic_sentiment_satisfied_black.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xhdpi/ic_sentiment_satisfied_black.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xhdpi/ic_sentiment_satisfied_black.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xxhdpi/ic_sentiment_neutral_black.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xxhdpi/ic_sentiment_neutral_black.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xxhdpi/ic_sentiment_satisfied_black.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xxhdpi/ic_sentiment_satisfied_black.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xxxhdpi/ic_sentiment_neutral_black.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xxxhdpi/ic_sentiment_neutral_black.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-hdpi/ic_sentiment_dissatisfied_black.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-hdpi/ic_sentiment_dissatisfied_black.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xhdpi/ic_sentiment_dissatisfied_black.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xhdpi/ic_sentiment_dissatisfied_black.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xxxhdpi/ic_sentiment_satisfied_black.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xxxhdpi/ic_sentiment_satisfied_black.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xxhdpi/ic_sentiment_dissatisfied_black.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xxhdpi/ic_sentiment_dissatisfied_black.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xxxhdpi/ic_sentiment_dissatisfied_black.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanzhenjie/NoFragment/HEAD/sample/src/main/res/drawable-xxxhdpi/ic_sentiment_dissatisfied_black.png
--------------------------------------------------------------------------------
/sample/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Fri Apr 21 14:20:40 CST 2017
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-3.3-all.zip
7 |
--------------------------------------------------------------------------------
/sample/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 10dp
4 | 10dp
5 | 10dp
6 | 10dp
7 | 10dp
8 |
9 | 16sp
10 | 18sp
11 |
--------------------------------------------------------------------------------
/fragment/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: rootProject.ext.plugins.library
2 |
3 | android {
4 | compileSdkVersion rootProject.ext.android.compileSdkVersion
5 | buildToolsVersion rootProject.ext.android.buildToolsVersion
6 |
7 | defaultConfig {
8 | minSdkVersion rootProject.ext.android.minSdkVersion
9 | targetSdkVersion rootProject.ext.android.targetSdkVersion
10 | }
11 | }
12 |
13 | dependencies {
14 | compile rootProject.ext.dependencies.appcompat
15 | }
16 |
17 | apply from: "maven.gradle"
18 |
--------------------------------------------------------------------------------
/sample/src/main/res/menu/menu_fragment_main.xml:
--------------------------------------------------------------------------------
1 |
16 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
9 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/sample/src/main/res/layout/item.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
15 |
16 |
22 |
23 |
--------------------------------------------------------------------------------
/sample/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/sample/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: rootProject.ext.plugins.application
2 |
3 | android {
4 | compileSdkVersion rootProject.ext.android.compileSdkVersion
5 | buildToolsVersion rootProject.ext.android.buildToolsVersion
6 |
7 | defaultConfig {
8 | applicationId rootProject.ext.android.applicationId
9 | minSdkVersion rootProject.ext.android.minSdkVersion
10 | targetSdkVersion rootProject.ext.android.targetSdkVersion
11 | versionCode rootProject.ext.android.versionCode
12 | versionName rootProject.ext.android.versionName
13 |
14 | compileOptions {
15 | sourceCompatibility JavaVersion.VERSION_1_8
16 | targetCompatibility JavaVersion.VERSION_1_8
17 | }
18 |
19 | jackOptions {
20 | enabled true
21 | }
22 | }
23 | }
24 |
25 | dependencies {
26 | compile rootProject.ext.dependencies.design
27 | compile rootProject.ext.dependencies.fragment
28 | compile rootProject.ext.dependencies.alertdialog
29 | }
30 |
--------------------------------------------------------------------------------
/config.gradle:
--------------------------------------------------------------------------------
1 | ext {
2 | plugins = [
3 | library : 'com.android.library',
4 | application: 'com.android.application',
5 | maven : 'com.github.dcendents.android-maven',
6 | bintray : 'com.jfrog.bintray'
7 | ]
8 |
9 | android = [
10 | // build.
11 | compileSdkVersion: 25,
12 | buildToolsVersion: "25.0.2",
13 |
14 | // defaultConfig.
15 | applicationId : "com.yanzhenjie.fragment.sample",
16 | minSdkVersion : 11,
17 | targetSdkVersion : 25,
18 | versionCode : 11,
19 | versionName : "1.01",
20 | ]
21 |
22 | dependencies = [
23 | // android-support
24 | design : 'com.android.support:design:25.3.1',
25 | appcompat : 'com.android.support:appcompat-v7:25.3.1',
26 | permission : 'com.yanzhenjie:permission:1.0.5',
27 | fragment : 'com.yanzhenjie:fragment:1.0.2',
28 | alertdialog: 'com.yanzhenjie.alertdialog:alertdialog:1.0.0'
29 | ]
30 |
31 | }
--------------------------------------------------------------------------------
/fragment/src/main/java/com/yanzhenjie/fragment/ResultCode.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © Yan Zhenjie. All Rights Reserved
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 com.yanzhenjie.fragment;
17 |
18 | import android.support.annotation.IntDef;
19 |
20 | import java.lang.annotation.Retention;
21 | import java.lang.annotation.RetentionPolicy;
22 |
23 | /**
24 | * Created by Yan Zhenjie on 2017/1/15.
25 | */
26 | @IntDef({NoFragment.RESULT_OK, NoFragment.RESULT_CANCELED})
27 | @Retention(RetentionPolicy.SOURCE)
28 | public @interface ResultCode {
29 | }
30 |
--------------------------------------------------------------------------------
/sample/src/main/res/layout/fragment_menu.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
22 |
23 |
24 |
25 |
31 |
32 |
--------------------------------------------------------------------------------
/sample/src/main/java/com/yanzhenjie/fragment/sample/MainActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © Yan Zhenjie. All Rights Reserved
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 com.yanzhenjie.fragment.sample;
17 |
18 | import android.os.Bundle;
19 |
20 | import com.yanzhenjie.fragment.CompatActivity;
21 | import com.yanzhenjie.fragment.sample.fragment.MainFragment;
22 |
23 |
24 | /**
25 | * Created by Yan Zhenjie on 2017/1/15.
26 | */
27 | public class MainActivity extends CompatActivity {
28 |
29 | @Override
30 | protected void onCreate(Bundle savedInstanceState) {
31 | super.onCreate(savedInstanceState);
32 | setContentView(R.layout.activity_main);
33 |
34 | /*
35 | * 继承CompatActivity后,显示一个fragment就这么简单,不要怀疑自己的眼睛,这是真的。
36 | */
37 | startFragment(MainFragment.class);
38 | }
39 |
40 | @Override
41 | protected int fragmentLayoutId() {
42 | return R.id.fragment_root;
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/sample/src/main/res/layout/fragment_argument.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
22 |
23 |
24 |
25 |
30 |
31 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/sample/src/main/res/menu/menu_fragment_setting.xml:
--------------------------------------------------------------------------------
1 |
37 |
--------------------------------------------------------------------------------
/sample/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | NoFragment
3 | 好的
4 |
5 | MainFragment
6 | 溢出菜单
7 | 传参数
8 | StartFragmentForResult
9 | 不保存回退栈
10 |
11 | 我是标题
12 | 我是内容
13 |
14 | 拦截关闭按钮点击事件
15 | 设置
16 | 退出
17 |
18 | MoreMenuFragment
19 | 不拦截关闭
20 | 添加
21 | 心情
22 | 开心
23 | 难过
24 | 一般
25 |
26 | ArgumentFragment
27 |
28 | StartResultFragment
29 | 结果
30 | 返回数据
31 | 返回
32 | 取消了操作
33 | 您没有填写返回内容
34 |
35 | StackFragment
36 | 打开新的Fragment
37 | 等下返回的时候看不到我了就。
38 |
39 | StackNewFragment
40 | 返回的时候看不到调用我的页面。
41 |
42 |
43 |
--------------------------------------------------------------------------------
/sample/src/main/res/layout/fragment_stack_new.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
22 |
23 |
24 |
25 |
31 |
32 |
37 |
38 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/sample/src/main/res/layout/fragment_stack.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
22 |
23 |
24 |
25 |
31 |
32 |
38 |
39 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/sample/src/main/java/com/yanzhenjie/fragment/sample/adapter/RecyclerAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © Yan Zhenjie. All Rights Reserved
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 com.yanzhenjie.fragment.sample.adapter;
17 |
18 | import android.content.Context;
19 | import android.support.v7.widget.RecyclerView;
20 | import android.view.LayoutInflater;
21 | import android.view.View;
22 | import android.view.ViewGroup;
23 |
24 | import com.yanzhenjie.fragment.sample.R;
25 |
26 | /**
27 | * Created by Yan Zhenjie on 2017/1/15.
28 | */
29 | public class RecyclerAdapter extends RecyclerView.Adapter {
30 |
31 | private LayoutInflater mLayoutInflater;
32 | private int itemCount;
33 |
34 | public RecyclerAdapter(Context context, int itemCount) {
35 | mLayoutInflater = LayoutInflater.from(context);
36 | this.itemCount = itemCount;
37 | }
38 |
39 | @Override
40 | public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
41 | return new ItemViewHolder(mLayoutInflater.inflate(R.layout.item, parent, false));
42 | }
43 |
44 | @Override
45 | public void onBindViewHolder(ItemViewHolder holder, int position) {
46 | }
47 |
48 | @Override
49 | public int getItemCount() {
50 | return itemCount;
51 | }
52 |
53 | static class ItemViewHolder extends RecyclerView.ViewHolder {
54 |
55 | public ItemViewHolder(View itemView) {
56 | super(itemView);
57 | }
58 | }
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/sample/src/main/res/layout/fragment_result.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
22 |
23 |
24 |
25 |
31 |
32 |
35 |
36 |
41 |
42 |
43 |
44 |
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/sample/src/main/java/com/yanzhenjie/fragment/sample/fragment/StackFragment.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © Yan Zhenjie. All Rights Reserved
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 com.yanzhenjie.fragment.sample.fragment;
17 |
18 | import android.os.Bundle;
19 | import android.support.annotation.Nullable;
20 | import android.support.v7.widget.Toolbar;
21 | import android.view.LayoutInflater;
22 | import android.view.View;
23 | import android.view.ViewGroup;
24 | import android.widget.Button;
25 |
26 | import com.yanzhenjie.fragment.NoFragment;
27 | import com.yanzhenjie.fragment.sample.R;
28 |
29 | /**
30 | * Created by Yan Zhenjie on 2017/1/15.
31 | */
32 | public class StackFragment extends NoFragment {
33 |
34 | private Toolbar mToolbar;
35 | private Button mBtnGo;
36 |
37 | @Nullable
38 | @Override
39 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
40 | return inflater.inflate(R.layout.fragment_stack, container, false);
41 | }
42 |
43 | @Override
44 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
45 | mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
46 | mBtnGo = (Button) view.findViewById(R.id.btn_go);
47 | }
48 |
49 | @Override
50 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
51 | super.onActivityCreated(savedInstanceState);
52 |
53 | setToolbar(mToolbar);
54 | setTitle(R.string.title_fragment_stack);
55 | displayHomeAsUpEnabled(R.drawable.ic_back_white);
56 |
57 | mBtnGo.setOnClickListener(v -> startFragment(StackNewFragment.class));
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/sample/src/main/java/com/yanzhenjie/fragment/sample/fragment/StackNewFragment.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © Yan Zhenjie. All Rights Reserved
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 com.yanzhenjie.fragment.sample.fragment;
17 |
18 | import android.os.Bundle;
19 | import android.support.annotation.Nullable;
20 | import android.support.v7.widget.Toolbar;
21 | import android.view.LayoutInflater;
22 | import android.view.View;
23 | import android.view.ViewGroup;
24 | import android.widget.Button;
25 |
26 | import com.yanzhenjie.fragment.NoFragment;
27 | import com.yanzhenjie.fragment.sample.R;
28 |
29 | /**
30 | * Created by Yan Zhenjie on 2017/1/15.
31 | */
32 | public class StackNewFragment extends NoFragment {
33 |
34 | private Toolbar mToolbar;
35 | private Button mBtnBack;
36 |
37 | @Nullable
38 | @Override
39 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
40 | return inflater.inflate(R.layout.fragment_stack_new, container, false);
41 | }
42 |
43 | @Override
44 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
45 | mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
46 | mBtnBack = (Button) view.findViewById(R.id.btn_back);
47 | }
48 |
49 | @Override
50 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
51 | super.onActivityCreated(savedInstanceState);
52 |
53 | setToolbar(mToolbar);
54 | setTitle(R.string.title_fragment_stack_new);
55 | displayHomeAsUpEnabled(R.drawable.ic_back_white);
56 |
57 | mBtnBack.setOnClickListener(v -> finish());
58 | }
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/sample/src/main/res/layout/fragment_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
21 |
22 |
23 |
24 |
29 |
30 |
35 |
36 |
41 |
42 |
48 |
49 |
54 |
55 |
56 |
57 |
--------------------------------------------------------------------------------
/sample/src/main/java/com/yanzhenjie/fragment/sample/fragment/ArgumentFragment.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © Yan Zhenjie. All Rights Reserved
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 com.yanzhenjie.fragment.sample.fragment;
17 |
18 | import android.os.Bundle;
19 | import android.support.annotation.Nullable;
20 | import android.support.v7.widget.Toolbar;
21 | import android.view.LayoutInflater;
22 | import android.view.View;
23 | import android.view.ViewGroup;
24 | import android.widget.TextView;
25 |
26 | import com.yanzhenjie.fragment.NoFragment;
27 | import com.yanzhenjie.fragment.sample.R;
28 |
29 | /**
30 | * Created by Yan Zhenjie on 2017/1/15.
31 | */
32 | public class ArgumentFragment extends NoFragment {
33 |
34 | private Toolbar mToolbar;
35 | private TextView mTvMessage;
36 |
37 | @Nullable
38 | @Override
39 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
40 | return inflater.inflate(R.layout.fragment_argument, container, false);
41 | }
42 |
43 | @Override
44 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
45 | mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
46 | mTvMessage = (TextView) view.findViewById(R.id.tv_message);
47 | }
48 |
49 | @Override
50 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
51 | super.onActivityCreated(savedInstanceState);
52 |
53 | setToolbar(mToolbar);
54 | setTitle(R.string.title_fragment_argument);
55 | displayHomeAsUpEnabled(R.drawable.ic_close_white);
56 |
57 | Bundle bundle = getArguments();
58 | String message = bundle.getString("hehe") + "\r\n";
59 | message += bundle.getString("meng") + "\r\n";
60 | message += bundle.getString("bang") + "\r\n";
61 | message += bundle.getString("meme") + "\r\n";
62 |
63 | mTvMessage.setText(message);
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/fragment/maven.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: rootProject.ext.plugins.maven
2 | apply plugin: rootProject.ext.plugins.bintray
3 |
4 | version = "1.0.2"
5 |
6 | def siteUrl = 'https://github.com/yanzhenjie/NoFragment'
7 | def gitUrl = 'git@github.com:yanzhenjie/NoFragment.git'
8 | group = "com.yanzhenjie"
9 |
10 | install {
11 | repositories.mavenInstaller {
12 | pom {
13 | project {
14 | packaging 'aar'
15 | name 'Fragment for Android'
16 | description 'Fragment for Android'
17 | url siteUrl
18 | licenses {
19 | license {
20 | name 'The Apache Software License, Version 2.0'
21 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
22 | }
23 | }
24 | developers {
25 | developer {
26 | id 'yanzhenjie'
27 | name 'yanzhenjie'
28 | email 'smallajax@foxmail.com'
29 | }
30 | }
31 | scm {
32 | connection gitUrl
33 | developerConnection gitUrl
34 | url siteUrl
35 | }
36 | }
37 | }
38 | }
39 | }
40 | task sourcesJar(type: Jar) {
41 | from android.sourceSets.main.java.srcDirs
42 | classifier = 'sources'
43 | }
44 | task javadoc(type: Javadoc) {
45 | source = android.sourceSets.main.java.srcDirs
46 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
47 | failOnError false
48 | }
49 | task javadocJar(type: Jar, dependsOn: javadoc) {
50 | classifier = 'javadoc'
51 | from javadoc.destinationDir
52 | }
53 | artifacts {
54 | archives javadocJar
55 | archives sourcesJar
56 | }
57 |
58 | Properties properties = new Properties()
59 | properties.load(project.rootProject.file('local.properties').newDataInputStream())
60 | bintray {
61 | user = properties.getProperty("bintray.user")
62 | key = properties.getProperty("bintray.apikey")
63 |
64 | configurations = ['archives']
65 | pkg {
66 | repo = "maven"
67 | name = "fragment"
68 | userOrg = 'yolanda'
69 | websiteUrl = siteUrl
70 | vcsUrl = gitUrl
71 | licenses = ["Apache-2.0"]
72 | publish = true
73 | version {
74 | gpg {
75 | sign = true
76 | passphrase = properties.getProperty("bintray.gpg.password")
77 | }
78 | }
79 | }
80 | }
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/sample/src/main/java/com/yanzhenjie/fragment/sample/fragment/StartResultFragment.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © Yan Zhenjie. All Rights Reserved
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 com.yanzhenjie.fragment.sample.fragment;
17 |
18 | import android.os.Bundle;
19 | import android.support.annotation.Nullable;
20 | import android.support.v7.widget.Toolbar;
21 | import android.text.TextUtils;
22 | import android.view.LayoutInflater;
23 | import android.view.View;
24 | import android.view.ViewGroup;
25 | import android.widget.Button;
26 | import android.widget.EditText;
27 |
28 | import com.yanzhenjie.fragment.NoFragment;
29 | import com.yanzhenjie.fragment.sample.R;
30 |
31 | /**
32 | * Created by Yan Zhenjie on 2017/1/15.
33 | */
34 | public class StartResultFragment extends NoFragment {
35 |
36 | private Toolbar mToolbar;
37 | private EditText mEditText;
38 | private Button mBtnBack;
39 |
40 | @Nullable
41 | @Override
42 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
43 | return inflater.inflate(R.layout.fragment_result, container, false);
44 | }
45 |
46 | @Override
47 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
48 | mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
49 | mEditText = (EditText) view.findViewById(R.id.edit);
50 | mBtnBack = (Button) view.findViewById(R.id.btn_back);
51 | }
52 |
53 | @Override
54 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
55 | super.onActivityCreated(savedInstanceState);
56 |
57 | setToolbar(mToolbar);
58 |
59 | displayHomeAsUpEnabled(R.drawable.ic_back_white);
60 | setTitle(R.string.title_fragment_result);
61 |
62 | mBtnBack.setOnClickListener(v -> {
63 | String result = mEditText.getText().toString();
64 | if (TextUtils.isEmpty(result)) {
65 | result = getString(R.string.message_null);
66 | }
67 | Bundle bundle = new Bundle();
68 | bundle.putString("message", result);
69 | setResult(RESULT_OK, bundle);
70 | finish();
71 | });
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/sample/src/main/java/com/yanzhenjie/fragment/sample/fragment/MoreMenuFragment.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © Yan Zhenjie. All Rights Reserved
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 com.yanzhenjie.fragment.sample.fragment;
17 |
18 | import android.os.Bundle;
19 | import android.support.annotation.Nullable;
20 | import android.support.design.widget.Snackbar;
21 | import android.support.v7.widget.LinearLayoutManager;
22 | import android.support.v7.widget.RecyclerView;
23 | import android.support.v7.widget.Toolbar;
24 | import android.view.LayoutInflater;
25 | import android.view.Menu;
26 | import android.view.MenuInflater;
27 | import android.view.MenuItem;
28 | import android.view.View;
29 | import android.view.ViewGroup;
30 | import android.widget.Toast;
31 |
32 | import com.yanzhenjie.fragment.NoFragment;
33 | import com.yanzhenjie.fragment.sample.R;
34 | import com.yanzhenjie.fragment.sample.adapter.RecyclerAdapter;
35 |
36 | /**
37 | * Created by Yan Zhenjie on 2017/1/15.
38 | */
39 | public class MoreMenuFragment extends NoFragment {
40 |
41 | private Toolbar mToolbar;
42 | private RecyclerView mRecyclerView;
43 |
44 | @Nullable
45 | @Override
46 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
47 | return inflater.inflate(R.layout.fragment_menu, container, false);
48 | }
49 |
50 | @Override
51 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
52 | mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
53 | mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
54 | }
55 |
56 | @Override
57 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
58 | super.onActivityCreated(savedInstanceState);
59 |
60 | // First must set toolbar, will invoke: onCreateOptionsMenu();
61 | setToolbar(mToolbar);
62 |
63 | // Set title for toolbar:
64 | setTitle(R.string.title_fragment_setting);
65 |
66 | // Display close button.
67 | displayHomeAsUpEnabled(R.drawable.ic_back_white);
68 |
69 | mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
70 | mRecyclerView.setAdapter(new RecyclerAdapter(getContext(), 100));
71 | }
72 |
73 | @Override
74 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
75 | // Load your menu.
76 | inflater.inflate(R.menu.menu_fragment_setting, menu);
77 | }
78 |
79 | @Override
80 | public boolean onOptionsItemSelected(MenuItem item) {
81 | // Handle menu item click.
82 | int id = item.getItemId();
83 | switch (id) {
84 | case R.id.action_add_friend: {
85 | Snackbar.make(mRecyclerView, R.string.action_add_friend, Snackbar.LENGTH_SHORT).show();
86 | break;
87 | }
88 | case R.id.action_satisfied: {
89 | Snackbar.make(mRecyclerView, R.string.action_satisfied, Snackbar.LENGTH_SHORT).show();
90 | break;
91 | }
92 | case R.id.action_dissatisfied: {
93 | Snackbar.make(mRecyclerView, R.string.action_dissatisfied, Snackbar.LENGTH_SHORT).show();
94 | break;
95 | }
96 | case R.id.action_neutral: {
97 | Snackbar.make(mRecyclerView, R.string.action_neutral, Snackbar.LENGTH_SHORT).show();
98 | break;
99 | }
100 |
101 | }
102 | return true;
103 | }
104 |
105 | @Override
106 | public boolean onInterceptToolbarBack() {
107 | Toast.makeText(getContext(), R.string.no_intercept_close, Toast.LENGTH_SHORT).show();
108 | return false;
109 | }
110 |
111 | }
112 |
--------------------------------------------------------------------------------
/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 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/sample/src/main/java/com/yanzhenjie/fragment/sample/fragment/MainFragment.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © Yan Zhenjie. All Rights Reserved
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 com.yanzhenjie.fragment.sample.fragment;
17 |
18 | import android.os.Bundle;
19 | import android.support.annotation.Nullable;
20 | import android.support.design.widget.Snackbar;
21 | import android.support.v7.widget.Toolbar;
22 | import android.view.LayoutInflater;
23 | import android.view.Menu;
24 | import android.view.MenuInflater;
25 | import android.view.MenuItem;
26 | import android.view.View;
27 | import android.view.ViewGroup;
28 |
29 | import com.yanzhenjie.alertdialog.AlertDialog;
30 | import com.yanzhenjie.fragment.NoFragment;
31 | import com.yanzhenjie.fragment.sample.R;
32 |
33 | /**
34 | * Created by Yan Zhenjie on 2017/1/15.
35 | */
36 | public class MainFragment extends NoFragment implements View.OnClickListener {
37 |
38 | private Toolbar mToolbar;
39 |
40 | @Nullable
41 | @Override
42 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
43 | return inflater.inflate(R.layout.fragment_main, container, false);
44 | }
45 |
46 | @Override
47 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
48 | mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
49 | view.findViewById(R.id.btn_menu_more).setOnClickListener(this);
50 | view.findViewById(R.id.btn_argument).setOnClickListener(this);
51 | view.findViewById(R.id.btn_for_result).setOnClickListener(this);
52 | view.findViewById(R.id.btn_stack).setOnClickListener(this);
53 | }
54 |
55 | @Override
56 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
57 | super.onActivityCreated(savedInstanceState);
58 |
59 | // First must set toolbar, will invoke: onCreateOptionsMenu();
60 | setToolbar(mToolbar);
61 |
62 | // Set title for toolbar:
63 | setTitle(R.string.title_fragment_main);
64 |
65 | // Display close button.
66 | displayHomeAsUpEnabled(R.drawable.ic_close_white);
67 | }
68 |
69 | @Override
70 | public void onClick(View v) {
71 | int id = v.getId();
72 | switch (id) {
73 | case R.id.btn_menu_more: {
74 | startFragment(MoreMenuFragment.class);
75 | break;
76 | }
77 | case R.id.btn_for_result: {
78 | startFragmentForResquest(StartResultFragment.class, 100);
79 | break;
80 | }
81 | case R.id.btn_argument: {
82 | Bundle bundle = new Bundle();
83 | bundle.putString("hehe", "呵呵哒");
84 | bundle.putString("meng", "萌萌哒");
85 | bundle.putString("bang", "棒棒哒");
86 | bundle.putString("meme", "么么哒");
87 |
88 | // Create fragment_menu for bundle.
89 | NoFragment fragment = fragment(ArgumentFragment.class, bundle);
90 |
91 | startFragment(fragment);
92 | break;
93 | }
94 | case R.id.btn_stack: {
95 | // Second argument false: don't join the back stack.
96 | startFragment(StackFragment.class, false);
97 | break;
98 | }
99 | }
100 | }
101 |
102 | @Override
103 | public void onFragmentResult(int requestCode, int resultCode, @Nullable Bundle result) {
104 | switch (requestCode) {
105 | case 100: {
106 | if (resultCode == RESULT_OK) {
107 | String message = result.getString("message");
108 | AlertDialog.build(getContext())
109 | .setCancelable(true)
110 | .setTitle(R.string.result)
111 | .setMessage(message)
112 | .setPositiveButton(R.string.ok, (dialog, which) -> {
113 | // TODO nothing.
114 | })
115 | .show();
116 | } else if (resultCode == RESULT_CANCELED) {
117 | Snackbar.make(mToolbar, R.string.message_canceled, Snackbar.LENGTH_SHORT).show();
118 | }
119 | break;
120 | }
121 | }
122 | }
123 |
124 | // ========================= Menu Sample ========================= //
125 |
126 | @Override
127 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
128 | // Load your menu.
129 | inflater.inflate(R.menu.menu_fragment_main, menu);
130 | }
131 |
132 | @Override
133 | public boolean onOptionsItemSelected(MenuItem item) {
134 | // Handle menu item click.
135 | int id = item.getItemId();
136 | switch (id) {
137 | case R.id.action_settings: {
138 | Snackbar.make(mToolbar, R.string.action_settings, Snackbar.LENGTH_SHORT).show();
139 | break;
140 | }
141 | case R.id.action_exit: {
142 | Snackbar.make(mToolbar, R.string.action_exit, Snackbar.LENGTH_SHORT).show();
143 | break;
144 | }
145 | }
146 | return true;
147 | }
148 |
149 | // ========================= Close Button ========================= //
150 |
151 | @Override
152 | public boolean onInterceptToolbarBack() {
153 | // Intercept close button click event.
154 | Snackbar.make(mToolbar, R.string.intercept_close, Snackbar.LENGTH_SHORT).show();
155 | return true;
156 | }
157 | }
158 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 严振杰的主页:[http://www.yanzhenjie.com](http://www.yanzhenjie.com)
2 | 严振杰的博客:[http://blog.yanzhenjie.com](http://blog.yanzhenjie.com)
3 |
4 | **欢迎加入QQ技术交流群:[46523908](http://jq.qq.com/?_wv=1027&k=40hvC7E)**
5 |
6 | 关于Fragment、NoFragment的使用看博客:[【2016Ending,2017Starting】NoFragment之Fragment玩法新姿势](http://blog.csdn.net/yanzhenjie1003/article/details/54562328)。
7 |
8 | # 特点
9 | 1. 支持传统`Fragment`的所有用法。
10 | 2. 支持`startFragmentForResult(Fragment)`、`onFragmentResult(int, int, Bundle)`,原生只有Activity。
11 | 3. 支持同一个`Fragment`启动多个实例。
12 | 5. 支持自动维护`Back Stack`,不会错乱。
13 | 6. 支持在`Fragment`中直接`setToolbar()`、`setTitle()`、`displayHomeButton()`。
14 | 7. 返回键和`homeButton`自动处理,支持开发者拦截处理。
15 | 8. 支持`ActionBar Menu`、溢出`Menu`等。
16 | 9. 开发者不用管跳转逻辑、back键处理、Toolbar加载菜单等。
17 |
18 | # 使用方法
19 | * Gradle一句话远程依赖
20 | ```groovy
21 | compile 'com.yanzhenjie:fragment:1.0.1'
22 | ```
23 | * Maven:
24 | ```xml
25 |
26 | com.yanzhenjie
27 | fragment
28 | 1.0.1
29 | pom
30 |
31 | ```
32 |
33 | * Eclipse ADT
34 | 请放弃治疗。
35 |
36 | # 图示
37 | 第一种,结合ToolBar、Menu的演示:
38 |
39 |
40 |
41 | 第二种,结合Toolbar、Menu + OverFlower的演示:
42 |
43 |
44 |
45 | 第三种,`startFragmentForResult()`、`onFragmentResult()`演示:
46 |
47 |
48 |
49 | 第四种,不保存的在回退栈的演示:
50 |
51 |
52 |
53 | # 代码展示
54 | 你的宿主`Activity`需要继承`CompatActivity`,然后启动一个`Fragment`:
55 | ```java
56 | public class MainActivity extends CompatActivity {
57 |
58 | @Override
59 | protected void onCreate(Bundle savedInstanceState) {
60 | super.onCreate(savedInstanceState);
61 | setContentView(R.layout.activity_main);
62 |
63 | /*
64 | * 一句话即可,不要怀疑自己的眼睛,这是真的。
65 | */
66 | startFragment(MainFragment.class);
67 | }
68 |
69 | @Override
70 | protected int fragmentLayoutId() {
71 | return R.id.fragment_root;
72 | }
73 |
74 | }
75 | ```
76 |
77 | 之后在Fragment中互相跳转,你可以不用管物理Back键被按下之类的:
78 |
79 | ## 一、以`standard`模式启动一个`Fragment`
80 | ```java
81 | startFragment(MoreMenuFragment.class);
82 | ```
83 |
84 | ## 二、以`startActivityForResult()`方式启动一个`Fragment`
85 | ```java
86 | // 启动,等待回调结果。
87 | startFragmentForResult(StartResultFragment.class, 100);
88 |
89 | // 不论怎样回来都会回调onFragmentResult()。
90 | @Override
91 | public void onFragmentResult(int requestCode, int resultCode, @Nullable Bundle result) {
92 | switch (requestCode) {
93 | case 100: {
94 | if (resultCode == RESULT_OK) {
95 | // 操作成功:result就是调用的Fragment返回的结果。
96 | } else if (resultCode == RESULT_CANCELED) {
97 | // 操作取消。
98 | }
99 | break;
100 | }
101 | }
102 | }
103 | ```
104 |
105 | 在`StartResultFragment`中如果要返回结果,那么:
106 | ```java
107 | Bundle bundle = new Bundle();
108 | bundle.putString("message", result);
109 | setResult(RESULT_OK, bundle);
110 | finish();
111 | ```
112 |
113 | 当然你也不设置,那么`resultCode`的默认值是`RESULT_CANCELED`。
114 |
115 | ## 三、跳转时带参数
116 | ```java
117 | // 封装参数:
118 | Bundle bundle = new Bundle();
119 | bundle.putString("hehe", "呵呵哒");
120 | bundle.putString("meng", "萌萌哒");
121 | bundle.putString("bang", "棒棒哒");
122 | bundle.putString("meme", "么么哒");
123 |
124 | // 在Activity中或者Fragment调用此方法:
125 | NoFragment fragment = fragment(ArgumentFragment.class, bundle);
126 |
127 | // 最后启动:
128 | startFragment(fragment);
129 | ```
130 |
131 | ## 四、跳转的`Fragment`不保存在`Back Stack`
132 | 这种方式显示的`fragment`中如果调用了其它`fragment`,从其它`fragment`中回来时,这个`fragment`将会跳过,不会显示,也就是说:A-B-C-[back]-A,从A到B,B不加入回退栈,B再到C,C按下返回键,或者调用`finish()`方法,将会直接回到A。
133 | ```java
134 | startFragment(StackFragment.class, false);
135 | ```
136 |
137 | ## 五、同一个Fragment,启动多个实例
138 | ```java
139 | startFragment(MoreMenuFragment.class);
140 | startFragment(MoreMenuFragment.class);
141 | startFragment(MoreMenuFragment.class);
142 | startFragment(MoreMenuFragment.class);
143 | ```
144 | 比如我们这里调用四次,那么回退栈中有四个`MoreMenuFragment`,按下返回键时将一个个退出。
145 |
146 | ## 六、Toolbar菜单的加载和处理
147 | 我们知道MD设计中,Toolbar的菜单很好看,而且利用Toolbar也很好加载,那么NoFragment也是完美支持的,当重写了`onCreateOptionsMenu()`方法后,调用`setToolbar(Toolbar)`方法时,将会调用`onCreateOptionsMenu()`方法,此时你就该加载菜单了,当然也只需要一句话。
148 |
149 | ```java
150 | @Override
151 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
152 | // Load your menu.
153 | inflater.inflate(R.menu.menu_fragment_main, menu);
154 | }
155 | ```
156 |
157 | 当用户点击meun的item时将会回调这个方法,和原生Activity是一样的。
158 | ```java
159 | @Override
160 | public boolean onOptionsItemSelected(MenuItem item) {
161 | // Handle menu item click.
162 | int id = item.getItemId();
163 | switch (id) {
164 | case R.id.action_settings: {
165 | Snackbar.make(mToolbar, R.string.action_settings, Snackbar.LENGTH_SHORT).show();
166 | break;
167 | }
168 | case R.id.action_exit: {
169 | Snackbar.make(mToolbar, R.string.action_exit, Snackbar.LENGTH_SHORT).show();
170 | break;
171 | }
172 | }
173 | return true;
174 | }
175 | ```
176 |
177 | ## 七、Toolbar的返回按钮的处理
178 | 在正常开发中给Toolbar设置返回按钮也要好几行代码的,如果使用了NoFragment,那么:
179 | ```java
180 | @Override
181 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
182 | super.onActivityCreated(savedInstanceState);
183 |
184 | // 首先设置Toolbar:
185 | setToolbar(mToolbar);
186 |
187 | // 设置标题:
188 | setTitle(R.string.title_fragment_main);
189 |
190 | // 显示返回按钮,图标开发者指定:
191 | displayHomeAsUpEnabled(R.drawable.ic_close_white);
192 | }
193 | ```
194 |
195 | 设置了返回按钮后,用户点击返回按钮将自动杀死当前`Fragment`,当然你也可以拦截用户的返回行为:
196 | ```java
197 | @Override
198 | public boolean onInterceptToolbarBack() {
199 | // 返回true将拦截,返回false将不拦截。
200 | return true;
201 | }
202 | ```
203 |
204 | # 混淆
205 | ```text
206 | -keep public class * extends android.support.v4.app.Fragment
207 | ```
208 |
209 | # License
210 | ```text
211 | Copyright 2017 Yan Zhenjie
212 |
213 | Licensed under the Apache License, Version 2.0 (the "License");
214 | you may not use this file except in compliance with the License.
215 | You may obtain a copy of the License at
216 |
217 | http://www.apache.org/licenses/LICENSE-2.0
218 |
219 | Unless required by applicable law or agreed to in writing, software
220 | distributed under the License is distributed on an "AS IS" BASIS,
221 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
222 | See the License for the specific language governing permissions and
223 | limitations under the License.
224 | ```
--------------------------------------------------------------------------------
/fragment/src/main/java/com/yanzhenjie/fragment/CompatActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © Yan Zhenjie. All Rights Reserved
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 com.yanzhenjie.fragment;
17 |
18 | import android.os.Bundle;
19 | import android.support.annotation.IdRes;
20 | import android.support.annotation.Nullable;
21 | import android.support.v4.app.Fragment;
22 | import android.support.v4.app.FragmentManager;
23 | import android.support.v4.app.FragmentTransaction;
24 | import android.support.v7.app.AppCompatActivity;
25 |
26 | import java.util.ArrayList;
27 | import java.util.HashMap;
28 | import java.util.List;
29 | import java.util.Map;
30 | import java.util.concurrent.atomic.AtomicInteger;
31 |
32 | /**
33 | * Must extends CompatActivity.
34 | * Created by Yan Zhenjie on 2017/1/13.
35 | */
36 | public abstract class CompatActivity extends AppCompatActivity {
37 |
38 | public static final int REQUEST_CODE_INVALID = -1;
39 |
40 | private FragmentManager mFManager;
41 | private AtomicInteger mAtomicInteger = new AtomicInteger();
42 | private List mFragmentStack = new ArrayList<>();
43 | private Map mFragmentEntityMap = new HashMap<>();
44 |
45 | static class FragmentStackEntity {
46 | private FragmentStackEntity() {
47 | }
48 |
49 | private boolean isSticky = false;
50 | private int requestCode = REQUEST_CODE_INVALID;
51 | @ResultCode
52 | int resultCode = RESULT_CANCELED;
53 | Bundle result = null;
54 | }
55 |
56 | public final T fragment(Class fragmentClass) {
57 | //noinspection unchecked
58 | return (T) Fragment.instantiate(this, fragmentClass.getName());
59 | }
60 |
61 | public final T fragment(Class fragmentClass, Bundle bundle) {
62 | //noinspection unchecked
63 | return (T) Fragment.instantiate(this, fragmentClass.getName(), bundle);
64 | }
65 |
66 | @Override
67 | protected void onCreate(@Nullable Bundle savedInstanceState) {
68 | super.onCreate(savedInstanceState);
69 | mFManager = getSupportFragmentManager();
70 | }
71 |
72 | /**
73 | * Show a fragment.
74 | *
75 | * @param clazz fragment class.
76 | */
77 | public final void startFragment(Class clazz) {
78 | try {
79 | NoFragment targetFragment = clazz.newInstance();
80 | startFragment(null, targetFragment, true, REQUEST_CODE_INVALID);
81 | } catch (Exception e) {
82 | e.printStackTrace();
83 | }
84 | }
85 |
86 | /**
87 | * Show a fragment.
88 | *
89 | * @param clazz fragment class.
90 | * @param stickyStack sticky to back stack.
91 | */
92 | public final void startFragment(Class clazz, boolean stickyStack) {
93 | try {
94 | NoFragment targetFragment = clazz.newInstance();
95 | startFragment(null, targetFragment, stickyStack, REQUEST_CODE_INVALID);
96 | } catch (Exception e) {
97 | e.printStackTrace();
98 | }
99 | }
100 |
101 | /**
102 | * Show a fragment.
103 | *
104 | * @param targetFragment fragment to display.
105 | * @param {@link NoFragment}.
106 | */
107 | public final void startFragment(T targetFragment) {
108 | startFragment(null, targetFragment, true, REQUEST_CODE_INVALID);
109 | }
110 |
111 | /**
112 | * Show a fragment.
113 | *
114 | * @param targetFragment fragment to display.
115 | * @param stickyStack sticky back stack.
116 | * @param {@link NoFragment}.
117 | */
118 | public final void startFragment(T targetFragment, boolean stickyStack) {
119 | startFragment(null, targetFragment, stickyStack, REQUEST_CODE_INVALID);
120 | }
121 |
122 | /**
123 | * Show a fragment for result.
124 | *
125 | * @param clazz fragment to display.
126 | * @param requestCode requestCode.
127 | * @param {@link NoFragment}.
128 | * @deprecated use {@link #startFragmentForResult(Class, int)} instead.
129 | */
130 | @Deprecated
131 | public final void startFragmentForResquest(Class clazz, int requestCode) {
132 | startFragmentForResult(clazz, requestCode);
133 | }
134 |
135 | /**
136 | * Show a fragment for result.
137 | *
138 | * @param targetFragment fragment to display.
139 | * @param requestCode requestCode.
140 | * @param {@link NoFragment}.
141 | * @deprecated use {@link #startFragmentForResult(NoFragment, int)} instead.
142 | */
143 | @Deprecated
144 | public final void startFragmentForResquest(T targetFragment, int requestCode) {
145 | startFragmentForResult(targetFragment, requestCode);
146 | }
147 |
148 | /**
149 | * Show a fragment for result.
150 | *
151 | * @param clazz fragment to display.
152 | * @param requestCode requestCode.
153 | * @param {@link NoFragment}.
154 | */
155 | public final void startFragmentForResult(Class clazz, int requestCode) {
156 | if (requestCode == REQUEST_CODE_INVALID)
157 | throw new IllegalArgumentException("The requestCode must be positive integer.");
158 | try {
159 | NoFragment targetFragment = clazz.newInstance();
160 | startFragment(null, targetFragment, true, requestCode);
161 | } catch (Exception e) {
162 | e.printStackTrace();
163 | }
164 | }
165 |
166 | /**
167 | * Show a fragment for result.
168 | *
169 | * @param targetFragment fragment to display.
170 | * @param requestCode requestCode.
171 | * @param {@link NoFragment}.
172 | */
173 | public final void startFragmentForResult(T targetFragment, int requestCode) {
174 | if (requestCode == REQUEST_CODE_INVALID)
175 | throw new IllegalArgumentException("The requestCode must be positive integer.");
176 | startFragment(null, targetFragment, true, requestCode);
177 | }
178 |
179 | /**
180 | * Show a fragment.
181 | *
182 | * @param thisFragment Now show fragment, can be null.
183 | * @param thatFragment fragment to display.
184 | * @param stickyStack sticky back stack.
185 | * @param requestCode requestCode.
186 | * @param {@link NoFragment}.
187 | */
188 | protected final void startFragment(T thisFragment, T thatFragment,
189 | boolean stickyStack, int requestCode) {
190 | FragmentTransaction fragmentTransaction = mFManager.beginTransaction();
191 | if (thisFragment != null) {
192 | FragmentStackEntity thisStackEntity = mFragmentEntityMap.get(thisFragment);
193 | if (thisStackEntity != null) {
194 | if (thisStackEntity.isSticky) {
195 | thisFragment.onPause();
196 | thisFragment.onStop();
197 | fragmentTransaction.hide(thisFragment);
198 | } else {
199 | fragmentTransaction.remove(thisFragment).commit();
200 | fragmentTransaction.commitNow();
201 | fragmentTransaction = mFManager.beginTransaction();
202 |
203 | mFragmentEntityMap.remove(thisFragment);
204 | mFragmentStack.remove(thisFragment);
205 | }
206 | }
207 | }
208 |
209 | String fragmentTag = thatFragment.getClass().getSimpleName() + mAtomicInteger.incrementAndGet();
210 | fragmentTransaction.add(fragmentLayoutId(), thatFragment, fragmentTag);
211 | fragmentTransaction.addToBackStack(fragmentTag);
212 | fragmentTransaction.commit();
213 |
214 | FragmentStackEntity fragmentStackEntity = new FragmentStackEntity();
215 | fragmentStackEntity.isSticky = stickyStack;
216 | fragmentStackEntity.requestCode = requestCode;
217 | thatFragment.setStackEntity(fragmentStackEntity);
218 | mFragmentEntityMap.put(thatFragment, fragmentStackEntity);
219 |
220 | mFragmentStack.add(thatFragment);
221 | }
222 |
223 | /**
224 | * When the back off.
225 | */
226 | protected final boolean onBackStackFragment() {
227 | if (mFragmentStack.size() > 1) {
228 | mFManager.popBackStack();
229 | NoFragment inFragment = mFragmentStack.get(mFragmentStack.size() - 2);
230 |
231 | FragmentTransaction fragmentTransaction = mFManager.beginTransaction();
232 | fragmentTransaction.show(inFragment);
233 | fragmentTransaction.commit();
234 |
235 | NoFragment outFragment = mFragmentStack.get(mFragmentStack.size() - 1);
236 | inFragment.onResume();
237 |
238 | FragmentStackEntity stackEntity = mFragmentEntityMap.get(outFragment);
239 | mFragmentStack.remove(outFragment);
240 | mFragmentEntityMap.remove(outFragment);
241 |
242 | if (stackEntity.requestCode != REQUEST_CODE_INVALID) {
243 | inFragment.onFragmentResult(stackEntity.requestCode, stackEntity.resultCode, stackEntity.result);
244 | }
245 | return true;
246 | }
247 | return false;
248 | }
249 |
250 | @Override
251 | public void onBackPressed() {
252 | if (!onBackStackFragment()) {
253 | finish();
254 | }
255 | }
256 |
257 | /**
258 | * Should be returned to display fragments id of {@link android.view.ViewGroup}.
259 | *
260 | * @return resource id of {@link android.view.ViewGroup}.
261 | */
262 | protected abstract
263 | @IdRes
264 | int fragmentLayoutId();
265 |
266 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright 2017 Yan Zhenjie
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
203 |
--------------------------------------------------------------------------------
/fragment/src/main/java/com/yanzhenjie/fragment/NoFragment.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright © Yan Zhenjie. All Rights Reserved
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 com.yanzhenjie.fragment;
17 |
18 | import android.app.Activity;
19 | import android.content.Context;
20 | import android.content.Intent;
21 | import android.graphics.drawable.Drawable;
22 | import android.os.Bundle;
23 | import android.support.annotation.DrawableRes;
24 | import android.support.annotation.NonNull;
25 | import android.support.annotation.Nullable;
26 | import android.support.v4.app.Fragment;
27 | import android.support.v4.content.ContextCompat;
28 | import android.support.v7.view.SupportMenuInflater;
29 | import android.support.v7.widget.Toolbar;
30 | import android.view.MenuItem;
31 | import android.view.View;
32 |
33 | /**
34 | * Created by Yan Zhenjie on 2017/1/13.
35 | */
36 | public class NoFragment extends Fragment {
37 |
38 | public static final int RESULT_OK = Activity.RESULT_OK;
39 | public static final int RESULT_CANCELED = Activity.RESULT_CANCELED;
40 |
41 | private static final int REQUEST_CODE_INVALID = CompatActivity.REQUEST_CODE_INVALID;
42 |
43 | /**
44 | * Create a new instance of a Fragment with the given class name. This is the same as calling its empty constructor.
45 | *
46 | * @param context context.
47 | * @param fragmentClass class of fragment.
48 | * @param subclass of {@link NoFragment}.
49 | * @return new instance.
50 | * @deprecated In {@code Activity} with {@link CompatActivity#fragment(Class)} instead;
51 | * in the {@code Fragment} width {@link #fragment(Class)} instead.
52 | */
53 | @Deprecated
54 | public static T instantiate(Context context, Class fragmentClass) {
55 | //noinspection unchecked
56 | return (T) instantiate(context, fragmentClass.getName(), null);
57 | }
58 |
59 | /**
60 | * Create a new instance of a Fragment with the given class name. This is the same as calling its empty constructor.
61 | *
62 | * @param context context.
63 | * @param fragmentClass class of fragment.
64 | * @param bundle argument.
65 | * @param subclass of {@link NoFragment}.
66 | * @return new instance.
67 | * @deprecated In {@code Activity} with {@link CompatActivity#fragment(Class, Bundle)} instead;
68 | * in the {@code Fragment} width {@link #fragment(Class, Bundle)} instead.
69 | */
70 | @Deprecated
71 | public static T instantiate(Context context, Class fragmentClass, Bundle bundle) {
72 | //noinspection unchecked
73 | return (T) instantiate(context, fragmentClass.getName(), bundle);
74 | }
75 |
76 | /**
77 | * Create a new instance of a Fragment with the given class name. This is the same as calling its empty constructor.
78 | *
79 | * @param fragmentClass class of fragment.
80 | * @param subclass of {@link NoFragment}.
81 | * @return new instance.
82 | */
83 | public final T fragment(Class fragmentClass) {
84 | //noinspection unchecked
85 | return (T) instantiate(getContext(), fragmentClass.getName(), null);
86 | }
87 |
88 | /**
89 | * Create a new instance of a Fragment with the given class name. This is the same as calling its empty constructor.
90 | *
91 | * @param fragmentClass class of fragment.
92 | * @param bundle argument.
93 | * @param subclass of {@link NoFragment}.
94 | * @return new instance.
95 | */
96 | public final T fragment(Class fragmentClass, Bundle bundle) {
97 | //noinspection unchecked
98 | return (T) instantiate(getContext(), fragmentClass.getName(), bundle);
99 | }
100 |
101 | /**
102 | * Toolbar.
103 | */
104 | private Toolbar mToolbar;
105 |
106 | /**
107 | * CompatActivity.
108 | */
109 | private CompatActivity mActivity;
110 |
111 | /**
112 | * Get BaseActivity.
113 | *
114 | * @return {@link CompatActivity}.
115 | */
116 | protected final CompatActivity getCompatActivity() {
117 | return mActivity;
118 | }
119 |
120 | /**
121 | * Start activity.
122 | *
123 | * @param clazz class for activity.
124 | * @param {@link Activity}.
125 | */
126 | protected final void startActivity(Class clazz) {
127 | startActivity(new Intent(mActivity, clazz));
128 | }
129 |
130 | /**
131 | * Start activity and finish my parent.
132 | *
133 | * @param clazz class for activity.
134 | * @param {@link Activity}.
135 | */
136 | protected final void startActivityFinish(Class clazz) {
137 | startActivity(new Intent(mActivity, clazz));
138 | mActivity.finish();
139 | }
140 |
141 | @Override
142 | public void onAttach(Context context) {
143 | super.onAttach(context);
144 | mActivity = (CompatActivity) context;
145 | }
146 |
147 | /**
148 | * Destroy me.
149 | */
150 | public void finish() {
151 | mActivity.onBackPressed();
152 | }
153 |
154 | /**
155 | * Set Toolbar.
156 | *
157 | * @param toolbar {@link Toolbar}.
158 | */
159 | public final void setToolbar(@NonNull Toolbar toolbar) {
160 | this.mToolbar = toolbar;
161 | onCreateOptionsMenu(mToolbar.getMenu(), new SupportMenuInflater(mActivity));
162 | mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
163 | @Override
164 | public boolean onMenuItemClick(MenuItem item) {
165 | return onOptionsItemSelected(item);
166 | }
167 | });
168 | }
169 |
170 | /**
171 | * Display home up button.
172 | *
173 | * @param drawableId drawable id.
174 | */
175 | public final void displayHomeAsUpEnabled(@DrawableRes int drawableId) {
176 | displayHomeAsUpEnabled(ContextCompat.getDrawable(mActivity, drawableId));
177 | }
178 |
179 | /**
180 | * Display home up button.
181 | *
182 | * @param drawable {@link Drawable}.
183 | */
184 | public final void displayHomeAsUpEnabled(Drawable drawable) {
185 | mToolbar.setNavigationIcon(drawable);
186 | mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
187 | @Override
188 | public void onClick(View view) {
189 | if (!onInterceptToolbarBack())
190 | finish();
191 | }
192 | });
193 | }
194 |
195 | /**
196 | * Override this method, intercept backPressed of ToolBar.
197 | *
198 | * @return true, other wise false.
199 | */
200 | public boolean onInterceptToolbarBack() {
201 | return false;
202 | }
203 |
204 | /**
205 | * Get Toolbar.
206 | *
207 | * @return {@link Toolbar}.
208 | */
209 | protected final
210 | @Nullable
211 | Toolbar getToolbar() {
212 | return mToolbar;
213 | }
214 |
215 | /**
216 | * Set title.
217 | *
218 | * @param title title.
219 | */
220 | protected void setTitle(CharSequence title) {
221 | if (mToolbar != null)
222 | mToolbar.setTitle(title);
223 | }
224 |
225 | /**
226 | * Set title.
227 | *
228 | * @param titleId string resource id from {@code string.xml}.
229 | */
230 | protected void setTitle(int titleId) {
231 | if (mToolbar != null)
232 | mToolbar.setTitle(titleId);
233 | }
234 |
235 | /**
236 | * Set sub title.
237 | *
238 | * @param title sub title.
239 | */
240 | protected void setSubtitle(CharSequence title) {
241 | if (mToolbar != null)
242 | mToolbar.setSubtitle(title);
243 | }
244 |
245 | /**
246 | * Set sub title.
247 | *
248 | * @param titleId string resource id from {@code string.xml}.
249 | */
250 | protected void setSubtitle(int titleId) {
251 | if (mToolbar != null)
252 | mToolbar.setSubtitle(titleId);
253 | }
254 |
255 | // ------------------------- Stack ------------------------- //
256 |
257 | /**
258 | * Stack info.
259 | */
260 | private CompatActivity.FragmentStackEntity mStackEntity;
261 |
262 | /**
263 | * Set result.
264 | *
265 | * @param resultCode result code, one of {@link NoFragment#RESULT_OK}, {@link NoFragment#RESULT_CANCELED}.
266 | */
267 | protected final void setResult(@ResultCode int resultCode) {
268 | mStackEntity.resultCode = resultCode;
269 | }
270 |
271 | /**
272 | * Set result.
273 | *
274 | * @param resultCode resultCode, use {@link }.
275 | * @param result {@link Bundle}.
276 | */
277 | protected final void setResult(@ResultCode int resultCode, @NonNull Bundle result) {
278 | mStackEntity.resultCode = resultCode;
279 | mStackEntity.result = result;
280 | }
281 |
282 | /**
283 | * Get the resultCode for requestCode.
284 | */
285 | final void setStackEntity(@NonNull CompatActivity.FragmentStackEntity stackEntity) {
286 | this.mStackEntity = stackEntity;
287 | }
288 |
289 | /**
290 | * You should override it.
291 | *
292 | * @param resultCode resultCode.
293 | * @param result {@link Bundle}.
294 | */
295 | public void onFragmentResult(int requestCode, @ResultCode int resultCode, @Nullable Bundle result) {
296 | }
297 |
298 | /**
299 | * Show a fragment.
300 | *
301 | * @param clazz fragment class.
302 | * @param {@link NoFragment}.
303 | */
304 | public final void startFragment(Class clazz) {
305 | try {
306 | NoFragment targetFragment = clazz.newInstance();
307 | startFragment(targetFragment, true, REQUEST_CODE_INVALID);
308 | } catch (Exception e) {
309 | e.printStackTrace();
310 | }
311 | }
312 |
313 | /**
314 | * Show a fragment.
315 | *
316 | * @param clazz fragment class.
317 | * @param stickyStack sticky to back stack.
318 | * @param {@link NoFragment}.
319 | */
320 | public final void startFragment(Class clazz, boolean stickyStack) {
321 | try {
322 | NoFragment targetFragment = clazz.newInstance();
323 | startFragment(targetFragment, stickyStack, REQUEST_CODE_INVALID);
324 | } catch (Exception e) {
325 | e.printStackTrace();
326 | }
327 | }
328 |
329 | /**
330 | * Show a fragment.
331 | *
332 | * @param targetFragment fragment to display.
333 | * @param {@link NoFragment}.
334 | */
335 | public final void startFragment(T targetFragment) {
336 | startFragment(targetFragment, true, REQUEST_CODE_INVALID);
337 | }
338 |
339 | /**
340 | * Show a fragment.
341 | *
342 | * @param targetFragment fragment to display.
343 | * @param stickyStack sticky back stack.
344 | * @param {@link NoFragment}.
345 | */
346 | public final void startFragment(T targetFragment, boolean stickyStack) {
347 | startFragment(targetFragment, stickyStack, REQUEST_CODE_INVALID);
348 | }
349 |
350 | /**
351 | * Show a fragment for result.
352 | *
353 | * @param clazz fragment to display.
354 | * @param requestCode requestCode.
355 | * @param {@link NoFragment}.
356 | * @deprecated use {@link #startFragmentForResult(Class, int)} instead.
357 | */
358 | @Deprecated
359 | public final void startFragmentForResquest(Class clazz, int requestCode) {
360 | startFragmentForResult(clazz, requestCode);
361 | }
362 |
363 | /**
364 | * Show a fragment for result.
365 | *
366 | * @param targetFragment fragment to display.
367 | * @param requestCode requestCode.
368 | * @param {@link NoFragment}.
369 | * @deprecated use {@link #startFragmentForResult(Class, int)} instead.
370 | */
371 | @Deprecated
372 | public final void startFragmentForResquest(T targetFragment, int requestCode) {
373 | startFragmentForResult(targetFragment, requestCode);
374 | }
375 |
376 | /**
377 | * Show a fragment for result.
378 | *
379 | * @param clazz fragment to display.
380 | * @param requestCode requestCode.
381 | * @param {@link NoFragment}.
382 | */
383 | public final void startFragmentForResult(Class clazz, int requestCode) {
384 | try {
385 | NoFragment targetFragment = clazz.newInstance();
386 | startFragment(targetFragment, true, requestCode);
387 | } catch (Exception e) {
388 | e.printStackTrace();
389 | }
390 | }
391 |
392 | /**
393 | * Show a fragment for result.
394 | *
395 | * @param targetFragment fragment to display.
396 | * @param requestCode requestCode.
397 | * @param {@link NoFragment}.
398 | */
399 | public final void startFragmentForResult(T targetFragment, int requestCode) {
400 | startFragment(targetFragment, true, requestCode);
401 | }
402 |
403 | /**
404 | * Show a fragment.
405 | *
406 | * @param targetFragment fragment to display.
407 | * @param stickyStack sticky back stack.
408 | * @param requestCode requestCode.
409 | * @param {@link NoFragment}.
410 | */
411 | private void startFragment(T targetFragment, boolean stickyStack, int requestCode) {
412 | mActivity.startFragment(this, targetFragment, stickyStack, requestCode);
413 | }
414 |
415 | }
416 |
--------------------------------------------------------------------------------