├── app
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── values
│ │ │ ├── strings.xml
│ │ │ ├── colors.xml
│ │ │ └── styles.xml
│ │ ├── mipmap-hdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-anydpi-v26
│ │ │ ├── ic_launcher.xml
│ │ │ └── ic_launcher_round.xml
│ │ ├── layout
│ │ │ ├── fragment_child.xml
│ │ │ ├── fragment_main.xml
│ │ │ └── activity_main.xml
│ │ ├── drawable-v24
│ │ │ └── ic_launcher_foreground.xml
│ │ └── drawable
│ │ │ └── ic_launcher_background.xml
│ │ ├── java
│ │ └── com
│ │ │ └── zuluft
│ │ │ └── sample
│ │ │ ├── ChildFragment.java
│ │ │ ├── TestFragment.java
│ │ │ ├── MainActivity.java
│ │ │ └── MainFragment.java
│ │ └── AndroidManifest.xml
├── proguard-rules.pro
└── build.gradle
├── impl
├── .gitignore
├── src
│ └── main
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── zuluft
│ │ └── impl
│ │ ├── CustomNonConfigInstance.java
│ │ ├── SafeFragmentTransactorFragment.java
│ │ └── SafeFragmentTransactorActivity.java
├── build.gradle
└── proguard-rules.pro
├── lib
├── .gitignore
├── src
│ └── main
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── zuluft
│ │ └── safeFragmentTransaction
│ │ └── SafeFragmentTransaction.java
├── build.gradle
└── proguard-rules.pro
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .idea
├── encodings.xml
├── vcs.xml
├── runConfigurations.xml
├── modules.xml
├── gradle.xml
└── misc.xml
├── .gitignore
├── gradle.properties
├── README.md
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/impl/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/lib/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':lib', ':impl'
2 |
--------------------------------------------------------------------------------
/impl/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/lib/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | SafeFragmentTransaction
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Zuluft/SafeFragmentTransaction/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Zuluft/SafeFragmentTransaction/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Zuluft/SafeFragmentTransaction/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Zuluft/SafeFragmentTransaction/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Zuluft/SafeFragmentTransaction/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Zuluft/SafeFragmentTransaction/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Zuluft/SafeFragmentTransaction/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Zuluft/SafeFragmentTransaction/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Zuluft/SafeFragmentTransaction/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Zuluft/SafeFragmentTransaction/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Zuluft/SafeFragmentTransaction/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Thu Apr 25 12:29:07 GET 2019
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_child.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/impl/src/main/java/com/zuluft/impl/CustomNonConfigInstance.java:
--------------------------------------------------------------------------------
1 | package com.zuluft.impl;
2 |
3 |
4 |
5 | import androidx.annotation.Nullable;
6 |
7 | import com.zuluft.safeFragmentTransaction.SafeFragmentTransaction;
8 |
9 | final class CustomNonConfigInstance {
10 |
11 | final SafeFragmentTransaction safeFragmentTransaction;
12 | final Object other;
13 |
14 | CustomNonConfigInstance(@Nullable final SafeFragmentTransaction safeFragmentTransaction,
15 | @Nullable final Object other) {
16 | this.safeFragmentTransaction = safeFragmentTransaction;
17 | this.other = other;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/app/src/main/java/com/zuluft/sample/ChildFragment.java:
--------------------------------------------------------------------------------
1 | package com.zuluft.sample;
2 |
3 | import android.os.Bundle;
4 | import android.view.LayoutInflater;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 |
8 | import androidx.annotation.NonNull;
9 | import androidx.annotation.Nullable;
10 | import androidx.fragment.app.Fragment;
11 |
12 | public class ChildFragment
13 | extends
14 | Fragment {
15 |
16 | @Nullable
17 | @Override
18 | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
19 | return inflater.inflate(R.layout.fragment_child, container, false);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/lib/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 |
4 | group = 'com.github.Zuluft.SafeFragmentTransaction.lib'
5 |
6 | android {
7 | compileSdkVersion 28
8 |
9 | defaultConfig {
10 | minSdkVersion 14
11 | targetSdkVersion 28
12 | versionCode 1
13 | versionName "1.0"
14 | }
15 |
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 |
23 | }
24 |
25 | dependencies {
26 | implementation fileTree(dir: 'libs', include: ['*.jar'])
27 | api 'androidx.appcompat:appcompat:1.0.2'
28 | }
29 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
12 |
13 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/impl/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 |
4 | group = 'com.github.Zuluft.SafeFragmentTransaction.impl'
5 |
6 | android {
7 | compileSdkVersion 28
8 |
9 | defaultConfig {
10 | minSdkVersion 14
11 | targetSdkVersion 28
12 | versionCode 1
13 | versionName "1.0"
14 | }
15 |
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 | }
23 |
24 | dependencies {
25 | implementation fileTree(include: ['*.jar'], dir: 'libs')
26 | api 'androidx.appcompat:appcompat:1.0.2'
27 | implementation project(':lib')
28 | }
29 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/impl/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/lib/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 28
5 | defaultConfig {
6 | applicationId "com.zuluft.safefragmenttransaction"
7 | minSdkVersion 14
8 | targetSdkVersion 28
9 | versionCode 1
10 | versionName "1.0"
11 | }
12 | buildTypes {
13 | release {
14 | minifyEnabled false
15 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
16 | }
17 | }
18 |
19 | compileOptions {
20 | sourceCompatibility JavaVersion.VERSION_1_8
21 | targetCompatibility JavaVersion.VERSION_1_8
22 | }
23 | }
24 |
25 | dependencies {
26 | implementation fileTree(include: ['*.jar'], dir: 'libs')
27 | api 'androidx.appcompat:appcompat:1.0.2'
28 | implementation project(':impl')
29 | implementation project(':lib')
30 | }
31 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/app/src/main/java/com/zuluft/sample/TestFragment.java:
--------------------------------------------------------------------------------
1 | package com.zuluft.sample;
2 |
3 |
4 | import android.os.Bundle;
5 | import android.view.LayoutInflater;
6 | import android.view.View;
7 | import android.view.ViewGroup;
8 |
9 | import androidx.annotation.NonNull;
10 | import androidx.annotation.Nullable;
11 | import androidx.fragment.app.Fragment;
12 |
13 | public class TestFragment
14 | extends
15 | Fragment {
16 |
17 | @Nullable
18 | @Override
19 | public View onCreateView(@NonNull LayoutInflater inflater,
20 | @Nullable ViewGroup container, Bundle savedInstanceState) {
21 | return inflater.inflate(R.layout.fragment_main, container, false);
22 | }
23 |
24 | @SuppressWarnings("WeakerAccess")
25 | public static TestFragment newInstance() {
26 | Bundle args = new Bundle();
27 | TestFragment fragment = new TestFragment();
28 | fragment.setArguments(args);
29 | return fragment;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/app/src/main/java/com/zuluft/sample/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.zuluft.sample;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 |
6 | import com.zuluft.impl.SafeFragmentTransactorActivity;
7 |
8 |
9 | public class MainActivity
10 | extends
11 | SafeFragmentTransactorActivity {
12 |
13 | @Override
14 | protected void onCreate(Bundle savedInstanceState) {
15 | super.onCreate(savedInstanceState);
16 | setContentView(R.layout.activity_main);
17 | if (getSupportFragmentManager().findFragmentById(R.id.flMainContent) == null) {
18 | drawFragment();
19 | }
20 | }
21 |
22 | private void drawFragment() {
23 | getSafeFragmentTransaction().registerFragmentTransition(fragmentManager ->
24 | fragmentManager.beginTransaction().add(R.id.flMainContent, new MainFragment())
25 | .commit());
26 | }
27 |
28 | public void onNextClicked(View view) {
29 | getSafeFragmentTransaction().registerFragmentTransition(fragmentManager ->
30 | fragmentManager.beginTransaction().replace(R.id.flMainContent,
31 | TestFragment.newInstance(), "TAG2")
32 | .addToBackStack(null)
33 | .commit());
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/impl/src/main/java/com/zuluft/impl/SafeFragmentTransactorFragment.java:
--------------------------------------------------------------------------------
1 | package com.zuluft.impl;
2 |
3 | import android.os.Bundle;
4 |
5 | import androidx.annotation.Nullable;
6 | import androidx.fragment.app.Fragment;
7 |
8 | import com.zuluft.safeFragmentTransaction.SafeFragmentTransaction;
9 |
10 | public abstract class SafeFragmentTransactorFragment
11 | extends
12 | Fragment {
13 |
14 | private SafeFragmentTransaction mSafeFragmentTransaction;
15 |
16 | @Override
17 | public void onCreate(@Nullable Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 | setRetainInstance(true);
20 | }
21 |
22 | @Override
23 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
24 | super.onActivityCreated(savedInstanceState);
25 | if (mSafeFragmentTransaction == null) {
26 | mSafeFragmentTransaction = SafeFragmentTransaction
27 | .createInstance();
28 | }
29 | mSafeFragmentTransaction.attachLifecycle(getLifecycle(), getChildFragmentManager());
30 | }
31 |
32 |
33 | @SuppressWarnings({"unused", "WeakerAccess"})
34 | public final SafeFragmentTransaction getSafeFragmentTransaction() {
35 | if (mSafeFragmentTransaction == null) {
36 | throw new RuntimeException(
37 | "getSafeFragmentTransaction() method was called before super.onActivityCreated"
38 | );
39 | }
40 | return mSafeFragmentTransaction;
41 | }
42 |
43 | @Override
44 | public void onDestroyView() {
45 | if (mSafeFragmentTransaction != null) {
46 | mSafeFragmentTransaction.detachLifecycle();
47 | }
48 | super.onDestroyView();
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/app/src/main/java/com/zuluft/sample/MainFragment.java:
--------------------------------------------------------------------------------
1 | package com.zuluft.sample;
2 |
3 | import android.os.Bundle;
4 | import android.os.Handler;
5 | import android.util.Log;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 |
10 | import androidx.annotation.NonNull;
11 | import androidx.annotation.Nullable;
12 |
13 | import com.zuluft.impl.SafeFragmentTransactorFragment;
14 |
15 |
16 | public class MainFragment
17 | extends
18 | SafeFragmentTransactorFragment {
19 |
20 | private static final String TAG = "MainFragment";
21 |
22 | @Nullable
23 | @Override
24 | public View onCreateView(@NonNull LayoutInflater inflater,
25 | @Nullable ViewGroup container,
26 | @Nullable Bundle savedInstanceState) {
27 | return inflater.inflate(R.layout.fragment_main, container, false);
28 | }
29 |
30 | @Override
31 | public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
32 | super.onViewCreated(view, savedInstanceState);
33 | Log.d(TAG, "onViewCreated: ");
34 |
35 | }
36 |
37 | @Override
38 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
39 | super.onActivityCreated(savedInstanceState);
40 | if (getChildFragmentManager().findFragmentById(R.id.flChildContent) == null) {
41 | new Handler().postDelayed(this::drawFragment, 5000);
42 | }
43 | }
44 |
45 | private void drawFragment() {
46 | getSafeFragmentTransaction().registerFragmentTransition(fragmentManager ->
47 | fragmentManager.beginTransaction().add(R.id.flChildContent, new ChildFragment())
48 | .commit());
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/impl/src/main/java/com/zuluft/impl/SafeFragmentTransactorActivity.java:
--------------------------------------------------------------------------------
1 | package com.zuluft.impl;
2 |
3 |
4 | import android.os.Bundle;
5 |
6 | import androidx.annotation.Nullable;
7 | import androidx.appcompat.app.AppCompatActivity;
8 |
9 | import com.zuluft.safeFragmentTransaction.SafeFragmentTransaction;
10 |
11 | public abstract class SafeFragmentTransactorActivity
12 | extends
13 | AppCompatActivity {
14 |
15 | private SafeFragmentTransaction mSafeFragmentTransaction;
16 |
17 | @Override
18 | protected void onCreate(@Nullable Bundle savedInstanceState) {
19 | super.onCreate(savedInstanceState);
20 | Object nonConfigInstance = getLastCustomNonConfigurationInstance();
21 | if (nonConfigInstance == null || ((CustomNonConfigInstance) nonConfigInstance)
22 | .safeFragmentTransaction == null) {
23 | mSafeFragmentTransaction = SafeFragmentTransaction
24 | .createInstance();
25 | } else {
26 | mSafeFragmentTransaction = ((CustomNonConfigInstance) nonConfigInstance)
27 | .safeFragmentTransaction;
28 | }
29 | mSafeFragmentTransaction.attachLifecycle(getLifecycle(),
30 | getSupportFragmentManager());
31 | }
32 |
33 | @SuppressWarnings("unused")
34 | public final SafeFragmentTransaction getSafeFragmentTransaction() {
35 | if (mSafeFragmentTransaction == null) {
36 | throw new RuntimeException(
37 | "getSafeFragmentTransaction() method was called before super.onCreate"
38 | );
39 | }
40 | return mSafeFragmentTransaction;
41 | }
42 |
43 |
44 | public Object onRetainOtherNonConfigInstance() {
45 | return null;
46 | }
47 |
48 | @SuppressWarnings("unused")
49 | @Nullable
50 | public final Object getLastOtherNonConfigInstance() {
51 | return ((CustomNonConfigInstance) getLastCustomNonConfigurationInstance()).other;
52 | }
53 |
54 | @Override
55 | public final Object onRetainCustomNonConfigurationInstance() {
56 | return new CustomNonConfigInstance(
57 | mSafeFragmentTransaction,
58 | onRetainOtherNonConfigInstance()
59 | );
60 | }
61 |
62 | @Override
63 | protected void onDestroy() {
64 | super.onDestroy();
65 | if (mSafeFragmentTransaction != null) {
66 | mSafeFragmentTransaction.detachLifecycle();
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## SafeFragmentTransaction
2 | I'm sure that every android developer has faced exception
3 |
4 | ```java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState```
5 |
6 | after ```fragmentTransaction.commit()``` or ```fragmentManager.popBackStack(...)``` when activity is paused. To avoid this, use ```SafeFragmentTransaction```. If activity is paused and fragment transactions are requested, ```SafeFragmentTransaction``` ensures that these transactions will be executed after activity comes alive. So there are no crashes and life is beautiful.
7 | It's very very easy to use:
8 |
9 | ## Gradle:
10 | Add it in your root build.gradle at the end of repositories:
11 | ```Groovy
12 | allprojects {
13 | repositories {
14 | ...
15 | maven { url 'https://jitpack.io' }
16 | }
17 | }
18 | ```
19 | Add dependency to app gradle:
20 | ```Groovy
21 | implementation 'com.github.Zuluft:SafeFragmentTransaction:v1.0'
22 | ```
23 |
24 | ## Step 1:
25 |
26 | Simply extend your activity by ```SafeFragmentTransactorActivity```
27 |
28 | or
29 |
30 | Write the following in your base activity:
31 | ```Java
32 | ...
33 | private SafeFragmentTransaction mSafeFragmentTransaction;
34 |
35 | @Override
36 | protected void onCreate(@Nullable Bundle savedInstanceState) {
37 | super.onCreate(savedInstanceState);
38 | mSafeFragmentTransaction = SafeFragmentTransaction.createInstance(getLifecycle(),
39 | getSupportFragmentManager());
40 | getLifecycle().addObserver(mSafeFragmentTransaction);
41 | }
42 |
43 | public final SafeFragmentTransaction getSafeFragmentTransaction() {
44 | return mSafeFragmentTransaction;
45 | }
46 | ...
47 | ```
48 |
49 | ## Step 2:
50 |
51 | When adding, replacing or poping fragments, write the following:
52 | ```Java
53 | ...
54 | getSafeFragmentTransaction().registerFragmentTransition(fragmentManager ->
55 | fragmentManager.popBackStackImmediate()
56 | fragmentManager.beginTransaction()
57 | .add(R.id.content1, new Fragment1(), "TAG1")
58 | .replace(R.id.content2, new Fragment2(), "TAG2")
59 | .commit());
60 | ...
61 | ```
62 |
63 | You can use ```SafeFragmentTransaction``` for child fragments too. Write the following in ```onCreateView(...)``` of your fragment:
64 |
65 | ```Java
66 | mSafeFragmentTransaction = SafeFragmentTransaction.createInstance(getLifecycle(),
67 | getChildFragmentManager());
68 | getLifecycle().addObserver(mSafeFragmentTransaction);
69 | ```
70 | And use it as it is shown in ```step 2```.
71 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/zuluft/safeFragmentTransaction/SafeFragmentTransaction.java:
--------------------------------------------------------------------------------
1 | package com.zuluft.safeFragmentTransaction;
2 |
3 |
4 |
5 |
6 | import androidx.annotation.NonNull;
7 | import androidx.fragment.app.FragmentManager;
8 | import androidx.lifecycle.Lifecycle;
9 | import androidx.lifecycle.LifecycleObserver;
10 | import androidx.lifecycle.OnLifecycleEvent;
11 |
12 | import java.util.Stack;
13 |
14 | public class SafeFragmentTransaction
15 | implements
16 | LifecycleObserver {
17 |
18 | private Lifecycle mLifecycle;
19 | private FragmentManager mFragmentManager;
20 | private final Stack mFragmentTransitionsStack;
21 |
22 | public static SafeFragmentTransaction createInstance() {
23 | return new SafeFragmentTransaction();
24 | }
25 |
26 | private SafeFragmentTransaction() {
27 | this.mFragmentTransitionsStack = new Stack<>();
28 | }
29 |
30 | private void onTransitionRegistered(SafeFragmentTransaction
31 | .TransitionHandler transition) {
32 | mFragmentTransitionsStack.add(transition);
33 | if (mLifecycle != null && mFragmentManager != null &&
34 | mLifecycle.getCurrentState()
35 | .isAtLeast(Lifecycle.State.RESUMED)) {
36 | doTransactions();
37 | }
38 | }
39 |
40 | @SuppressWarnings("unused")
41 | @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
42 | void onReadyToDoPendingTransactions() {
43 | doTransactions();
44 | }
45 |
46 | private synchronized void doTransactions() {
47 | while (mFragmentTransitionsStack.size() != 0) {
48 | TransitionHandler transition = mFragmentTransitionsStack.pop();
49 | transition.onTransitionAvailable(mFragmentManager);
50 | }
51 | }
52 |
53 | public void registerFragmentTransition(TransitionHandler transitionHandler) {
54 | onTransitionRegistered(transitionHandler);
55 | }
56 |
57 | public void detachLifecycle() {
58 | mLifecycle.removeObserver(this);
59 | mLifecycle = null;
60 | mFragmentManager = null;
61 | }
62 |
63 | public void attachLifecycle(@NonNull final Lifecycle lifecycle,
64 | @NonNull final FragmentManager fragmentManager) {
65 | this.mLifecycle = lifecycle;
66 | this.mFragmentManager = fragmentManager;
67 | lifecycle.addObserver(this);
68 | }
69 |
70 | @FunctionalInterface
71 | public interface TransitionHandler {
72 | void onTransitionAvailable(final FragmentManager fragmentManager);
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------