【问题标题】:onResume is Called everytime application is Opened每次打开应用程序时都会调用 onResume
【发布时间】:2018-02-01 08:30:16
【问题描述】:

我希望在登录后 onCreate 应该运行并在重新启动应用程序后 onResume 应该运行,但我面临一些问题。无法理解哪里出了问题是我的逻辑

public class EmployeeActivity extends AppCompatActivity implements AsyncResponse, AsyncResponseSecond {

boolean shouldExecuteOnResume;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_employee);

        name = findViewById(R.id.text_name);
        sp = getSharedPreferences("attendlogin", MODE_PRIVATE);
        String emp_name = sp.getString("name", null);
        name.setText(emp_name);

        in_time_button = findViewById(R.id.buttonlogin);
        out_time_button = findViewById(R.id.buttonlogout);
        close_button = findViewById(R.id.buttonclose);
        close_button.setEnabled(false);
        out_time_button.setEnabled(false);
        In_time = findViewById(R.id.text_in_time);
        Out_time = findViewById(R.id.text_out_time);

        shouldExecuteOnResume = false;
}

@Override
    protected void onResume() {
        if (shouldExecuteOnResume) {
            super.onResume();
            sp1 = getSharedPreferences("InTime", MODE_PRIVATE);
            String in_time_textView = sp1.getString("in_time_sp", null);
            In_time.setText(in_time_textView);
            in_time_button.setEnabled(false);
            out_time_button.setEnabled(true);
        } else {
            shouldExecuteOnResume = true;
        }
}

错误:

--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.rtos.rtos, PID: 26179
                  java.lang.RuntimeException: Unable to resume activity {com.example.rtos.rtos/com.example.rtos.rtos.EmployeeActivity}: android.util.SuperNotCalledException: Activity {com.example.rtos.rtos/com.example.rtos.rtos.EmployeeActivity} did not call through to super.onResume()
                      at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3421)
                      at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3461)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2730)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6123)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
                   Caused by: android.util.SuperNotCalledException: Activity {com.example.rtos.rtos/com.example.rtos.rtos.EmployeeActivity} did not call through to super.onResume()
                      at android.app.Activity.performResume(Activity.java:6778)
                      at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3398)
                      at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3461) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2730) 
                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6123) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 
Application terminated.

我正在使用此链接 (Android - Prevent onResume() function if Activity is loaded for the first time (without using SharedPreferences)) 作为参考,但我仍然收到 Error 。

【问题讨论】:

  • testcase : 在条件之外尝试这一行super.onResume();。
  • 第一次打开活动时,不会调用 onResume 的超级方法。正如@VishvaDave 所说,试试这个。
  • @VishvaDave 给出了完美的答案。实际上在某些回调方法中有一些规则,您必须在任何语句之前调用超级方法。
  • 是的,这是真的......这是一个非常小的错误,我忽略了......@Abdul Rizwan

标签: android android-lifecycle


【解决方案1】:

在 if 条件之外尝试super.onResume();。

@Override
    protected void onResume() {
        super.onResume();
        if (shouldExecuteOnResume) {
            sp1 = getSharedPreferences("InTime", MODE_PRIVATE);
            String in_time_textView = sp1.getString("in_time_sp", null);
            In_time.setText(in_time_textView);
            in_time_button.setEnabled(false);
            out_time_button.setEnabled(true);
        } else {
            shouldExecuteOnResume = true;
        }
}

【讨论】:

    【解决方案2】:

    用这个替换你的 onResume

    @Override
        protected void onResume() {
            if (shouldExecuteOnResume) {
                super.onResume();
                sp1 = getSharedPreferences("InTime", MODE_PRIVATE);
                String in_time_textView = sp1.getString("in_time_sp", null);
                In_time.setText(in_time_textView);
                in_time_button.setEnabled(false);
                out_time_button.setEnabled(true);
            } else {
                super.onResume();
                shouldExecuteOnResume = true;
            }
    }
    

    【讨论】:

      【解决方案3】:

      您需要确保在被覆盖的方法中有 super.onStop 和 super.onDestroy。对于您遇到的问题,这可能是一个很好的候选:

      @Override
      protected void onStop() {
      Log.w(TAG, "App stopped");
      super.onStop();
      }
      
      @Override
      protected void onDestroy() {
      Log.w(TAG, "App destroyed");
      super.onDestroy();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-07-05
        • 1970-01-01
        • 2014-09-09
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多