Run App Twice To Work

前端 未结 4 1109

I\'m making an android app that test if certain security features on your phone are enabled. For example, if you have password log in enabled or if your data is encrypted on you

4条回答
  •  被撕碎了的回忆
    2021-02-05 08:13

    Here is good explanation how app lifecycle flows. onStart() can be executed many times. You can keep counter how many times you had entered this method and act differently on each time:

     static int counter=0;
     public void onStart()
        {
          counter++;
          Log.i("MyApp", "onStart() run "+counter);
          switch (counter){
            case 1: break; // first run
            case 2: break; // second run
            default: break;// other runs
          }
     }
    

    To be more clear about life cycle and why your onStart() method is called twice I suggest to have counter and Log.i() in each important state of the cycle - at least in onCreate() and onRestart().

    Keep in mind that app stays in memory when you click Home button. When you click app icon again it restarts already running app (calls onRestart() and then onStart() methods and no onCreate() ). When you really kill you app for real then sequence would be onCreate and onStart without onRestart. Having logcat records really helps you to understand app lifecycle flow and why your onStart() is called twice or more times.

提交回复
热议问题