0

there is Class Named "Configs.java" contains :

    public Contect context;
    public Activity currentActivity;

On Global Class Named "App.java" :

    public static Configs configs = new Configs();

On Main Activity Java Class Oncreate :

    App.configs.context = getApplicationContext();
    App.configs.currentActivity = this;

On Products Activity Java Class Oncreate (Main Activity Not Finished And Must Work In Background):

    App.configs.currentActivity = this;

After Long Time Minimized , it shows An Error On Bellow Line From Products Activity Java Class :

    App.configs.currentActivity = this;

NullPointerException

Unable To Use "Bundle savedInstanceState" Because It's Activity Valiable

Hossein Kurd
  • 3,184
  • 3
  • 41
  • 71

2 Answers2

0

Try moving this code to onResume() instead of onCreate() and see if the error still exists.

0

Using static variables on Android is no good. This post explains that Android is very likely to kill your app (kills your Application instance, hence the static variable becomes null when your app is recreated and the class loader reinstantiates your vars) to save memory. The post also refers to Saving Activity state in Android which offers an alternative to save stuff with Bundles.

Anyway, if you need a quick solution, make your Configs class a singleton, and use getters and setters instead of making it public. Make the getter check if your singleton is null, and in this case, instantiate a new and return it. You won't get NPEs but it is very risky since it won't restore the previous state.

Community
  • 1
  • 1
walljam7
  • 343
  • 2
  • 11