├── .gitignore
├── LICENSE
├── README.md
├── build.gradle
├── fragment.swapper.sample.app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── pl
│ │ └── openrnd
│ │ └── managers
│ │ └── fragmentswapper
│ │ └── sample
│ │ └── ApplicationTest.java
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── pl
│ │ └── openrnd
│ │ └── managers
│ │ └── fragmentsswapper
│ │ └── sample
│ │ ├── MainActivity.java
│ │ ├── SampleScreenManager.java
│ │ └── fragment
│ │ ├── BaseScreenFragment.java
│ │ ├── Screen1Fragment.java
│ │ ├── Screen2Fragment.java
│ │ ├── Screen3Fragment.java
│ │ └── Screen4Fragment.java
│ └── res
│ ├── drawable-hdpi
│ └── ic_launcher.png
│ ├── drawable-mdpi
│ └── ic_launcher.png
│ ├── drawable-xhdpi
│ └── ic_launcher.png
│ ├── drawable-xxhdpi
│ └── ic_launcher.png
│ ├── layout
│ ├── activity_main.xml
│ └── fragment_screen.xml
│ ├── values-w820dp
│ └── dimens.xml
│ └── values
│ ├── colors.xml
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
├── fragment.swapper
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── pl
│ │ └── openrnd
│ │ └── managers
│ │ └── fragmentsswapper
│ │ ├── BaseFragment.java
│ │ ├── FragmentDescriptor.java
│ │ ├── FragmentDescriptorImpl.java
│ │ ├── FragmentSwapper.java
│ │ ├── InitializationParams.java
│ │ ├── OnFragmentSwapperListener.java
│ │ ├── PopParams.java
│ │ ├── ScreenManager.java
│ │ ├── SingleContainerFragmentSwapper.java
│ │ └── SwapParams.java
│ └── res
│ ├── anim
│ ├── slide_down_in.xml
│ ├── slide_down_out.xml
│ ├── slide_left_in.xml
│ ├── slide_left_out.xml
│ ├── slide_right_in.xml
│ ├── slide_right_out.xml
│ ├── slide_up_in.xml
│ └── slide_up_out.xml
│ └── values
│ └── integers.xml
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | #built application files
2 | *.apk
3 | *.ap_
4 |
5 | # files for the dex VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # generated files
12 | bin/
13 | gen/
14 |
15 | # Local configuration file (sdk path, etc)
16 | local.properties
17 |
18 | # Windows thumbnail db
19 | Thumbs.db
20 |
21 | # OSX files
22 | .DS_Store
23 |
24 | # Eclipse project files
25 | .classpath
26 | .project
27 |
28 | # Android Studio
29 | .idea
30 | .idea/workspace.xml
31 | .gradle
32 | build/
33 | *.iml
34 | gradlew
35 | gradlew.bat
36 | gradle/
37 | gradle.properties
38 |
39 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright [yyyy] [name of copyright owner]
191 |
192 | Licensed under the Apache License, Version 2.0 (the "License");
193 | you may not use this file except in compliance with the License.
194 | You may obtain a copy of the License at
195 |
196 | http://www.apache.org/licenses/LICENSE-2.0
197 |
198 | Unless required by applicable law or agreed to in writing, software
199 | distributed under the License is distributed on an "AS IS" BASIS,
200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201 | See the License for the specific language governing permissions and
202 | limitations under the License.
203 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # FragmentSwapper
2 | FragmentSwapper is an Open Source Android library that allows easy fragment's management. It is somewhat similar to Activities management model.
3 | For instance, new fragment can be launched from another one with action's request (using request code) and then recieve the result.
4 |
5 | FragmentSwapper works with Android Support Library.
6 |
7 | In order to manage multiple fragments, FragmentSwapper object is required.
8 |
9 | Each fragment must implement FragmentDescriptor interface. Its example implementation is provided in BaseFragment class. BaseFragment can be also used as a base class in your application.
10 |
11 | Currently, it is possible to manage fragments within one container, using SingleContainerFragmentSwapper class.
12 |
13 | For information how to initialize the SingleContainerFragmentSwapper, and how to implement fragment swapping please check sample application and the library code.
14 |
15 | ### Project integration
16 |
17 | Add repository reference in your build.gradle file:
18 |
19 |
20 | repositories {
21 | ...
22 | maven {
23 | url 'http://dev.open-rnd.net:30844/content/groups/public/'
24 | }
25 | ...
26 | }
27 |
28 |
29 | Add library dependence:
30 |
31 |
32 | dependencies {
33 | compile group: "pl.openrnd.android", name: "fragmentsswapper", version: "1.0.1"
34 | }
35 |
36 |
37 | ### License
38 |
39 | 2015 (C) Copyright Open-RnD Sp. z o.o.
40 |
41 | Licensed under the Apache License, Version 2.0 (the "License");
42 | you may not use this file except in compliance with the License.
43 | You may obtain a copy of the License at
44 |
45 | http://www.apache.org/licenses/LICENSE-2.0
46 |
47 | Unless required by applicable law or agreed to in writing, software
48 | distributed under the License is distributed on an "AS IS" BASIS,
49 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
50 | See the License for the specific language governing permissions and
51 | limitations under the License.
52 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | //jcenter()
6 | maven {
7 | url 'http://dev.open-rnd.net:30844/content/groups/public/'
8 | }
9 | }
10 | dependencies {
11 | classpath 'com.android.tools.build:gradle:1.0.0'
12 |
13 | // NOTE: Do not place your application dependencies here; they belong
14 | // in the individual module build.gradle files
15 | }
16 | }
17 |
18 | allprojects {
19 | repositories {
20 | //jcenter()
21 | maven {
22 | url 'http://dev.open-rnd.net:30844/content/groups/public/'
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/.gitignore:
--------------------------------------------------------------------------------
1 | #built application files
2 | *.apk
3 | *.ap_
4 |
5 | # files for the dex VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # generated files
12 | bin/
13 | gen/
14 |
15 | # Local configuration file (sdk path, etc)
16 | local.properties
17 |
18 | # Windows thumbnail db
19 | Thumbs.db
20 |
21 | # OSX files
22 | .DS_Store
23 |
24 | # Eclipse project files
25 | .classpath
26 | .project
27 |
28 | # Android Studio
29 | .idea
30 | .idea/workspace.xml
31 | .gradle
32 | build/
33 | *.iml
34 | gradlew
35 | gradlew.bat
36 | gradle/
37 | gradle.properties
38 |
39 |
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 21
5 | buildToolsVersion "21.0.2"
6 |
7 | defaultConfig {
8 | applicationId "pl.openrnd.managers.fragmentsswapper.sample"
9 | minSdkVersion 14
10 | targetSdkVersion 21
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 |
15 | compileOptions {
16 | sourceCompatibility JavaVersion.VERSION_1_7
17 | targetCompatibility JavaVersion.VERSION_1_7
18 | }
19 | buildTypes {
20 | release {
21 | minifyEnabled false
22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
23 | }
24 | }
25 | }
26 |
27 | dependencies {
28 | compile fileTree(dir: 'libs', include: ['*.jar'])
29 | compile project(":fragment.swapper")
30 | compile 'com.android.support:appcompat-v7:21.0.3'
31 | }
32 |
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Applications/Android Studio.app/Contents/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/src/androidTest/java/pl/openrnd/managers/fragmentswapper/sample/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.example.askwarna.myapplication;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
10 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/src/main/java/pl/openrnd/managers/fragmentsswapper/sample/MainActivity.java:
--------------------------------------------------------------------------------
1 | /******************************************************************************
2 | *
3 | * 2015 (C) Copyright Open-RnD Sp. z o.o.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | *
17 | ******************************************************************************/
18 |
19 | package pl.openrnd.managers.fragmentsswapper.sample;
20 |
21 | import android.os.Bundle;
22 | import android.support.v4.app.Fragment;
23 | import android.support.v4.app.FragmentActivity;
24 | import android.util.Log;
25 |
26 | import pl.openrnd.managers.fragmentsswapper.FragmentSwapper;
27 | import pl.openrnd.managers.fragmentsswapper.InitializationParams;
28 | import pl.openrnd.managers.fragmentsswapper.OnFragmentSwapperListener;
29 | import pl.openrnd.managers.fragmentsswapper.PopParams;
30 | import pl.openrnd.managers.fragmentsswapper.SingleContainerFragmentSwapper;
31 | import pl.openrnd.managers.fragmentsswapper.SwapParams;
32 | import pl.openrnd.managers.fragmentsswapper.sample.fragment.Screen1Fragment;
33 | import pl.openrnd.managers.fragmentsswapper.sample.fragment.Screen2Fragment;
34 | import pl.openrnd.managers.fragmentsswapper.sample.fragment.Screen3Fragment;
35 | import pl.openrnd.managers.fragmentsswapper.sample.fragment.Screen4Fragment;
36 |
37 | public class MainActivity extends FragmentActivity implements SampleScreenManager {
38 | private static final String TAG = MainActivity.class.getSimpleName();
39 |
40 | private SingleContainerFragmentSwapper mFragmentSwapper;
41 |
42 | @Override
43 | protected void onCreate(Bundle savedInstanceState) {
44 | super.onCreate(savedInstanceState);
45 | setContentView(R.layout.activity_main);
46 |
47 | initializeFragmentSwapper(savedInstanceState);
48 | }
49 |
50 | private void initializeFragmentSwapper(Bundle savedInstanceState) {
51 | InitializationParams.Builder builder = new InitializationParams.Builder();
52 | builder.screenManager(this);
53 | builder.contentFrame(R.id.fragmentContainer);
54 | builder.fragmentManager(getSupportFragmentManager());
55 |
56 | mFragmentSwapper = new SingleContainerFragmentSwapper();
57 | mFragmentSwapper.setOnFragmentSwapperListener(mOnFragmentSwapperListener);
58 | mFragmentSwapper.initialize(builder.build());
59 | mFragmentSwapper.onRestoreInstanceState(savedInstanceState);
60 | }
61 |
62 | @Override
63 | protected void onPause() {
64 | super.onPause();
65 |
66 | mFragmentSwapper.onPause();
67 | }
68 |
69 | @Override
70 | protected void onResume() {
71 | super.onResume();
72 |
73 | mFragmentSwapper.onResume();
74 | }
75 |
76 | @Override
77 | public void onBackPressed() {
78 | mFragmentSwapper.onBackPressed(new PopParams.Builder().build());
79 | }
80 |
81 | @Override
82 | public void onMainScreenRequested() {
83 | Log.v(TAG, "onMainScreenRequested()");
84 |
85 | onSampleScreen1Requested(new SwapParams.Builder().build());
86 | }
87 |
88 | @Override
89 | public void onSampleScreen1Requested(SwapParams swapParams) {
90 | Log.v(TAG, "onSampleScreen1Requested()");
91 |
92 | mFragmentSwapper.swapFragment(swapParams, Screen1Fragment.newInstance());
93 | }
94 |
95 | @Override
96 | public void onSampleScreen2Requested(SwapParams swapParams) {
97 | Log.v(TAG, "onSampleScreen2Requested()");
98 |
99 | mFragmentSwapper.swapFragment(swapParams, Screen2Fragment.newInstance());
100 | }
101 |
102 | @Override
103 | public void onSampleScreen3Requested(SwapParams swapParams) {
104 | Log.v(TAG, "onSampleScreen3Requested()");
105 |
106 | mFragmentSwapper.swapFragment(swapParams, Screen3Fragment.newInstance());
107 | }
108 |
109 | @Override
110 | public void onSampleScreen4Requested(SwapParams swapParams) {
111 | Log.v(TAG, "onSampleScreen4Requested()");
112 |
113 | mFragmentSwapper.swapFragment(swapParams, Screen4Fragment.newInstance());
114 | }
115 |
116 | private OnFragmentSwapperListener mOnFragmentSwapperListener = new OnFragmentSwapperListener() {
117 |
118 | @Override
119 | public void onFragmentEntered(FragmentSwapper fragmentSwapper, Fragment fragment) {
120 | Log.v(TAG, "onFragmentEntered()");
121 | }
122 |
123 | @Override
124 | public void onCloseRequested(FragmentSwapper fragmentSwapper) {
125 | Log.v(TAG, "onCloseRequested()");
126 |
127 | setResult(RESULT_CANCELED);
128 | finish();
129 | }
130 | };
131 | }
132 |
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/src/main/java/pl/openrnd/managers/fragmentsswapper/sample/SampleScreenManager.java:
--------------------------------------------------------------------------------
1 | /******************************************************************************
2 | *
3 | * 2015 (C) Copyright Open-RnD Sp. z o.o.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | *
17 | ******************************************************************************/
18 |
19 | package pl.openrnd.managers.fragmentsswapper.sample;
20 |
21 | import pl.openrnd.managers.fragmentsswapper.ScreenManager;
22 | import pl.openrnd.managers.fragmentsswapper.SwapParams;
23 |
24 | public interface SampleScreenManager extends ScreenManager {
25 | void onSampleScreen1Requested(SwapParams swapParams);
26 | void onSampleScreen2Requested(SwapParams swapParams);
27 | void onSampleScreen3Requested(SwapParams swapParams);
28 | void onSampleScreen4Requested(SwapParams swapParams);
29 | }
30 |
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/src/main/java/pl/openrnd/managers/fragmentsswapper/sample/fragment/BaseScreenFragment.java:
--------------------------------------------------------------------------------
1 | /******************************************************************************
2 | *
3 | * 2015 (C) Copyright Open-RnD Sp. z o.o.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | *
17 | ******************************************************************************/
18 |
19 | package pl.openrnd.managers.fragmentsswapper.sample.fragment;
20 |
21 | import android.app.Activity;
22 | import android.os.Bundle;
23 | import android.support.annotation.Nullable;
24 | import android.util.Log;
25 | import android.view.LayoutInflater;
26 | import android.view.View;
27 | import android.view.ViewGroup;
28 | import android.widget.CheckBox;
29 | import android.widget.TextView;
30 |
31 | import pl.openrnd.managers.fragmentsswapper.BaseFragment;
32 | import pl.openrnd.managers.fragmentsswapper.PopParams;
33 | import pl.openrnd.managers.fragmentsswapper.SwapParams;
34 | import pl.openrnd.managers.fragmentsswapper.sample.R;
35 | import pl.openrnd.managers.fragmentsswapper.sample.SampleScreenManager;
36 |
37 | public abstract class BaseScreenFragment extends BaseFragment {
38 | private final String TAG = getClass().getSimpleName();
39 |
40 | protected abstract int getColorResId();
41 | protected abstract int getFragmentReqId();
42 | protected abstract void onNextFragmentRequested(SwapParams swapParams);
43 |
44 | private SampleScreenManager mSampleScreenManager;
45 | private CheckBox mBackStackView;
46 | private CheckBox mMainContextView;
47 | private TextView mInfoView;
48 |
49 | @Override
50 | public void onAttach(Activity activity) {
51 | super.onAttach(activity);
52 |
53 | Log.v(TAG, "onAttach()");
54 |
55 | if (activity instanceof SampleScreenManager) {
56 | mSampleScreenManager = (SampleScreenManager)activity;
57 | } else {
58 | throw new IllegalArgumentException();
59 | }
60 | }
61 |
62 | @Override
63 | public void onDetach() {
64 | super.onDetach();
65 |
66 | Log.v(TAG, "onDetach()");
67 | }
68 |
69 | protected boolean isLastFragment() {
70 | return false;
71 | }
72 |
73 | protected SampleScreenManager getSampleScreenManager() {
74 | return mSampleScreenManager;
75 | }
76 |
77 | @Override
78 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
79 | View root = LayoutInflater.from(getContextActivity()).inflate(R.layout.fragment_screen, null);
80 | initViews(root);
81 | return root;
82 | }
83 |
84 | private void initViews(View root) {
85 | root.setBackgroundResource(getColorResId());
86 |
87 | mInfoView = (TextView)root.findViewById(R.id.info);
88 | mInfoView.setText(collectEnterInfo());
89 |
90 | mBackStackView = (CheckBox)root.findViewById(R.id.addToBackStack);
91 | mMainContextView = (CheckBox)root.findViewById(R.id.mainContext);
92 |
93 | root.findViewById(R.id.closeNoAnimate).setOnClickListener(mOnClickListener);
94 | root.findViewById(R.id.close).setOnClickListener(mOnClickListener);
95 |
96 | View nextScreenView = root.findViewById(R.id.nextScreen);
97 | View nextScreenNoAnimView = root.findViewById(R.id.nextScreenNoAnimate);
98 |
99 | nextScreenView.setOnClickListener(mOnClickListener);
100 | nextScreenNoAnimView.setOnClickListener(mOnClickListener);
101 |
102 | int visibility = isLastFragment() ? View.GONE : View.VISIBLE;
103 | mBackStackView.setVisibility(visibility);
104 | mMainContextView.setVisibility(visibility);
105 | nextScreenView.setVisibility(visibility);
106 | nextScreenNoAnimView.setVisibility(visibility);
107 | }
108 |
109 | private String collectEnterInfo() {
110 | StringBuilder builder = new StringBuilder();
111 |
112 | builder.append("Fragment id: ");
113 | builder.append(getFragmentReqId());
114 | builder.append("\n");
115 |
116 | Integer id = getRequestCode();
117 | if (id != null) {
118 | builder.append("Fragment requested with id: ");
119 | builder.append(id);
120 | builder.append("\n");
121 | }
122 |
123 | return builder.toString();
124 | }
125 |
126 | /**
127 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
128 | */
129 | @Override
130 | public void onFragmentPause() {
131 | super.onFragmentPause();
132 |
133 | Log.v(TAG, "onFragmentPause()");
134 | }
135 |
136 | /**
137 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
138 | */
139 | @Override
140 | public void onFragmentResume() {
141 | super.onFragmentResume();
142 |
143 | Log.v(TAG, "onFragmentResume()");
144 | }
145 |
146 | /**
147 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
148 | */
149 | @Override
150 | public void onFragmentResult(Integer requestCode, int resultCode, Bundle data) {
151 | super.onFragmentResult(requestCode, resultCode, data);
152 |
153 | Log.v(TAG, String.format("onFragmentResult(): requestCode[%s], resultCode[%d]", requestCode, resultCode));
154 |
155 | StringBuilder builder = new StringBuilder(collectEnterInfo());
156 | builder.append("\n");
157 | builder.append("onFragmentResult(): requestCode: ");
158 | builder.append(requestCode != null ? requestCode : "N/A");
159 | builder.append("\n");
160 | builder.append("onFragmentResult(): resultCode: ");
161 | builder.append(resultCode == RESULT_OK ? "RESULT_OK" :
162 | (resultCode == RESULT_CANCELED ? "RESULT_CANCELED" : resultCode));
163 |
164 | mInfoView.setText(builder.toString());
165 | }
166 |
167 | private void onCloseRequested(PopParams popParams) {
168 | setResult(RESULT_OK, null);
169 |
170 | onBackPressed(popParams);
171 | }
172 |
173 | private View.OnClickListener mOnClickListener = new View.OnClickListener() {
174 |
175 | @Override
176 | public void onClick(View v) {
177 | switch (v.getId()) {
178 | case R.id.close:
179 | onCloseRequested(new PopParams.Builder()
180 | .animate(true)
181 | .build());
182 | break;
183 |
184 | case R.id.closeNoAnimate:
185 | onCloseRequested(new PopParams.Builder()
186 | .animate(false)
187 | .build());
188 | break;
189 |
190 | case R.id.nextScreen:
191 | onNextFragmentRequested(new SwapParams.Builder()
192 | .animate(true)
193 | .requestCode(getFragmentReqId())
194 | .addToBackStack(mBackStackView.isChecked())
195 | .mainContext(mMainContextView.isChecked())
196 | .build());
197 | break;
198 |
199 | case R.id.nextScreenNoAnimate:
200 | onNextFragmentRequested(new SwapParams.Builder()
201 | .animate(false)
202 | .requestCode(getFragmentReqId())
203 | .addToBackStack(mBackStackView.isChecked())
204 | .mainContext(mMainContextView.isChecked())
205 | .build());
206 | break;
207 | }
208 | }
209 | };
210 | }
211 |
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/src/main/java/pl/openrnd/managers/fragmentsswapper/sample/fragment/Screen1Fragment.java:
--------------------------------------------------------------------------------
1 | /******************************************************************************
2 | *
3 | * 2015 (C) Copyright Open-RnD Sp. z o.o.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | *
17 | ******************************************************************************/
18 |
19 | package pl.openrnd.managers.fragmentsswapper.sample.fragment;
20 |
21 | import pl.openrnd.managers.fragmentsswapper.SwapParams;
22 | import pl.openrnd.managers.fragmentsswapper.sample.R;
23 |
24 | public class Screen1Fragment extends BaseScreenFragment {
25 |
26 | public static Screen1Fragment newInstance() {
27 | return new Screen1Fragment();
28 | }
29 |
30 | @Override
31 | protected int getColorResId() {
32 | return R.color.screen_1;
33 | }
34 |
35 | @Override
36 | protected int getFragmentReqId() {
37 | return 1;
38 | }
39 |
40 | @Override
41 | protected void onNextFragmentRequested(SwapParams swapParams) {
42 | getSampleScreenManager().onSampleScreen2Requested(swapParams);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/src/main/java/pl/openrnd/managers/fragmentsswapper/sample/fragment/Screen2Fragment.java:
--------------------------------------------------------------------------------
1 | /******************************************************************************
2 | *
3 | * 2015 (C) Copyright Open-RnD Sp. z o.o.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | *
17 | ******************************************************************************/
18 |
19 | package pl.openrnd.managers.fragmentsswapper.sample.fragment;
20 |
21 | import pl.openrnd.managers.fragmentsswapper.SwapParams;
22 | import pl.openrnd.managers.fragmentsswapper.sample.R;
23 |
24 | public class Screen2Fragment extends BaseScreenFragment {
25 |
26 | public static Screen2Fragment newInstance() {
27 | return new Screen2Fragment();
28 | }
29 |
30 | @Override
31 | protected int getColorResId() {
32 | return R.color.screen_2;
33 | }
34 |
35 | @Override
36 | protected int getFragmentReqId() {
37 | return 2;
38 | }
39 |
40 | @Override
41 | protected void onNextFragmentRequested(SwapParams swapParams) {
42 | getSampleScreenManager().onSampleScreen3Requested(swapParams);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/src/main/java/pl/openrnd/managers/fragmentsswapper/sample/fragment/Screen3Fragment.java:
--------------------------------------------------------------------------------
1 | /******************************************************************************
2 | *
3 | * 2015 (C) Copyright Open-RnD Sp. z o.o.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | *
17 | ******************************************************************************/
18 |
19 | package pl.openrnd.managers.fragmentsswapper.sample.fragment;
20 |
21 | import pl.openrnd.managers.fragmentsswapper.SwapParams;
22 | import pl.openrnd.managers.fragmentsswapper.sample.R;
23 |
24 | public class Screen3Fragment extends BaseScreenFragment {
25 |
26 | public static Screen3Fragment newInstance() {
27 | return new Screen3Fragment();
28 | }
29 |
30 | @Override
31 | protected int getColorResId() {
32 | return R.color.screen_3;
33 | }
34 |
35 | @Override
36 | protected int getFragmentReqId() {
37 | return 3;
38 | }
39 |
40 | @Override
41 | protected void onNextFragmentRequested(SwapParams swapParams) {
42 | getSampleScreenManager().onSampleScreen4Requested(swapParams);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/src/main/java/pl/openrnd/managers/fragmentsswapper/sample/fragment/Screen4Fragment.java:
--------------------------------------------------------------------------------
1 | /******************************************************************************
2 | *
3 | * 2015 (C) Copyright Open-RnD Sp. z o.o.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | *
17 | ******************************************************************************/
18 |
19 | package pl.openrnd.managers.fragmentsswapper.sample.fragment;
20 |
21 | import pl.openrnd.managers.fragmentsswapper.SwapParams;
22 | import pl.openrnd.managers.fragmentsswapper.sample.R;
23 |
24 | public class Screen4Fragment extends BaseScreenFragment {
25 |
26 | public static Screen4Fragment newInstance() {
27 | return new Screen4Fragment();
28 | }
29 |
30 | @Override
31 | protected int getColorResId() {
32 | return R.color.screen_4;
33 | }
34 |
35 | @Override
36 | protected int getFragmentReqId() {
37 | return 4;
38 | }
39 |
40 | @Override
41 | protected boolean isLastFragment() {
42 | return true;
43 | }
44 |
45 | @Override
46 | protected void onNextFragmentRequested(SwapParams swapParams) {
47 |
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/src/main/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/open-rnd/android-fragment-swapper/ccef2a90b28e0958afcf944df51bf11a26ce7827/fragment.swapper.sample.app/src/main/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/src/main/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/open-rnd/android-fragment-swapper/ccef2a90b28e0958afcf944df51bf11a26ce7827/fragment.swapper.sample.app/src/main/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/src/main/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/open-rnd/android-fragment-swapper/ccef2a90b28e0958afcf944df51bf11a26ce7827/fragment.swapper.sample.app/src/main/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/src/main/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/open-rnd/android-fragment-swapper/ccef2a90b28e0958afcf944df51bf11a26ce7827/fragment.swapper.sample.app/src/main/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/src/main/res/layout/fragment_screen.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
14 |
15 |
23 |
24 |
32 |
33 |
40 |
41 |
48 |
49 |
56 |
57 |
64 |
65 |
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FF0000
4 | #00FF00
5 | #0000FF
6 | #FF00FF
7 | #FFFFFF
8 |
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 | 14sp
7 |
8 |
9 |
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | FragmentSwapperSampleApp
5 | Next
6 | Next (no animate)
7 | Close
8 | Close (no animate)
9 | Add to BackStack
10 | Main context
11 |
12 |
13 |
--------------------------------------------------------------------------------
/fragment.swapper.sample.app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/fragment.swapper/.gitignore:
--------------------------------------------------------------------------------
1 | #built application files
2 | *.apk
3 | *.ap_
4 |
5 | # files for the dex VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # generated files
12 | bin/
13 | gen/
14 |
15 | # Local configuration file (sdk path, etc)
16 | local.properties
17 |
18 | # Windows thumbnail db
19 | Thumbs.db
20 |
21 | # OSX files
22 | .DS_Store
23 |
24 | # Eclipse project files
25 | .classpath
26 | .project
27 |
28 | # Android Studio
29 | .idea
30 | .idea/workspace.xml
31 | .gradle
32 | build/
33 | *.iml
34 | gradlew
35 | gradlew.bat
36 | gradle/
37 | gradle.properties
38 |
39 |
--------------------------------------------------------------------------------
/fragment.swapper/build.gradle:
--------------------------------------------------------------------------------
1 | import java.util.regex.Pattern
2 |
3 | apply plugin: 'com.android.library'
4 | apply plugin: 'maven'
5 |
6 | def groupId = "pl.openrnd.android"
7 | def artifactId = "fragmentsswapper"
8 |
9 | android {
10 | compileSdkVersion 21
11 | buildToolsVersion "21.1.2"
12 |
13 | defaultConfig {
14 | minSdkVersion 14
15 | targetSdkVersion 21
16 | }
17 |
18 | buildTypes {
19 | debug {
20 | minifyEnabled false
21 | }
22 | release {
23 | minifyEnabled false
24 | }
25 | }
26 | }
27 |
28 | dependencies {
29 | compile fileTree(dir: 'libs', include: ['*.jar'])
30 | compile 'com.android.support:support-v4:21.0.+'
31 | compile 'com.google.android.gms:play-services:+'
32 | compile group: "pl.openrnd.android", name: "utils", version: "1.0.1", changing: true
33 | }
34 |
35 |
36 |
--------------------------------------------------------------------------------
/fragment.swapper/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/mwlodarczyk/Programming/android-sdks/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/fragment.swapper/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/fragment.swapper/src/main/java/pl/openrnd/managers/fragmentsswapper/BaseFragment.java:
--------------------------------------------------------------------------------
1 | /******************************************************************************
2 | *
3 | * 2015 (C) Copyright Open-RnD Sp. z o.o.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | *
17 | ******************************************************************************/
18 |
19 | package pl.openrnd.managers.fragmentsswapper;
20 |
21 | import android.app.Activity;
22 | import android.content.res.Resources;
23 | import android.os.Bundle;
24 | import android.support.v4.app.Fragment;
25 | import android.view.animation.Animation;
26 |
27 | import pl.openrnd.utils.ViewUtils;
28 |
29 | /**
30 | * Base implementation of fragment that is supported by FragmentSwapper.
31 | */
32 | public class BaseFragment extends Fragment implements FragmentDescriptor {
33 |
34 | private FragmentDescriptorImpl mFragmentDescriptor;
35 |
36 | /**
37 | * Base fragment constructor.
38 | */
39 | public BaseFragment() {
40 | mFragmentDescriptor = new FragmentDescriptorImpl(this);
41 | }
42 |
43 | /**
44 | * @see android.support.v4.app.Fragment
45 | */
46 | @Override
47 | public void onCreate(Bundle savedInstanceState) {
48 | super.onCreate(savedInstanceState);
49 |
50 | mFragmentDescriptor.onCreate(savedInstanceState);
51 | }
52 |
53 | /**
54 | * @see android.support.v4.app.Fragment
55 | */
56 | @Override
57 | public void onSaveInstanceState(Bundle outState) {
58 | super.onSaveInstanceState(outState);
59 |
60 | mFragmentDescriptor.onSaveInstanceState(outState);
61 | }
62 |
63 | /**
64 | * @see android.support.v4.app.Fragment
65 | */
66 | @Override
67 | public void onAttach(Activity activity) {
68 | super.onAttach(activity);
69 |
70 | mFragmentDescriptor.onAttach(activity);
71 | }
72 |
73 | /**
74 | * @see android.support.v4.app.Fragment
75 | *
76 | * Method checks if animations are currently enabled in FragmentSwapper that
77 | * the fragment is attache to. If animations are disabled empty animation is
78 | * returned.
79 | */
80 | @Override
81 | public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
82 | FragmentSwapper fragmentSwapper = mFragmentDescriptor.getFragmentSwapper();
83 |
84 | if ((fragmentSwapper == null) || fragmentSwapper.isAnimationEnabled()) {
85 | return super.onCreateAnimation(transit, enter, nextAnim);
86 | } else {
87 | Animation animation = new Animation() {};
88 | animation.setDuration(0);
89 | return animation;
90 | }
91 | }
92 |
93 | /**
94 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
95 | */
96 | @Override
97 | public void assignFragmentSwapper(FragmentSwapper fragmentSwapper) {
98 | mFragmentDescriptor.assignFragmentSwapper(fragmentSwapper);
99 | }
100 |
101 | /**
102 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
103 | */
104 | @Override
105 | public FragmentSwapper getFragmentSwapper() {
106 | return mFragmentDescriptor.getFragmentSwapper();
107 | }
108 |
109 | /**
110 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
111 | */
112 | @Override
113 | public String getName() {
114 | return mFragmentDescriptor.getName();
115 | }
116 |
117 | /**
118 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
119 | */
120 | @Override
121 | public Integer getRequestCode() {
122 | return mFragmentDescriptor.getRequestCode();
123 | }
124 |
125 | /**
126 | * Sets fragments result code and data.
127 | *
128 | * @param resultCode Result code.
129 | * @param resultData Bundle object containing fragment result data.
130 | */
131 | protected void setResult(int resultCode, Bundle resultData) {
132 | mFragmentDescriptor.setResult(resultCode, resultData);
133 | }
134 |
135 | /**
136 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
137 | */
138 | @Override
139 | public int getResultCode() {
140 | return mFragmentDescriptor.getResultCode();
141 | }
142 |
143 | /**
144 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
145 | */
146 | @Override
147 | public Bundle getResultData() {
148 | return mFragmentDescriptor.getResultData();
149 | }
150 |
151 | /**
152 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
153 | */
154 | @Override
155 | public void setRequestCode(Integer requestCode) {
156 | mFragmentDescriptor.setRequestCode(requestCode);
157 | }
158 |
159 | /**
160 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
161 | */
162 | @Override
163 | public void onFragmentResult(Integer requestCode, int resultCode, Bundle data) {
164 | mFragmentDescriptor.onFragmentResult(requestCode, resultCode, data);
165 | }
166 |
167 | /**
168 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
169 | */
170 | @Override
171 | public void onFragmentPause() {
172 |
173 | }
174 |
175 | /**
176 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
177 | */
178 | @Override
179 | public void onFragmentResume() {
180 | ViewUtils.hideSoftKeyboard(getContextActivity());
181 | }
182 |
183 | /**
184 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
185 | */
186 | @Override
187 | public void onBackPressed(PopParams popParams) {
188 | mFragmentDescriptor.onBackPressed(popParams);
189 | }
190 |
191 | /**
192 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
193 | */
194 | @Override
195 | public Activity getContextActivity() {
196 | return mFragmentDescriptor.getContextActivity();
197 | }
198 |
199 | /**
200 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
201 | */
202 | @Override
203 | public String getContextString(int resId) {
204 | return mFragmentDescriptor.getContextString(resId);
205 | }
206 |
207 | /**
208 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
209 | */
210 | @Override
211 | public String getContextString(int resId, Object... formatArgs) {
212 | return mFragmentDescriptor.getContextString(resId, formatArgs);
213 | }
214 |
215 | /**
216 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
217 | */
218 | @Override
219 | public Resources getContextResources() {
220 | return mFragmentDescriptor.getContextResources();
221 | }
222 | }
223 |
--------------------------------------------------------------------------------
/fragment.swapper/src/main/java/pl/openrnd/managers/fragmentsswapper/FragmentDescriptor.java:
--------------------------------------------------------------------------------
1 | /******************************************************************************
2 | *
3 | * 2015 (C) Copyright Open-RnD Sp. z o.o.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | *
17 | ******************************************************************************/
18 |
19 | package pl.openrnd.managers.fragmentsswapper;
20 |
21 | import android.app.Activity;
22 | import android.content.res.Resources;
23 | import android.os.Bundle;
24 |
25 | /**
26 | * Interface that has to be implemented by fragments
27 | * managed by FragmentSwapper
28 | */
29 | public interface FragmentDescriptor {
30 | public static final int RESULT_OK = Activity.RESULT_OK;
31 | public static final int RESULT_CANCELED = Activity.RESULT_CANCELED;
32 | public static final int RESULT_FIRST_USER = Activity.RESULT_FIRST_USER;
33 |
34 | /**
35 | * Gets fragment name.
36 | *
37 | * @return Fragment name.
38 | */
39 | String getName();
40 |
41 | /**
42 | * Gets request code that the fragment was started with.
43 | *
44 | * @return Fragments request code or null if fragment was not started for result.
45 | */
46 | Integer getRequestCode();
47 |
48 | /**
49 | * Gets fragments result code.
50 | *
51 | * @return Fragments result code. RESULT_CANCELED is returned if fragment was canceled.
52 | */
53 | int getResultCode();
54 |
55 | /**
56 | * Gets Bundle object with fragment result data.
57 | *
58 | * @return Bundle object or null if not provided while fragment closing.
59 | */
60 | Bundle getResultData();
61 |
62 | /**
63 | * Sets fragment request code.
64 | *
65 | * To be used only by FragmentSwapper while Fragment creation.
66 | *
67 | * @param requestCode Fragment request code.
68 | */
69 | void setRequestCode(Integer requestCode);
70 |
71 | /**
72 | * Method called when the fragment that was started is exiting.
73 | *
74 | * The request code, result code and result data that was provided while
75 | * fragment closing are passed to the fragment that previously started it.
76 | *
77 | * @param requestCode Fragments request code or null if fragment was not started with request code.
78 | * @param resultCode Fragments result code.
79 | * @param data Bundle object containing fragments result data or null if not provided.
80 | */
81 | void onFragmentResult(Integer requestCode, int resultCode, Bundle data);
82 |
83 | /**
84 | * Method called when the fragment is paused.
85 | */
86 | void onFragmentPause();
87 |
88 | /**
89 | * Method called when the fragment is resumed.
90 | */
91 | void onFragmentResume();
92 |
93 | /**
94 | * Assigns FragmentSwapper that the fragment is attached to.
95 | *
96 | * Method to be used only by FragmentSwapper.
97 | *
98 | * @param fragmentSwapper Attached FragmentSwapper.
99 | */
100 | void assignFragmentSwapper(FragmentSwapper fragmentSwapper);
101 |
102 | /**
103 | * Gets FragmentSwapper that the fragment is attached to.
104 | *
105 | * @return FragmentSwapper that the fragment is attached to.
106 | */
107 | FragmentSwapper getFragmentSwapper();
108 |
109 | /**
110 | * Method to be called by the fragment when its close is requested.
111 | *
112 | * @param popParams PopParams object with fragment swapping transaction parameters.
113 | */
114 | void onBackPressed(PopParams popParams);
115 |
116 | /**
117 | * Gets Activity that the fragment is/was attached to.
118 | *
119 | * @return Activity that the fragment is/was attached to.
120 | */
121 | Activity getContextActivity();
122 |
123 | /**
124 | * Gets string from the application's resources.
125 | *
126 | * @see android.app.Activity
127 | *
128 | * @return Localized formatted string.
129 | */
130 | String getContextString(int resId, Object... formatArgs);
131 |
132 | /**
133 | * Gets string from the application's resources.
134 | *
135 | * @see android.app.Activity
136 | *
137 | * @return Localized formatted string.
138 | */
139 | String getContextString(int resId);
140 |
141 | /**
142 | * Gets Resources instance of the application.
143 | * @return Resources instance of the application.
144 | */
145 | Resources getContextResources();
146 | }
147 |
--------------------------------------------------------------------------------
/fragment.swapper/src/main/java/pl/openrnd/managers/fragmentsswapper/FragmentDescriptorImpl.java:
--------------------------------------------------------------------------------
1 | /******************************************************************************
2 | *
3 | * 2015 (C) Copyright Open-RnD Sp. z o.o.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | *
17 | ******************************************************************************/
18 |
19 | package pl.openrnd.managers.fragmentsswapper;
20 |
21 | import android.app.Activity;
22 | import android.content.res.Resources;
23 | import android.os.Bundle;
24 | import android.support.v4.app.Fragment;
25 | import android.util.Log;
26 |
27 | /**
28 | * Base implementation of FragmentDescription.
29 | *
30 | * Class to be when 3rd parties fragments have to be managed by FragmentSwapper.
31 | *
32 | * @see pl.openrnd.managers.fragmentsswapper.BaseFragment
33 | */
34 | public class FragmentDescriptorImpl implements FragmentDescriptor {
35 |
36 | private static final String TAG = FragmentDescriptorImpl.class.getSimpleName();
37 |
38 | private static final String DATA_REQUEST_CODE = String.format("%s_%s", TAG, "DATA_REQUEST_CODE");
39 | private static final String DATA_RESULT_CODE = String.format("%s_%s", TAG, "DATA_RESULT_CODE");
40 | private static final String DATA_RESULT_BUNDLE = String.format("%s_%s", TAG, "DATA_RESULT_BUNDLE");
41 |
42 | private FragmentSwapper mFragmentSwapper;
43 | private Activity mActivity;
44 |
45 | private Integer mResultCode;
46 | private Bundle mResultData;
47 |
48 | private Fragment mFragment;
49 |
50 | /**
51 | * Class constructor
52 | *
53 | * @param fragment Holding fragment
54 | */
55 | public FragmentDescriptorImpl(Fragment fragment) {
56 | mFragment = fragment;
57 | }
58 |
59 | /**
60 | * Method must be called in onAttach() method of holding fragment.
61 | *
62 | * @see android.support.v4.app.Fragment
63 | *
64 | * @param activity Activity that the fragment will be attached to.
65 | */
66 | public void onAttach(Activity activity) {
67 | mActivity = activity;
68 | }
69 |
70 | /**
71 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
72 | */
73 | @Override
74 | public void assignFragmentSwapper(FragmentSwapper fragmentSwapper) {
75 | mFragmentSwapper = fragmentSwapper;
76 | }
77 |
78 | /**
79 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
80 | */
81 | @Override
82 | public FragmentSwapper getFragmentSwapper() {
83 | return mFragmentSwapper;
84 | }
85 |
86 | /**
87 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
88 | */
89 | @Override
90 | public String getName() {
91 | return mFragment.getClass().getSimpleName();
92 | }
93 |
94 | /**
95 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
96 | */
97 | @Override
98 | public Integer getRequestCode() {
99 | Integer result = null;
100 |
101 | Bundle arguments = mFragment.getArguments();
102 | if ((arguments != null) && (arguments.containsKey(DATA_REQUEST_CODE))) {
103 | result = arguments.getInt(DATA_REQUEST_CODE);
104 | }
105 |
106 | Log.v(TAG, String.format("getResultData(): result[%s]", result));
107 |
108 | return result;
109 | }
110 |
111 | /**
112 | * Method must be called in onCreate() method of holding fragment.
113 | *
114 | * @see android.support.v4.app.Fragment
115 | *
116 | * @param savedInstanceState Bundle object containing saved state.
117 | */
118 | public void onCreate(Bundle savedInstanceState) {
119 | if (savedInstanceState != null) {
120 | if (savedInstanceState.containsKey(DATA_REQUEST_CODE)) {
121 | mResultCode = savedInstanceState.getInt(DATA_REQUEST_CODE, RESULT_CANCELED);
122 | }
123 |
124 | if (savedInstanceState.containsKey(DATA_RESULT_BUNDLE)) {
125 | mResultData = savedInstanceState.getBundle(DATA_RESULT_BUNDLE);
126 | }
127 | }
128 | }
129 |
130 | /**
131 | * Method must be called in onSaveInstanceState() method of holding fragment.
132 | *
133 | * @see android.support.v4.app.Fragment
134 | *
135 | * @param outState Bundle to store saved state.
136 | */
137 | public void onSaveInstanceState(Bundle outState) {
138 | if (outState != null) {
139 | if (mResultCode != null) {
140 | outState.putInt(DATA_RESULT_CODE, mResultCode);
141 | }
142 |
143 | if (mResultData != null) {
144 | outState.putBundle(DATA_RESULT_BUNDLE, mResultData);
145 | }
146 | }
147 | }
148 |
149 | /**
150 | * Sets holding fragments result code and data.
151 | *
152 | * @param resultCode Result code.
153 | * @param resultData Bundle object containing fragment results data.
154 | */
155 | public void setResult(int resultCode, Bundle resultData) {
156 | mResultCode = resultCode;
157 | mResultData = resultData;
158 | }
159 |
160 | /**
161 | * Gets holding fragment result code.
162 | *
163 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
164 | *
165 | * @return Holding fragment result code.
166 | */
167 | @Override
168 | public int getResultCode() {
169 | int result = mResultCode == null ? RESULT_CANCELED : mResultCode;
170 |
171 | Log.v(TAG, String.format("getResultCode(): result[%d]", result));
172 |
173 | return result;
174 | }
175 |
176 | /**
177 | * Gets holding fragment result data as a Bundle object.
178 | *
179 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
180 | *
181 | * @return Holding fragment result data as a Bundle object.
182 | */
183 | @Override
184 | public Bundle getResultData() {
185 | Log.v(TAG, String.format("getResultData(): result[%b]", mResultData != null));
186 |
187 | return mResultData;
188 | }
189 |
190 | /**
191 | * Sets holding fragment request code.
192 | *
193 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
194 | *
195 | * @param requestCode Fragment request code.
196 | */
197 | @Override
198 | public void setRequestCode(Integer requestCode) {
199 | if (requestCode != null) {
200 | Bundle arguments = mFragment.getArguments();
201 | if (arguments == null) {
202 | arguments = new Bundle();
203 | } else {
204 | if (arguments.containsKey(DATA_REQUEST_CODE)) {
205 | throw new IllegalStateException("Request code key already taken");
206 | }
207 | }
208 | arguments.putInt(DATA_REQUEST_CODE, requestCode);
209 | mFragment.setArguments(arguments);
210 | }
211 | }
212 |
213 | /**
214 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
215 | */
216 | @Override
217 | public void onFragmentResult(Integer requestCode, int resultCode, Bundle data) {
218 | Log.v(TAG, String.format("onFragmentResult(): requestCode[%s], resultCode[%d], data[%b]", requestCode, resultCode, data != null));
219 | }
220 |
221 | /**
222 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
223 | */
224 | @Override
225 | public void onBackPressed(PopParams popParams) {
226 | Log.v(TAG, "onBackPressed()");
227 |
228 | if (mFragmentSwapper != null) {
229 | mFragmentSwapper.popFragment(popParams);
230 | }
231 | }
232 |
233 | /**
234 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
235 | */
236 | @Override
237 | public void onFragmentPause() {
238 |
239 | }
240 |
241 | /**
242 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
243 | */
244 | @Override
245 | public void onFragmentResume() {
246 |
247 | }
248 |
249 | /**
250 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
251 | */
252 | @Override
253 | public Activity getContextActivity() {
254 | return mActivity;
255 | }
256 |
257 | /**
258 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
259 | */
260 | @Override
261 | public String getContextString(int resId) {
262 | return mActivity.getString(resId);
263 | }
264 |
265 | /**
266 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
267 | */
268 | @Override
269 | public String getContextString(int resId, Object... formatArgs) {
270 | return mActivity.getString(resId, formatArgs);
271 | }
272 |
273 | /**
274 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
275 | */
276 | @Override
277 | public Resources getContextResources() {
278 | return mActivity.getResources();
279 | }
280 | }
281 |
--------------------------------------------------------------------------------
/fragment.swapper/src/main/java/pl/openrnd/managers/fragmentsswapper/FragmentSwapper.java:
--------------------------------------------------------------------------------
1 | /******************************************************************************
2 | *
3 | * 2015 (C) Copyright Open-RnD Sp. z o.o.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | *
17 | ******************************************************************************/
18 |
19 | package pl.openrnd.managers.fragmentsswapper;
20 |
21 | import android.support.v4.app.Fragment;
22 |
23 | /**
24 | * Interface defining base FragmentSwapper API.
25 | *
26 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
27 | * @see android.support.v4.app.Fragment
28 | *
29 | * @param Fragment class that is supported by FragmentSwapper.
30 | */
31 | public interface FragmentSwapper {
32 |
33 | /**
34 | * Gets information if fragments transactions animations are enabled.
35 | *
36 | * @return True if animations are enabled, false otherwise.
37 | */
38 | boolean isAnimationEnabled();
39 |
40 | /**
41 | * Method that must be called by the holding Activity in its onPause() method.
42 | *
43 | * @see android.app.Activity
44 | */
45 | void onPause();
46 |
47 | /**
48 | * Method that must be called by the holding Activity in its onResume() method.
49 | *
50 | * @see android.app.Activity
51 | */
52 | void onResume();
53 |
54 | /**
55 | * Method starting fragment enter transaction.
56 | *
57 | * @param swapParams SwapParams object with transaction parameters.
58 | * @param fragment Fragment to be entered.
59 | */
60 | void swapFragment(SwapParams swapParams, F fragment);
61 |
62 | /**
63 | * Method starting fragment popping transaction.
64 | *
65 | * @param popParams PopParams object with transaction parameters.
66 | */
67 | void popFragment(PopParams popParams);
68 |
69 | /**
70 | * Gets current fragment
71 | *
72 | * @return Current fragment
73 | */
74 | F getCurrentFragment();
75 | }
76 |
--------------------------------------------------------------------------------
/fragment.swapper/src/main/java/pl/openrnd/managers/fragmentsswapper/InitializationParams.java:
--------------------------------------------------------------------------------
1 | /******************************************************************************
2 | *
3 | * 2015 (C) Copyright Open-RnD Sp. z o.o.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | *
17 | ******************************************************************************/
18 |
19 | package pl.openrnd.managers.fragmentsswapper;
20 |
21 | import android.support.v4.app.FragmentManager;
22 |
23 | /**
24 | * FragmentSwapper initialization parameters class.
25 | */
26 | public class InitializationParams {
27 | private FragmentManager mFragmentManager;
28 | private int mContentFrame;
29 | private ScreenManager mScreenManager;
30 |
31 | public FragmentManager getFragmentManager() {
32 | return mFragmentManager;
33 | }
34 |
35 | public int getContentFrame() {
36 | return mContentFrame;
37 | }
38 |
39 | public ScreenManager getScreenManager() {
40 | return mScreenManager;
41 | }
42 |
43 | private InitializationParams(Builder builder) {
44 | mFragmentManager = builder.mFragmentManager;
45 | mContentFrame = builder.mContentFrame;
46 | mScreenManager = builder.mScreenManager;
47 | }
48 |
49 | /**
50 | * Parameters builder class.
51 | */
52 | public static class Builder {
53 | private FragmentManager mFragmentManager;
54 | private Integer mContentFrame;
55 | private ScreenManager mScreenManager;
56 |
57 | /**
58 | * Sets FragmentManager associated with the FragmentSwapper context Activity.
59 | *
60 | * Parameter required.
61 | *
62 | * @param fragmentManager FragmentManager
63 | * @return Builder object
64 | */
65 | public Builder fragmentManager(FragmentManager fragmentManager) {
66 | mFragmentManager = fragmentManager;
67 | return this;
68 | }
69 |
70 | /**
71 | * Sets container Id that will be used in fragments transactions by the FragmentSwapper.
72 | *
73 | * Parameter required.
74 | *
75 | * @param contentFrame Container Id
76 | * @return Builder object
77 | */
78 | public Builder contentFrame(int contentFrame) {
79 | mContentFrame = contentFrame;
80 | return this;
81 | }
82 |
83 | /**
84 | * Sets ScreenManager object with applications screens triggering routines.
85 | *
86 | * Parameter required.
87 | *
88 | * @param screenManager ScreenManager object
89 | * @return Builder object
90 | */
91 | public Builder screenManager(ScreenManager screenManager) {
92 | mScreenManager = screenManager;
93 | return this;
94 | }
95 |
96 | /**
97 | * Builds InitializationParams object with current arguments state.
98 | *
99 | * @throws IllegalStateException The exception is thrown when required parameter is not provided.
100 | *
101 | * @return InitializationParams object.
102 | */
103 | public InitializationParams build() {
104 | if ((mFragmentManager == null)
105 | || (mContentFrame == null)
106 | || (mScreenManager == null)) {
107 | throw new IllegalStateException("All parameters are mandatory");
108 | }
109 |
110 | return new InitializationParams(this);
111 | }
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/fragment.swapper/src/main/java/pl/openrnd/managers/fragmentsswapper/OnFragmentSwapperListener.java:
--------------------------------------------------------------------------------
1 | /******************************************************************************
2 | *
3 | * 2015 (C) Copyright Open-RnD Sp. z o.o.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | *
17 | ******************************************************************************/
18 |
19 | package pl.openrnd.managers.fragmentsswapper;
20 |
21 | import android.support.v4.app.Fragment;
22 |
23 | /**
24 | * Interface used for getting notification about state and requests from FragmentSwapper.
25 | */
26 | public interface OnFragmentSwapperListener {
27 |
28 | /**
29 | * Method called when new fragment enters.
30 | *
31 | * This method can be used for updating UI according to new fragment.
32 | *
33 | * @param fragmentSwapper Notification sender.
34 | * @param fragment New fragment instance.
35 | */
36 | void onFragmentEntered(FragmentSwapper fragmentSwapper, Fragment fragment);
37 |
38 | /**
39 | * Method called when last fragment was popped.
40 | *
41 | * This notification can be used for requesting application close.
42 | *
43 | * @param fragmentSwapper Notification sender.
44 | */
45 | void onCloseRequested(FragmentSwapper fragmentSwapper);
46 | }
47 |
--------------------------------------------------------------------------------
/fragment.swapper/src/main/java/pl/openrnd/managers/fragmentsswapper/PopParams.java:
--------------------------------------------------------------------------------
1 | /******************************************************************************
2 | *
3 | * 2015 (C) Copyright Open-RnD Sp. z o.o.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | *
17 | ******************************************************************************/
18 |
19 | package pl.openrnd.managers.fragmentsswapper;
20 |
21 | /**
22 | * Class holding fragments pop transaction parameters.
23 | */
24 | public class PopParams {
25 | private boolean mAnimate;
26 |
27 | /**
28 | * Gets animation status of pop transaction.
29 | *
30 | * @return True if pop animation is enabled, false otherwise.
31 | */
32 | public boolean isAnimate() {
33 | return mAnimate;
34 | }
35 |
36 | private PopParams(Builder builder) {
37 | mAnimate = builder.mAnimate;
38 | }
39 |
40 | /**
41 | * PopParams object builder.
42 | */
43 | public static class Builder {
44 | private boolean mAnimate;
45 |
46 | /**
47 | * Class constructor.
48 | */
49 | public Builder() {
50 | mAnimate = true;
51 | }
52 |
53 | /**
54 | * Sets animation status of pop transaction.
55 | *
56 | * @param animate True if animations is enabled, false otherwise.
57 | * @return Builder object.
58 | */
59 | public Builder animate(boolean animate) {
60 | mAnimate = animate;
61 | return this;
62 | }
63 |
64 | /**
65 | * Builds PopParams object with current builder arguments.
66 | *
67 | * @return PopParams object.
68 | */
69 | public PopParams build() {
70 | return new PopParams(this);
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/fragment.swapper/src/main/java/pl/openrnd/managers/fragmentsswapper/ScreenManager.java:
--------------------------------------------------------------------------------
1 | /******************************************************************************
2 | *
3 | * 2015 (C) Copyright Open-RnD Sp. z o.o.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | *
17 | ******************************************************************************/
18 |
19 | package pl.openrnd.managers.fragmentsswapper;
20 |
21 | /**
22 | * Base interface for screens definitions.
23 | */
24 | public interface ScreenManager {
25 | void onMainScreenRequested();
26 | }
27 |
--------------------------------------------------------------------------------
/fragment.swapper/src/main/java/pl/openrnd/managers/fragmentsswapper/SingleContainerFragmentSwapper.java:
--------------------------------------------------------------------------------
1 | /******************************************************************************
2 | *
3 | * 2015 (C) Copyright Open-RnD Sp. z o.o.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | *
17 | ******************************************************************************/
18 |
19 | package pl.openrnd.managers.fragmentsswapper;
20 |
21 | import android.os.Bundle;
22 | import android.os.Handler;
23 | import android.os.Looper;
24 | import android.support.v4.app.Fragment;
25 | import android.support.v4.app.FragmentManager;
26 | import android.support.v4.app.FragmentTransaction;
27 | import android.util.Log;
28 |
29 | import java.util.LinkedList;
30 |
31 | /**
32 | * FragmentSwapper that supports only one container for fragments transactions.
33 | *
34 | * @see pl.openrnd.managers.fragmentsswapper.FragmentDescriptor
35 | * @see android.support.v4.app.Fragment
36 | *
37 | * @param Fragment class that is supported by FragmentSwapper.
38 | */
39 | public class SingleContainerFragmentSwapper implements FragmentSwapper {
40 | private static final String TAG = SingleContainerFragmentSwapper.class.getSimpleName();
41 |
42 | private InitializationParams mInitializationParams;
43 |
44 | private boolean mIsAnimationEnabled;
45 | private boolean mPopInProgress;
46 |
47 | private Handler mUiHandler;
48 | private F mContentFragment;
49 | private int mLastStackCount;
50 |
51 | private Integer mRequestCode;
52 | private int mResultCode;
53 | private Bundle mResultBundle;
54 |
55 | private boolean mIsSavedStateActive;
56 |
57 | private LinkedList mPendingOperations;
58 |
59 | private OnFragmentSwapperListener mOnFragmentSwapperListener;
60 |
61 | /**
62 | * Default constructor.
63 | */
64 | public SingleContainerFragmentSwapper() {
65 | mUiHandler = new Handler(Looper.getMainLooper());
66 | }
67 |
68 | /**
69 | * FragmentSwapper initialization routine.
70 | *
71 | * @param initializationParams InitializationParams object with initialization parameters.
72 | */
73 | public void initialize(InitializationParams initializationParams) {
74 | if (initializationParams == null) {
75 | throw new IllegalArgumentException("Argument is mandatory");
76 | }
77 |
78 | mInitializationParams = initializationParams;
79 | getFragmentManager().addOnBackStackChangedListener(mOnBackStackChangedListener);
80 | mLastStackCount = getFragmentManager().getBackStackEntryCount();
81 | mPendingOperations = new LinkedList();
82 | }
83 |
84 | /**
85 | * @see pl.openrnd.managers.fragmentsswapper.FragmentSwapper
86 | */
87 | @Override
88 | public void onPause() {
89 | Log.v(TAG, "onPause()");
90 |
91 | mIsSavedStateActive = true;
92 | }
93 |
94 | /**
95 | * @see pl.openrnd.managers.fragmentsswapper.FragmentSwapper
96 | */
97 | @Override
98 | public void onResume() {
99 | Log.v(TAG, "onResume()");
100 |
101 | mIsSavedStateActive = false;
102 |
103 | handlePendingOperations();
104 | }
105 |
106 | /**
107 | * Method to be called with Bundle object containing state of holding Activity.
108 | *
109 | * Method starts main screen using attached ScreenManager if activity is created for the first time.
110 | *
111 | * @param savedInstanceState Bundle object with saved state.
112 | */
113 | public void onRestoreInstanceState(Bundle savedInstanceState) {
114 | Log.v(TAG, String.format("onRestoreInstanceState(): savedInstanceState[%b]", savedInstanceState != null));
115 |
116 | if (savedInstanceState == null) {
117 | mInitializationParams.getScreenManager().onMainScreenRequested();
118 | } else {
119 | if (findCurrentFragment()) {
120 | notifyNewFragment(mContentFragment);
121 | }
122 | mOnBackStackChangedListener.onBackStackChanged();
123 | }
124 | }
125 |
126 | private void handlePendingOperations() {
127 | Log.v(TAG, String.format("handlePendingOperations(): size[%d]", mPendingOperations.size()));
128 |
129 | LinkedList pendingOperations = new LinkedList(mPendingOperations);
130 | mPendingOperations.clear();
131 |
132 | Runnable runnable = pendingOperations.pollFirst();
133 | while (runnable != null) {
134 | performOperationIfAllowed(runnable);
135 |
136 | runnable = pendingOperations.pollFirst();
137 | }
138 | }
139 |
140 | private void performOperationIfAllowed(final Runnable operation) {
141 | mUiHandler.post(new Runnable() {
142 | @Override
143 | public void run() {
144 | Log.v(TAG, String.format("performOperationIfAllowed(): isSavedStateActive[%b]", mIsSavedStateActive));
145 |
146 | if (mIsSavedStateActive) {
147 | mPendingOperations.addLast(operation);
148 | } else {
149 | operation.run();
150 | }
151 | }
152 | });
153 | }
154 |
155 | /**
156 | * Sets OnFragmentSwapperListener object that will receive notifications related to FragmentSwapper state and requests.
157 | *
158 | * @param listener OnFragmentSwapperListener object
159 | */
160 | public void setOnFragmentSwapperListener(OnFragmentSwapperListener listener) {
161 | mOnFragmentSwapperListener = listener;
162 | }
163 |
164 | private void notifyNewFragment(final F fragment) {
165 | Log.v(TAG, String.format("notifyNewFragment(): fragment[%s]", fragment != null ? fragment.getName() : null));
166 |
167 | mUiHandler.post(new Runnable() {
168 | @Override
169 | public void run() {
170 | if (mOnFragmentSwapperListener != null) {
171 | mOnFragmentSwapperListener.onFragmentEntered(SingleContainerFragmentSwapper.this, fragment);
172 | }
173 | }
174 | });
175 | }
176 |
177 | private void notifyPause(final F fragment) {
178 | Log.v(TAG, String.format("notifyPause(): fragment[%s]", fragment != null ? fragment.getName() : null));
179 |
180 | if (fragment != null) {
181 | fragment.onFragmentPause();
182 | }
183 | }
184 |
185 | private void notifyResume(final F fragment) {
186 | Log.v(TAG, String.format("notifyResume(): fragment[%s]", fragment != null ? fragment.getName() : null));
187 |
188 | if (fragment != null) {
189 | fragment.onFragmentResume();
190 | }
191 | }
192 |
193 | private void notifyCloseRequest() {
194 | Log.v(TAG, "notifyCloseRequest()");
195 |
196 | mUiHandler.post(new Runnable() {
197 | @Override
198 | public void run() {
199 | if (mOnFragmentSwapperListener != null) {
200 | mOnFragmentSwapperListener.onCloseRequested(SingleContainerFragmentSwapper.this);
201 | }
202 | }
203 | });
204 | }
205 |
206 | /**
207 | * @see pl.openrnd.managers.fragmentsswapper.FragmentSwapper
208 | */
209 | @Override
210 | public void popFragment(final PopParams popParams) {
211 | Runnable operation = new Runnable() {
212 | @Override
213 | public void run() {
214 | int stackEntries = getFragmentManager().getBackStackEntryCount();
215 | Log.v(TAG, String.format("popFragment(): entries[%d]", stackEntries));
216 |
217 | notifyPause(getCurrentFragment());
218 |
219 | obtainResultsFromCurrentFragment();
220 |
221 | if (stackEntries > 1) {
222 | setAnimationEnabled(popParams.isAnimate());
223 | getFragmentManager().popBackStack();
224 | } else {
225 | notifyCloseRequest();
226 | }
227 | }
228 | };
229 | performOperationIfAllowed(operation);
230 | }
231 |
232 | /**
233 | * @see pl.openrnd.managers.fragmentsswapper.FragmentSwapper
234 | */
235 | @Override
236 | public F getCurrentFragment() {
237 | return mContentFragment;
238 | }
239 |
240 | /**
241 | * Method must be called by holding Activity in its onBackPressed() method.
242 | *
243 | * Method pops current fragment.
244 | *
245 | * @param popParams PopParams object with pop transaction parameters.
246 | */
247 | public void onBackPressed(PopParams popParams) {
248 | Log.v(TAG, String.format("onBackPressed(): fragment[%s], animate[%b]", mContentFragment != null ? mContentFragment.getName() : null, popParams.isAnimate()));
249 |
250 | if (mContentFragment != null) {
251 | mContentFragment.onBackPressed(popParams);
252 | }
253 | }
254 |
255 | private FragmentManager getFragmentManager() {
256 | return mInitializationParams.getFragmentManager();
257 | }
258 |
259 | private boolean findCurrentFragment() {
260 | mContentFragment = (F) getFragmentManager().findFragmentById(mInitializationParams.getContentFrame());
261 |
262 | if (mContentFragment != null) {
263 | mContentFragment.assignFragmentSwapper(this);
264 | }
265 |
266 | FragmentManager fragmentManager = getFragmentManager();
267 | int entryCount = fragmentManager.getBackStackEntryCount();
268 | Log.v(TAG, String.format("findCurrentFragment()... entries [%d]", entryCount));
269 | return mContentFragment != null;
270 | }
271 |
272 | private void obtainResultsFromCurrentFragment() {
273 | FragmentDescriptor fragment = mContentFragment;
274 |
275 | mRequestCode = fragment.getRequestCode();
276 | mResultCode = fragment.getResultCode();
277 | mResultBundle = fragment.getResultData();
278 |
279 | Log.v(TAG, String.format("obtainResultsFromCurrentFragment(): current[%s], requestCode[%s], resultCode[%d], data[%b]",
280 | fragment.getName(), mRequestCode, mResultCode, mResultBundle != null));
281 | }
282 |
283 | private void sendResultsToCurrentFragmentAndClear() {
284 | Integer requestCode = mRequestCode;
285 | int resultCode = mResultCode;
286 | Bundle resultData = mResultBundle;
287 |
288 | mRequestCode = null;
289 | mResultCode = FragmentDescriptor.RESULT_CANCELED;
290 | mResultBundle = null;
291 |
292 | if (mContentFragment != null) {
293 | Log.v(TAG, String.format("sendResultsToCurrentFragmentAndClear(): current[%s], requestCode[%s], resultCode[%d], data[%b]",
294 | mContentFragment.getName(), requestCode, resultCode, resultData != null));
295 |
296 | mContentFragment.onFragmentResult(requestCode, resultCode, resultData);
297 | }
298 | }
299 |
300 | /**
301 | * @see pl.openrnd.managers.fragmentsswapper.FragmentSwapper
302 | */
303 | @Override
304 | public boolean isAnimationEnabled() {
305 | return mIsAnimationEnabled;
306 | }
307 |
308 | protected void setAnimationEnabled(boolean enabled) {
309 | Log.v(TAG, String.format("setAnimationEnabled(): enabled[%b]", enabled));
310 |
311 | mIsAnimationEnabled = enabled;
312 | }
313 |
314 | private FragmentManager.OnBackStackChangedListener mOnBackStackChangedListener = new FragmentManager.OnBackStackChangedListener() {
315 |
316 | @Override
317 | public void onBackStackChanged() {
318 | Log.w(TAG, String.format("onBackStackChanged(): popInProgress[%b]", mPopInProgress));
319 |
320 | if (mPopInProgress) {
321 | obtainResultsFromCurrentFragment();
322 | return;
323 | }
324 |
325 | int stackCount = getFragmentManager().getBackStackEntryCount();
326 |
327 | Log.v(TAG, String.format("onBackStackChanged(): lastStackCount[%d], currentStackCount[%d]", mLastStackCount, stackCount));
328 |
329 | findCurrentFragment();
330 |
331 | setAnimationEnabled(true);
332 |
333 | if (stackCount < mLastStackCount) {
334 | sendResultsToCurrentFragmentAndClear();
335 | }
336 |
337 | notifyResume(mContentFragment);
338 | notifyNewFragment(mContentFragment);
339 |
340 | mLastStackCount = stackCount;
341 | }
342 | };
343 |
344 | /**
345 | * Clears fragments back stack removing all fragments from the hierarchy.
346 | */
347 | public void clearStack() {
348 | mPopInProgress = true;
349 | setAnimationEnabled(false);
350 |
351 | FragmentManager fragmentManager = getFragmentManager();
352 | int entryCount = fragmentManager.getBackStackEntryCount();
353 | Log.v(TAG, String.format("clearStack()... entries [%d]", entryCount));
354 | for (int i = 0; i < entryCount; ++i) {
355 | try {
356 | fragmentManager.popBackStackImmediate();
357 | } catch (Exception exc) {
358 | Log.e(TAG, "clearStack()", exc);
359 | }
360 | }
361 |
362 | findCurrentFragment();
363 |
364 | Log.v(TAG, "clearStack()... DONE");
365 | setAnimationEnabled(true);
366 | mPopInProgress = false;
367 | }
368 |
369 | private boolean clearToFragmentIfFound(F fragment) {
370 | mPopInProgress = true;
371 | boolean result = getFragmentManager().popBackStackImmediate(fragment.getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
372 | mPopInProgress = false;
373 |
374 | Log.v(TAG, String.format("clearToFragmentIfFound(): fragmentName[%s], result[%b]", fragment.getName(), result));
375 |
376 | return result;
377 | }
378 |
379 | /**
380 | * @see pl.openrnd.managers.fragmentsswapper.FragmentSwapper
381 | */
382 | @Override
383 | public void swapFragment(final SwapParams swapParams, final F fragment) {
384 | Runnable operation = new Runnable() {
385 | @Override
386 | public void run() {
387 | Log.v(TAG, "swapFragment()");
388 |
389 | fragment.assignFragmentSwapper(SingleContainerFragmentSwapper.this);
390 |
391 | notifyPause(mContentFragment);
392 |
393 | mPopInProgress = true;
394 |
395 | boolean popped = false;
396 |
397 | if (swapParams.isMainContext()) {
398 | clearStack();
399 | setAnimationEnabled(swapParams.isAnimate());
400 | } else {
401 | setAnimationEnabled(swapParams.isAnimate());
402 |
403 | popped = clearToFragmentIfFound(fragment);
404 | }
405 |
406 | FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
407 | if (popped) {
408 | fragmentTransaction.setCustomAnimations(0, 0, swapParams.getPopEnterAnimResId(), swapParams.getPopExitAnimResId());
409 | } else {
410 | fragmentTransaction.setCustomAnimations(swapParams.getEnterAnimResId(), swapParams.getExitAnimResId(), swapParams.getPopEnterAnimResId(), swapParams.getPopExitAnimResId());
411 |
412 | if (mContentFragment != null) {
413 | if (swapParams.getRemoveOld()) {
414 | fragmentTransaction.remove(mContentFragment);
415 | } else {
416 | fragmentTransaction.hide(mContentFragment);
417 | }
418 | }
419 | }
420 |
421 | fragment.setRequestCode(swapParams.getRequestCode());
422 |
423 | fragmentTransaction.add(mInitializationParams.getContentFrame(), fragment);
424 | if (swapParams.isAddToBackStack()) {
425 | fragmentTransaction.addToBackStack(fragment.getName());
426 | }
427 |
428 | fragmentTransaction.commit();
429 | getFragmentManager().executePendingTransactions();
430 |
431 | if (!swapParams.isAddToBackStack()) {
432 | //force to update current fragment and notify OnFragmentSwapperListener
433 | mOnBackStackChangedListener.onBackStackChanged();
434 | }
435 | }
436 | };
437 |
438 | performOperationIfAllowed(operation);
439 | }
440 | }
441 |
--------------------------------------------------------------------------------
/fragment.swapper/src/main/java/pl/openrnd/managers/fragmentsswapper/SwapParams.java:
--------------------------------------------------------------------------------
1 | /******************************************************************************
2 | *
3 | * 2015 (C) Copyright Open-RnD Sp. z o.o.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | *
17 | ******************************************************************************/
18 |
19 | package pl.openrnd.managers.fragmentsswapper;
20 |
21 | import pl.openrnd.managers.R;
22 |
23 | /**
24 | * Class holding fragment enter transaction parameters.
25 | */
26 | public class SwapParams {
27 | private Integer mRequestCode;
28 | private boolean mAddToBackStack;
29 | private boolean mAnimate;
30 | private Integer mEnterAnimResId;
31 | private Integer mExitAnimResId;
32 | private Integer mPopEnterAnimResId;
33 | private Integer mPopExitAnimResId;
34 | private boolean mIsMainContext;
35 | private boolean mRemoveOld;
36 |
37 | /**
38 | * Gets animation resource id.
39 | *
40 | * @see android.support.v4.app.FragmentTransaction
41 | *
42 | * @return Animation resource id.
43 | */
44 | public Integer getPopExitAnimResId() {
45 | return mPopExitAnimResId;
46 | }
47 |
48 | /**
49 | * Gets fragment request code.
50 | *
51 | * @return Fragment request code or null if not provided.
52 | */
53 | public Integer getRequestCode() {
54 | return mRequestCode;
55 | }
56 |
57 | /**
58 | * Gets information if new fragment has to be added to FragmentManagers
59 | * back stack.
60 | *
61 | * @return True if fragment has to be added to back stack, false otherwise.
62 | */
63 | public boolean isAddToBackStack() {
64 | return mAddToBackStack;
65 | }
66 |
67 | /**
68 | * Gets information if animation will be enabled for the fragments transaction.
69 | *
70 | * @return True if animation is enabled, false otherwise.
71 | */
72 | public boolean isAnimate() {
73 | return mAnimate;
74 | }
75 |
76 | /**
77 | * Gets animation resource id.
78 | *
79 | * @see android.support.v4.app.FragmentTransaction
80 | *
81 | * @return Animation resource id.
82 | */
83 | public Integer getEnterAnimResId() {
84 | return mEnterAnimResId;
85 | }
86 |
87 | /**
88 | * Gets animation resource id.
89 | *
90 | * @see android.support.v4.app.FragmentTransaction
91 | *
92 | * @return Animation resource id.
93 | */
94 | public Integer getExitAnimResId() {
95 | return mExitAnimResId;
96 | }
97 |
98 | /**
99 | * Gets animation resource id.
100 | *
101 | * @see android.support.v4.app.FragmentTransaction
102 | *
103 | * @return Animation resource id.
104 | */
105 | public Integer getPopEnterAnimResId() {
106 | return mPopEnterAnimResId;
107 | }
108 |
109 | /**
110 | * Gets information if new fragment will be a main context.
111 | *
112 | * When new fragment in transaction is marked as a main context,
113 | * all previously added fragments will be removed from back stack.
114 | * User will not be able to go back to them.
115 | *
116 | * @return True if new fragment will be a main context.
117 | */
118 | public boolean isMainContext() {
119 | return mIsMainContext;
120 | }
121 |
122 | /**
123 | * Gets information about current fragment hide method.
124 | *
125 | * Current fragment can be hidden and removed. While going back to it, it will be shown again or
126 | * recreated from scratch.
127 | *
128 | * @return True if fragment has to be removed, false if fragment has to be hidden.
129 | */
130 | public boolean getRemoveOld(){
131 | return mRemoveOld;
132 | }
133 |
134 | /**
135 | * SwapParams class builder
136 | */
137 | public SwapParams(Builder builder) {
138 | mRequestCode = builder.mRequestCode;
139 | mAddToBackStack = builder.mAddToBackStack;
140 | mAnimate = builder.mAnimate;
141 | mEnterAnimResId = builder.mEnterAnimResId;
142 | mExitAnimResId = builder.mExitAnimResId;
143 | mPopEnterAnimResId = builder.mPopEnterAnimResId;
144 | mPopExitAnimResId = builder.mPopExitAnimResId;
145 | mIsMainContext = builder.mIsMainContext;
146 | mRemoveOld = builder.mRemoveOld;
147 | }
148 |
149 | /**
150 | * SwapParams class builder
151 | */
152 | public static class Builder {
153 | private Integer mRequestCode;
154 | private boolean mAddToBackStack;
155 | private boolean mAnimate;
156 | private Integer mEnterAnimResId;
157 | private Integer mExitAnimResId;
158 | private Integer mPopEnterAnimResId;
159 | private Integer mPopExitAnimResId;
160 | private boolean mIsMainContext;
161 | private boolean mRemoveOld;
162 |
163 | /**
164 | * Class constructor
165 | */
166 | public Builder() {
167 | initDefaultValues();
168 | }
169 |
170 | private void initDefaultValues() {
171 | mEnterAnimResId = R.anim.slide_left_out;
172 | mExitAnimResId = R.anim.slide_right_out;
173 | mPopEnterAnimResId = R.anim.slide_left_in;
174 | mPopExitAnimResId = R.anim.slide_right_in;
175 | mAnimate = true;
176 | mRequestCode = null;
177 | mAddToBackStack = true;
178 | mIsMainContext = false;
179 | mRemoveOld = false;
180 | }
181 |
182 | /**
183 | * Sets adding new fragment to back stack state.
184 | *
185 | * @param addToBackStack True if new fragment has to be added to back stack, false otherwise.
186 | * @return Builder object.
187 | */
188 | public Builder addToBackStack(boolean addToBackStack) {
189 | mAddToBackStack = addToBackStack;
190 | return this;
191 | }
192 |
193 | /**
194 | * Sets new fragment request code.
195 | *
196 | * @param requestCode Fragment request code.
197 | * @return Builder object.
198 | */
199 | public Builder requestCode(int requestCode) {
200 | mRequestCode = requestCode;
201 | return this;
202 | }
203 |
204 | /**
205 | * Sets transaction animate state.
206 | *
207 | * @param animate True if transactions animation is enabled, false otherwise.
208 | * @return Builder object.
209 | */
210 | public Builder animate(boolean animate){
211 | mAnimate = animate;
212 | return this;
213 | }
214 |
215 | /**
216 | * Sets animation resource id.
217 | *
218 | * @see android.support.v4.app.FragmentTransaction
219 | *
220 | * @param enterAnimResId Animation resource id.
221 | * @return Builder object.
222 | */
223 | public Builder enterAnimResId(int enterAnimResId){
224 | mEnterAnimResId = enterAnimResId;
225 | return this;
226 | }
227 |
228 | /**
229 | * Sets animation resource id.
230 | *
231 | * @see android.support.v4.app.FragmentTransaction
232 | *
233 | * @param exitAnimResId Animation resource id.
234 | * @return Builder object.
235 | */
236 | public Builder exitAnimResId(int exitAnimResId){
237 | mExitAnimResId = exitAnimResId;
238 | return this;
239 | }
240 |
241 | /**
242 | * Sets animation resource id.
243 | *
244 | * @see android.support.v4.app.FragmentTransaction
245 | *
246 | * @param popEnterAnimResId Animation resource id.
247 | * @return Builder object.
248 | */
249 | public Builder popEnterAnimResId(int popEnterAnimResId){
250 | mPopEnterAnimResId = popEnterAnimResId;
251 | return this;
252 | }
253 |
254 | /**
255 | * Sets animation resource id.
256 | *
257 | * @see android.support.v4.app.FragmentTransaction
258 | *
259 | * @param popExitAnimResId Animation resource id.
260 | * @return Builder object.
261 | */
262 | public Builder popExitAnimResId(int popExitAnimResId){
263 | mPopExitAnimResId = popExitAnimResId;
264 | return this;
265 | }
266 |
267 | /**
268 | * Sets information if new fragment will be a main context.
269 | *
270 | * When new fragment in transaction is marked as a main context,
271 | * all previously added fragments will be removed from back stack.
272 | * User will not be able to go back to them.
273 | *
274 | * @param mainContext True if new fragment is a main context fragment, false otherwise.
275 | * @return Builder object.
276 | */
277 | public Builder mainContext(boolean mainContext) {
278 | mIsMainContext = mainContext;
279 | return this;
280 | }
281 |
282 | /**
283 | * Sets information about current fragment hide method.
284 | *
285 | * Current fragment can be hidden and removed. While going back to it, it will be shown again or
286 | * recreated from scratch.
287 | *
288 | * @param removeOld True if current fragment has to be removed, false if has to be hidden.
289 | * @return Builder object.
290 | */
291 | public Builder removeOld(boolean removeOld){
292 | mRemoveOld = removeOld;
293 | return this;
294 | }
295 |
296 | /**
297 | * Builds SwapParams object with current builder parameters.
298 | *
299 | * @return SwapParams object.
300 | */
301 | public SwapParams build() {
302 | SwapParams swapParams = new SwapParams(this);
303 | return swapParams;
304 | }
305 | }
306 | }
307 |
308 |
--------------------------------------------------------------------------------
/fragment.swapper/src/main/res/anim/slide_down_in.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/fragment.swapper/src/main/res/anim/slide_down_out.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/fragment.swapper/src/main/res/anim/slide_left_in.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/fragment.swapper/src/main/res/anim/slide_left_out.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/fragment.swapper/src/main/res/anim/slide_right_in.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/fragment.swapper/src/main/res/anim/slide_right_out.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/fragment.swapper/src/main/res/anim/slide_up_in.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/fragment.swapper/src/main/res/anim/slide_up_out.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/fragment.swapper/src/main/res/values/integers.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 400
4 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':fragment.swapper', ':fragment.swapper.sample.app'
2 |
--------------------------------------------------------------------------------