2

解釋我面臨的問題的步驟如下所示;

1.我啓動了android應用程序。
2.我按回家按鈕把它放在backgorund上。
3.我關閉了屏幕。
4.我在幾秒鐘後打開屏幕。
5.我的應用程序突然變成了前景。我意識到,應用程序被迫重新開始,而我使用的再次創建的應用程序類,然後我的應用程序的起始頁(這是登錄頁)顯示雖然第2步:
之前打開的頁面
我到目前爲止所嘗試的是如下;

我把所有的activiy標籤configChanges和screenOrientation在我的AndroidManifest.xml(我試圖把它們逐個兩者也在一起。)每次屏幕顯示時重新創建應用程序類別

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

我還試圖用screenOrientation =「畫像「但沒有奏效。

我的問題是與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:****************

我使用的片段,也許這將是原因,但我不確定。

+2

你在說「應用程序」還是「活動」? – skywall

+1

我們可以得到啓動的Activity的代碼嗎? –

+0

應用程序類。並且由於重新創建應用程序類的結果,最後一個活動被銷燬,並且作爲應用程序的開始活動的登錄活動被創建並再次顯示。 – ondermerol

回答

1

問題出在我使用過的圖書館。它在後臺運行並使應用程序重新啓動。
當應用程序進入後臺時,我將其掛起,並在它到達時重新開始。就是這樣,問題就解決了。
無論如何,謝謝你。

相關問題