5

EDIT* This issue is occurring while testing on a android device. This issue does not appear while testing on a emultor.


I am starting a new activity and I see onPause is called right after onResume is called. If I look at log it goes to Idle after onResume. Therefore causing onPause right after onResume is called.

Caller activity - calling MainActivity on onClick via intent.

public class TestActivity extends AppCompatActivity implements View.OnClickListener{

    String TAG = "acr";
    Button testBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        testBtn = (Button) findViewById(R.id.testBtn);
        testBtn.setOnClickListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "on pause called on TestActivity ");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "on resume called on  TestActivity ");
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.testBtn:
                Intent mainIntent = new Intent(this, MainActivity.class);
                TestActivity.this.startActivity(mainIntent);
                break;
        }
    }
}

Activity that has bug

public class MainActivity extends AppCompatActivity{

    public static final String TAG = "acrx";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "on pause called on mainactivity");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "on resume Called on Main activity");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "on stop Called on Main activity");
    }
}

Log

12-06 23:24:19.751 22983-22983/com.example.m1alesis.smartcardreader I/acrx: on resume Called on Main activity
12-06 23:24:19.751 22983-22983/com.example.m1alesis.smartcardreader D/SecWifiDisplayUtil: Metadata value : SecSettings2
12-06 23:24:19.751 22983-22983/com.example.m1alesis.smartcardreader D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{5ce8491 I.E...... R.....ID 0,0-0,0}
12-06 23:24:19.781 22983-23012/com.example.m1alesis.smartcardreader D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000,  [1440x2560]-format:1
12-06 23:24:19.811 22983-22983/com.example.m1alesis.smartcardreader W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
12-06 23:24:19.831 22983-22983/com.example.m1alesis.smartcardreader D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=1
12-06 23:24:19.871 22983-23012/com.example.m1alesis.smartcardreader D/OpenGLRenderer: endAllActiveAnimators on 0x7f9c17ec00 (RippleDrawable) with handle 0x7f9ccc8b60
12-06 23:24:19.871 22983-22983/com.example.m1alesis.smartcardreader I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@fa2f707 time:376798424
12-06 23:24:20.131 22983-22983/com.example.m1alesis.smartcardreader V/ActivityThread: updateVisibility : ActivityRecord{e78cff6 token=android.os.BinderProxy@a67fd36 {com.example.m1alesis.smartcardreader/com.example.m1alesis.smartcardreader.TestActivity}} show : false
12-06 23:24:31.561 22983-22983/com.example.m1alesis.smartcardreader I/acrx: on pause called on mainactivity
12-06 23:24:31.701 22983-22983/com.example.m1alesis.smartcardreader I/acrx: on resume Called on Main activity
12-06 23:24:31.721 22983-22983/com.example.m1alesis.smartcardreader I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@fa2f707 time:376810271
m1alesis
  • 670
  • 6
  • 16
  • its definitely against the android activity cycle.. it maybe its a bug. did you open any apps after your app is launched? – ZeroOne Dec 07 '16 at 06:59
  • I know that this kind of temporary pause may happen if a result needs to be delivered to an Activity, but it is already resumed. This is not the case here, but maybe this kind of pausing can happen in other cases as well. – Marcin Jedynak Dec 07 '16 at 08:54
  • @MuhammadHafiqIqmal I have not opened any other apps. It happens immediately. Within 1 sec after the onResume is called. – m1alesis Dec 07 '16 at 09:17

3 Answers3

3

I had the same issue and for me it is related to the activity starting up and the screen not being on - I only saw it after I had the screen off (e.g. after touching the power button and then home to resume again).

This is a behaviour that others have seen also - I found this question and answer has useful references: OnPause and OnStop() called immediately after starting activity

The key quote that helped me understand the behaviour was:

Keep in mind that onResume is not the best indicator that your activity is visible to the user; a system window such as the keyguard may be in front. Use onWindowFocusChanged(boolean) to know for certain that your activity is visible to the user

Community
  • 1
  • 1
Mick
  • 24,231
  • 1
  • 54
  • 120
3

In my case the same issue was caused by ActivityCompat.requestPermissions(this, new String[]{Manifest.permission...);.

 * This method may start an activity allowing the user to choose which permissions
 * to grant and which to reject. Hence, you should be prepared that your activity
 * may be paused and resumed. Further, granting some permissions may require
 * a restart of you application. In such a case, the system will recreate the
 * activity stack before delivering the result to your
 * {@link OnRequestPermissionsResultCallback#onRequestPermissionsResult(int, String[], int[])}.

This slowed down the app start noticeably.

einUsername
  • 1,569
  • 2
  • 15
  • 26
0

I tried it in my test activity and I did not get your problem. I had only one button on my MainActivity and my TestActivity.

I have found something here: Pausing and Resuming an Activity.

This is the interesting part:

Note: When the system calls your activity's onPause() method, the system may be signaling that the activity will be paused for a moment and the user may return focus to your activity, or that the app is running in multi-window mode. However, this method call may also be the first indication that the user is leaving your activity.

Zoltan Febert
  • 471
  • 5
  • 9
  • thats strange, I created new project to test it but still same problem. Tested on my 2 samsung phones both with same bug. Do you think this is because of the phone and not the code? – m1alesis Dec 07 '16 at 00:23
  • I have a Samsung Galaxy Tab E-Lite tablet. I tried it in debug and normal mode. So I have no idea. – Zoltan Febert Dec 07 '16 at 00:58
  • @m1alesis, Have you found any solution? I was thinking a lot about it. If you send me your test project I would try it on my tablet. – Zoltan Febert Dec 07 '16 at 15:10
  • Unfortunately no solution yet but I recently tested the code on emulator and had no issue but I'm having the issue on phone. I have tested in 2 phones so far and both has issue. – m1alesis Dec 07 '16 at 20:15