问题
I have two activities Activity2
and TestActivity
. Activity2
shows notification when launched. TestActivity
is launched when notification is clicked. Below is the code of Activity2.java
.
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.droidapp.apptest.R;
public class Activity2 extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
Log.w("app_test", "onCreate()");
showNotification();
}
@Override
protected void onStart() {
Log.w("app_test", "onStart()");
super.onStart();
}
@Override
protected void onRestart() {
Log.w("app_test", "onRestart()");
super.onRestart();
}
@Override
protected void onResume() {
Log.w("app_test", "onResume()");
super.onResume();
}
@Override
protected void onPause() {
Log.w("app_test", "onPause()");
super.onPause();
}
@Override
protected void onStop() {
Log.w("app_test", "onStop()");
super.onStop();
}
@Override
protected void onDestroy() {
Log.w("app_test", "onDestroy()");
super.onDestroy();
}
public void showNotification() {
Intent intent = new Intent(this, TestActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "CHANNEL_ID")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Title")
.setContentText("Content text")
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat.from(this).notify(102, mBuilder.build());
}
}
Below all lifecycle methods belongs to Activity2
.
When Activity2 first time launched. Here onCreate()
notification is shown.
W/app_test: onCreate()
W/app_test: onStart()
W/app_test: onResume()
After pressing home button
W/app_test: onPause()
W/app_test: onStop()
After clicking notification TestActivity
is launched. Activity2
lifecycle methods aren't called. Everything is working as expected.
If instead of pressing home button, I press power button
W/app_test: onPause()
W/app_test: onStop()
Then click notification from lock screen Activity2
lifecycle methods are called.
W/app_test: onRestart()
W/app_test: onStart()
W/app_test: onStop()
And then TestActivity
is launched. Here strange thing is Activity2
onResume()
isn't called.
But if I don't click notification, and just unlock screen by swiping onResume()
is called.
W/app_test: onRestart()
W/app_test: onStart()
W/app_test: onResume()
In my real application I register BroadcastReceiver
on activities onResume()
state and unregister it onPause()
. Unregistering BroadcastReceiver
which wasn't registered causes IllegalStateException
. Now I'm avoiding this exception by putting boolean
variable.
Question. Is it expected behaviour or bug in android framework?
Edit. Even more strange thing. If I first press home button, then power button and then click notification from lock screen.
W/app_test: onRestart()
W/app_test: onStart()
W/app_test: onResume()
W/app_test: onPause()
W/app_test: onStop()
And then TestActivity
is launched.
activity properties in manifest
<activity
android:name=".activities.Activity2"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".activities.TestActivity"/>
回答1:
This is as per expected behavior since when you click the notification from lock screen Activity2
is not visible and goes to background behind TestActivity
screen and hence its onResume()
won't be called. However, when you don't click the notification and just unlock your phone, Activity2
gets visible and its onResume()
will be called. Hope it helps.
来源:https://stackoverflow.com/questions/50167202/current-activities-onresume-is-not-called-when-another-activity-is-launched-fr