当 phone 从睡眠中醒来时加载 app/activity

Load app/activity when phone wakes up from sleep

我有一个 android activity,但是当 phone 进入休眠状态时(这意味着我将 phone 放在那里几秒钟,然后屏幕黑色),然后我重新打开它,activity/app 消失了(它仍然处于活动状态,但我必须按概览按钮才能返回到 activity/app)。我如何让它自动恢复?

我想要做的是当 phone 进入休眠状态时,当我重新打开它时,app/activity 会像进入休眠状态时一样存在。我检查了 onResume、BroadcastReceivers、WakeLock、KeepScreenOn、Services,但我知道我做的不对。

OnResume 不起作用,WakeLock 不起作用,KeepScreenOn,只是保持屏幕打开并且不允许 phone 休眠,我没有尝试过服务和 BroadcastReceivers,但我认为还是先问这里吧

请帮忙。谢谢。

我有 MainActivity.java,它最初打开然后开始 AdminAddMerchantActivity.java。 AdminAddMerchantActivity.java 是一个 navigationView,它启动了 4 个片段,包括 TimeFragment.java,它具有选项卡布局、视图分页器和分页器适配器。 TimeFragment.java 开始 5 个片段,包括 PriceFragment.java。 以下是下面的活动生命周期方法。

MainActivity.java:

...
@Override
    protected void onPause() {
        super.onPause();
        Log.d("state", "Pausing Main");
        // Handle countdown stop here
    }

    @Override
    protected void onResume() {
        super.onResume();

            Log.d("state", "Resuming Main");
        currentActivity = sharedPreferences.getString(CURRENT_ACT, "main");
        if(mAuth.getCurrentUser() != null)
        {
            if(currentActivity.equals("confirmFinalOrder"))
            {
                isResumed++;
                Intent intent = new Intent(MainActivity.this, ConfirmFinalOrderActivity.class);
                startActivity(intent);
                finish();
            }
            else if(currentActivity.equals("merchantDetails"))
            {
                isResumed++;
                Intent intent = new Intent(MainActivity.this, MerchantDetailsActivity.class);
                intent.putExtra("mid", sharedPreferences.getString("merchantid", ""));
                startActivity(intent);
                finish();
            }
            else if(currentActivity.equals("navigation")) {
                isResumed++;
                Intent intent = new Intent(MainActivity.this, NavigationActivity.class);
                fragment = sharedPreferences.getString("fragment", "Find Food");
                intent.putExtra("activity", fragment);
                startActivity(intent);
                finish();
            }
            else if(currentActivity.equals("adminaddnewmerchant"))
            {
                isResumed++;
                Intent intent = new Intent(MainActivity.this, AdminAddNewMerchantActivity.class);
                startActivity(intent);
                finish();
            }
            else if(currentActivity.equals("searchmerchants"))
            {
                isResumed++;
                Intent intent = new Intent(MainActivity.this, SearchMerchantsActivity.class);
                startActivity(intent);
                finish();
            }
            else if(currentActivity.equals("settingsuser"))
            {
                isResumed++;
                Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
                startActivity(intent);
                finish();
            }
            else if(currentActivity.equals("settingsmerchant"))
            {
                isResumed++;
                Intent intent = new Intent(this, SettingsMerchantActivity.class);
                startActivity(intent);
                finish();
            }
            else if(currentActivity.equals("sellerregistration"))
            {
                isResumed++;
                Intent intent = new Intent(MainActivity.this, SellerRegistrationActivity.class);
                startActivity(intent);
                finish();
            }
        }
        else{
            if(currentActivity.equals("sellerregistration"))
            {
                isResumed++;
                Intent intent = new Intent(MainActivity.this, SellerRegistrationActivity.class);
                startActivity(intent);
                finish();
            }
            else if(!sharedPreferences.getString("current activity", "main").equals("login user")
                    && !sharedPreferences.getString("current activity", "main").equals("login merchant"))
            {
                currentActivity = "main";

                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.clear();
                editor.putString(CURRENT_ACT,currentActivity);
                editor.commit();


                Paper.book().write(Prevalent.RememberMeMerchant, "false");
                Paper.book().write(Prevalent.emailKey, "UserEmail");
                Paper.book().write(Prevalent.passwordKey, "UserPassword");
            }
        }

        // Handle countdown start here
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("state","Stopping Main");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("state", "Destroyed Main");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("state", "Restarted Main");

    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("state", "Started Main");

    }

    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.d("state", "onRestoreInstanceState Main");
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.d("state", "onSaveInstanceState Main");
    }
//if the user

    @Override
    public void onBackPressed() {
        Log.d("state", "back login");

        currentActivity = "main";

        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(CURRENT_ACT,currentActivity);
        editor.commit();
    }
...

AdminAddNewMerchantActivity.java:

...
@Override
    public void onBackPressed()
    {
        if(drawer.isDrawerOpen(GravityCompat.START))
        {
            drawer.closeDrawer(GravityCompat.START);
        }
        else{
            super.onBackPressed();
        }
    }
@Override
    public void onResume() {
        super.onResume();  // Always call the superclass method first

        // Get the Camera instance as the activity achieves full user focus
        //if (mCamera == null) {
            //initializeCamera(); // Local method to handle camera init
        //}
    }
...

PriceFragment.java:

...
@Override
    public void onDestroy() {
        super.onDestroy();
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt("scrollPrice", scrollView.getScrollY());
        editor.commit();
    }

    @Override
    public void onResume() {
        Log.d("onResume", "Resumed");
        super.onResume();

    }
...

TimeFragment.java:

...
@Override
    public void onResume() {
        super.onResume();



    }
...

默认的 Android 行为应该会自动执行此操作...

您是否对 onPause 或 onStop 方法做了一些特殊的事情?

如果不是,能否通过 Android Studio 创建一个新项目并测试此行为是否在新应用程序上持续存在?