Столкнулся с проблемой.OnStart Onresume метод не вызывается после разблокировки устройства - PullRequest
0 голосов
/ 17 июня 2019

По какой-то причине в новом проекте, когда устройство заблокировано / разблокировано, жизненный цикл действия и фрагмента ведет себя не совсем корректно. При блокировке вызывается только onPause. При разблокировке ничего не вызывается. Как это можно исправить?

    apply plugin: 'com.android.application'

    android {
        compileSdkVersion 27
        defaultConfig {
            applicationId "com.iteco.testfragmentactivity"
            minSdkVersion 21
            targetSdkVersion 27
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            ndk {
                abiFilters "armeabi-v7a", "x86", 'armeabi', 'arm64-v8a'
            }
            packagingOptions {
                exclude '/lib/mips64/**'
                exclude '/lib/arm64-v8a/**'
                exclude '/lib/x86_64/**'
                exclude 'META-INF/rxjava.properties'
            }
            javaCompileOptions {
                annotationProcessorOptions {
                    arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
                }
            }
        }

        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }

        testOptions {
            unitTests {
                returnDefaultValues = true
            }
        }

        lintOptions {
            abortOnError false
        }

        aaptOptions {
            cruncherEnabled = true
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
    }

    dependencies {
        def android_rx_version = '2.1.1'
        def retrofit_version = '2.3.0'

        implementation "android.arch.persistence.room:runtime:1.0.0"
        annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
        implementation 'com.android.support:support-annotations:27.1.0'
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.github.traphan:material-calendarview:1.19'
        implementation 'com.android.support:appcompat-v7:27.1.0'
        implementation 'com.android.support:support-v4:27.1.0'
        implementation 'com.android.support:design:27.1.0'
        implementation 'com.android.support.constraint:constraint-layout:1.1.3'
        testImplementation 'junit:junit:4.12'
        implementation 'com.jakewharton:butterknife:8.8.1'
        annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
        implementation 'com.github.traphan:PinBlockLib:1.1'
        implementation 'com.android.support:recyclerview-v7:27.1.0'
        implementation 'com.jakewharton.timber:timber:4.6.0'
        implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
        implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
        implementation 'com.squareup.okhttp3:okhttp:3.9.1'
        implementation group: 'io.reactivex.rxjava2', name: 'rxjava', version: "$android_rx_version"
        implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'
        implementation 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
        implementation 'io.reactivex:rxandroid:1.2.1'
        implementation "android.arch.lifecycle:extensions:1.1.0"
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
        implementation 'com.android.support:cardview-v7:27.1.0'
    }
    import android.arch.lifecycle.Lifecycle;
    import android.arch.lifecycle.LifecycleOwner;
    import android.arch.lifecycle.LifecycleRegistry;
    import android.arch.lifecycle.ViewModelProviders;
    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import static com.iteco.testfragmentactivity.MainActivity.TAG_LOG;

    public class CommonFragment extends Fragment implements LifecycleOwner {

        private LifecycleRegistry lifecycleRegistry;
        private CommonViewModel mViewModel;

        public static CommonFragment newInstance() {
            return new CommonFragment();
        }

        public CommonFragment() {
            lifecycleRegistry = new LifecycleRegistry(this);
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.d(TAG_LOG, this.toString() + "onCreate");
            lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
        }

        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                                 @Nullable Bundle savedInstanceState) {
            Log.d(TAG_LOG, this.toString() + "onCreateView");
            return inflater.inflate(R.layout.common_fragment, container, false);
        }

        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            Log.d(TAG_LOG, this.toString() + "onActivityCreated");
            mViewModel = ViewModelProviders.of(this).get(CommonViewModel.class);
            // TODO: Use the ViewModel
        }

        @Override
        public void onDestroyView() {
            Log.d(TAG_LOG, this.toString() + "onDestroyView");
            mViewModel.onCleared();
            super.onDestroyView();
        }

        @NonNull
        @Override
        public Lifecycle getLifecycle() {
            return lifecycleRegistry;
        }

        @Override
        public void onStart() {
            super.onStart();
            //more code here
            Log.d(TAG_LOG, this.toString() + "onStart");
            lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
        }

        @Override
        public void onResume() {
            super.onResume();
            //more code here
            Log.d(TAG_LOG, this.toString() + "onResume");
            lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
        }

        @Override
        public void onPause() {
            super.onPause();
            //more code here
            Log.d(TAG_LOG, this.toString() + "onPause");
            lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE);
        }

        @Override
        public void onStop() {
            super.onStop();
            //more code here
            Log.d(TAG_LOG, this.toString() + "onStop");
            lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            //more code here
            Log.d(TAG_LOG, this.toString() + "onDestroy");
            lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
        }

    }
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import butterknife.ButterKnife;
    import static com.iteco.testfragmentactivity.MainActivity.TAG_LOG;
    public class HostActivity extends AppCompatActivity {

        private FragmentTransaction fragmentTransaction;
        private FragmentManager fragmentManager;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_host);
            ButterKnife.bind(this);
            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
            commitRegisterOfTaskFragment();
        }

        public void commitRegisterOfTaskFragment() {
            CommonFragment commonFragment = new CommonFragment();
            fragmentTransaction.replace(R.id.host, commonFragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }

    }
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.iteco.testfragmentactivity">
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".HostActivity"
                android:configChanges="orientation"></activity>
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    </manifest>

Это тестовый код. Код не подходит для меня. Эта проблема является критической для меня.

...