1 year ago

#324613

test-img

Jayesh M

Android - Why All variables, views declared on Activity is getting null in onRestart() method after screen unlock on Android OS 11?

I am trying to develop a application but I got stuck in a case that I mentioned below :

1. I am in an Activity of my application.

2. My screen turned off (locked) after 5 min

3. I unlock mobile

4. After unlock, onRestart() method on Activity called and I have done some code on onRestart() method

5. But all variable and views on onRestart() is getting null when I debugged the app

My code in onRestart() method.

@Override
protected void onRestart() {
    super.onRestart();
    try {

        if (new CommonUtils()
                .isConnectingToInternet(getApplicationContext()))
            new UpdatetoServerAsynk(getApplicationContext()).execute();

        GlobalData globalData = (GlobalData) getApplicationContext();
        CommonDomain serverLogin = globalData.getServercredentials();

        if (serverLogin == null || !serverLogin.Date.equals(new CommonUtils().getCurrentDate())) {
            new CommonUtils().forceLogout(this);
        }

        if (globalData.getUserdetails().TimeInOutFlag == GlobalData.PUNCHED_IN)
            mUsernameTextView.setTextColor(Color.parseColor(GlobalData.GREEN));
        else
            mUsernameTextView.setTextColor(mColorStateList);

      
        new myAsync(HomescreenActivity.this).execute();

    } catch (Exception e) {

        e.printStackTrace();
    }
}

All data is showing null even the views. I don't know why its all went to null. And its getting only in Android 11 OS. My target & Compiled SDK is 31. I checked whether it occur due to low memory and its not. Is anyone have this kind of problem while developing If so any solution or any idea why this is happening?

android

nullpointerexception

android-11

activity-lifecycle

0 Answers

Your Answer

Accepted video resources