每次屏幕亮起时都会重新创建应用程序类

时间:2015-12-03 08:46:28

标签: android android-activity screen-orientation android-lifecycle activity-lifecycle

解释我遇到的问题的步骤如下:

1。我启动了android应用程序 2。我按回车键将其放在backgorund上 3。我关闭了屏幕 4. 我在几秒钟后打开屏幕 5. 我的应用程序突然成为前景。我意识到应用程序被强制重新启动,我再次创建了我使用的应用程序类,然后显示了我的应用程序的起始页面(这是登录页面),尽管在第2步。

到目前为止我所尝试的内容如下;

我把configChanges和screenOrientation放在我的Androidmanifest.xml中的所有activiy标签中(我试图将它们一个一个地放在一起,也都放在一起。)

 <activity android:configChanges="orientation|keyboardHidden|screenSize"
                android:name="com.myproject.Activity"
                android:screenOrientation="nosensor"/>

我也试过使用screenOrientation =&#34; portrait&#34;但是没有工作。

我的问题与How do I disable orientation change on Android?Prevent Android activity from being recreated on turning screen off和其他问题有关,但我找不到问题的解决方案。

你能帮忙吗?

编辑1:******************************** *
的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myproject"
    android:versionCode="1"
    android:versionName="1.0"
    xmlns:tools="http://schemas.android.com/tools" >

    <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />

    <!-- Permission to use NFC -->
    <uses-permission android:name="android.permission.NFC" />

    <uses-feature
        android:name="android.hardware.nfc.hce"
        android:required="true" />

    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

    <permission android:name="com.myproject.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.myproject.permission.C2D_MESSAGE" />

    <uses-feature android:name="android.hardware.camera" />

    <uses-permission android:name="android.permission.CAMERA" />

    <!--
        Add this permission to check which network access properties (e.g.
        active type: 3G/WiFi).
    -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <!-- Add this permission to access WLAN MAC address. -->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <!-- Add this permission to access HW ID. -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <application
        android:name="com.myproject.hce.MyApplication"
        android:hardwareAccelerated="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/MyAppTheme"
        tools:replace="android:icon,android:theme"
        >

        <!-- Splash Activity -->
        <activity android:configChanges="orientation|keyboardHidden|screenSize"
            android:name=".Splash"
            android:theme="@android:style/Theme.Holo.Light.NoActionBar"
            android:windowSoftInputMode="stateHidden"
            android:screenOrientation="nosensor" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Login Activity -->

        <activity android:configChanges="orientation|keyboardHidden|screenSize"
            android:name="com.myproject.LoginView"
            android:screenOrientation="nosensor"
            android:windowSoftInputMode="adjustResize" >
        </activity>

        <activity android:configChanges="orientation|keyboardHidden|screenSize"
            android:name="com.myproject.MainView"
            android:screenOrientation="nosensor" >
        </activity>

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/fb_app_id" />

        <meta-data
            android:name="com.crashlytics.ApiKey"
            android:value="12h3g21h4v32hv43hv4" />

        <service
            android:name="com.myproject.pushnotifications.GCMIntentService"
            android:enabled="true" >
        </service>
        <service
            android:name="com.myproject.hce.McbpHceService"
            android:exported="true"
            android:permission="android.permission.BIND_NFC_SERVICE" >
            <intent-filter>
                <action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE" />
            </intent-filter>

            <meta-data
                android:name="android.nfc.cardemulation.host_apdu_service"
                android:resource="@xml/apduservice" />
        </service>

        <receiver
            android:name="com.myproject.pushnotifications.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RETRY" />
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </receiver>

        <service
            android:name="com.myproject.hce.MyService"
            android:exported="true"
            android:enabled="true"
            android:stopWithTask="false"/>

    </application>

</manifest>


申请类

public class MyApplication extends Application {

    private static MyApplication MyApplication;

    private MyActivityLifeCycleCallbacks MyActivityLifeCycleCallbacks;

    public static MyApplication getMyApplication() {
        return MyApplication;
    }

    @Override
    public void onCreate() { //coko1

        MyLog.i(UtilConstants.LOG_TAG_HCE, "MyApplication " + "onCreate ...");

        if (MyApplication == null)
            MyApplication = this;
        else
            return;

        MyLog.i(UtilConstants.LOG_TAG_HCE, "MyApplication " + "super.onCreate() ...");

        super.onCreate();

        registerActivityLifecycleCallbacks(new MyLifecycleHandler());

        setMyActivityLifeCycleCallbacks(new MyActivityLifeCycleCallbacks());

        registerActivityLifecycleCallbacks(getMyActivityLifeCycleCallbacks());
    }

    public MyActivityLifeCycleCallbacks getMyActivityLifeCycleCallbacks() {
        return MyActivityLifeCycleCallbacks;
    }

    public void setMyActivityLifeCycleCallbacks(
            MyActivityLifeCycleCallbacks MyActivityLifeCycleCallbacks) {
        this.MyActivityLifeCycleCallbacks = MyActivityLifeCycleCallbacks;
    }
}

编辑2:****************

Splash.java

public class Splash extends Activity {

    public static Context context;

    public static final String LOG_TAG = "Splash ";


    @Override
    public void onCreate(Bundle bundle)
    {
        MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "onCreate...");

        Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this)); // catch unexpected error
        super.onCreate(bundle);
        context = this;

        MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "MainView starts...");
        Intent mainIntent = new Intent(Splash.this, MainView.class);
        Splash.this.startActivity(mainIntent);
        Splash.this.finish();
    }


    @Override
    protected void onResume() {
        MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "onResume ...");
        super.onResume();
    }
}

MainView.class

public class MainView extends Activity {


    Context context;
    String LOG_TAG = "MainView ";

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "onCreate ...");
        super.onCreate(savedInstanceState);
        Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
        context = this;

        MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "LoginView starts...");
        Intent loginIntent = new Intent(Splash.this, LoginView.class);
        Splash.this.startActivity(loginIntent);
        Splash.this.finish();
    }

    @Override
    protected void onResume() {
        MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "onResume ...");
        super.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
        MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "onStart ...");
    };

    @Override
    protected void onPause() {
        super.onPause();
        MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "onPause ...");
    };

    @Override
    protected void onDestroy() {
        MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "onDestroy ...");
        super.onDestroy();
    };
}

编辑3:****************

我使用碎片,也许这就是原因,但我不确定。

1 个答案:

答案 0 :(得分:1)

问题是由于我使用的库。它在后台运行并使应用程序重新启动。
当应用程序进入后台时我暂停它,并在它来到foregorund时恢复它。就是这样,问题就解决了 无论如何,谢谢你。