gpt4 book ai didi

android - 每次屏幕打开时重新创建应用程序类

转载 作者:行者123 更新时间:2023-11-29 01:25:59 29 4
gpt4 key购买 nike

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

1. 我启动 android 应用程序。
2. 我按主页按钮将其置于背景。
3. 我关掉屏幕。
4. 几秒钟后我打开屏幕。
5. 我的应用程序突然变成前台。我意识到应用程序被迫再次启动,我使用的应用程序类被再次创建,然后显示我的应用程序的起始页面(这是登录页面),尽管在第 2 步。

到目前为止我已经尝试过的如下;

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

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

我也尝试过使用 screenOrientation="portrait"但没有成功。

我的问题与 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:******************

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

最佳答案

问题出在我使用的库上。它在后台工作并使应用程序重新启动。
我在应用程序进入后台时暂停它,并在它进入前台时恢复它。就这样,问题解决了。
无论如何谢谢你。

关于android - 每次屏幕打开时重新创建应用程序类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34061388/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com