首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >每次打开应用程序时都会调用onResume。

每次打开应用程序时都会调用onResume。
EN

Stack Overflow用户
提问于 2018-02-01 08:30:16
回答 3查看 632关注 0票数 0

我希望在登录之后,onCreate应该运行,在重新启动应用程序之后,onResume应该运行,但是我面临一些问题。我的逻辑是不能理解什么是错的

代码语言:javascript
复制
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;
        }
}

错误:

代码语言:javascript
复制
--------- 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.

我正在使用THis链接(Android - Prevent onResume() function if Activity is loaded for the first time (without using SharedPreferences))作为参考,但仍然会出现错误。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-02-01 08:39:19

如果条件在外部,请尝试super.onResume();

代码语言:javascript
复制
@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;
        }
}
票数 1
EN

Stack Overflow用户

发布于 2018-02-01 08:36:50

将您的onResume替换为

代码语言:javascript
复制
@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;
        }
}
票数 1
EN

Stack Overflow用户

发布于 2018-02-01 08:37:32

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

代码语言:javascript
复制
@Override
protected void onStop() {
Log.w(TAG, "App stopped");
super.onStop();
}

@Override
protected void onDestroy() {
Log.w(TAG, "App destroyed");
super.onDestroy();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48558331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档