0

I want after Login onCreate Should Run and after restarting the Application onResume Should Run but i am facing some Issue . Not able to understand what is wrong is my logic

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;
        }
}

Error:

--------- 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.

I am Using THis Link (Android - Prevent onResume() function if Activity is loaded for the first time (without using SharedPreferences)) as a reference But Still I am getting Error .

Abhijit
  • 71
  • 7

3 Answers3

1

Replace your onResume with this

@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

You need to make sure that you have super.onStop and super.onDestroy in your overridden methods. This could be a good candidate for the problem you have:

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

@Override
protected void onDestroy() {
Log.w(TAG, "App destroyed");
super.onDestroy();
}
Uthaya
  • 363
  • 2
  • 15
1

Try super.onResume(); outside if condition.

@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;
        }
}
Vidhi Dave
  • 5,614
  • 2
  • 33
  • 55