我有一个Activity
和一个Service
。该活动用于搜索,该服务用于在Facebook聊天头等所有屏幕上显示悬停小部件。
当用户离开活动时,如按后退键,我启动服务,小部件开始悬停。在单击悬停窗口小部件时,使用FLAG_ACTIVITY_NEW_TASK
再次从服务重新启动活动。但是当我的活动与其他应用活动重叠时,执行此操作时,onStop()
和onPause()
都会被调用。调用onStop()
之上的活动时会调用onStop()
。
有人可以解释为什么会这样吗?提前谢谢。
活动代码:
public class CouponSearchActivity extends AppCompatActivity {
public static final String TAG = CouponSearchActivity.class.getSimpleName();
public static final String BROADCAST_ACTION_ACTIVITY_STARTED =
"activity_created";
public static final String BROADCAST_ACTION_ACTIVITY_STOPPED = "activity_stopped";
public static void startActivity(Context context){
Intent intent = new Intent(context, CouponSearchActivity.class);
context.startActivity(intent);
}
public static void startActivityFromService(Context context){
Intent intent = new Intent(context, CouponSearchActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("TAG",": onCreate()");
setContentView(R.layout.activity_coupon_search);
CouponWidgetService.stopService(this);
if(savedInstanceState == null){
initCouponSearchFragment();
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.d("TAG",": onNewIntent()");
}
private void initCouponSearchFragment(){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container, CouponSearchFragment.newInstance()).commit();
}
private void sendBroadcast(){
Intent intent = new Intent();
intent.setAction(BROADCAST_ACTION_ACTIVITY_STARTED);
sendBroadcast(intent);
Log.d("Broadcast sent", action = " +BROADCAST_ACTION_ACTIVITY_STARTED);
}
@Override
protected void onResume() {
super.onResume();
Log.d("TAG","onResume()");
sendBroadcast();
CouponWidgetService.stopService(this);
}
@Override
protected void onPause() {
super.onPause();
Log.d("TAG + "onPause()");
}
@Override
protected void onStop() {
super.onStop();
Intent intent = new Intent();
intent.setAction(BROADCAST_ACTION_ACTIVITY_STOPPED);
sendBroadcast(intent);
Log.d("TAG", "onStop()");
Log.d("Broadcast sent", action = " + BROADCAST_ACTION_ACTIVITY_STOPPED);
CouponWidgetService.startService(this, StoreCoupons.getStoreCouponsResponse(), StoreCoupons.getSelectedCategories());
}
@Override
public void onBackPressed() {
Log.d("onBackPressed()");
//CouponWidgetService.startService(this, StoreCoupons.getStoreCouponsResponse(), StoreCoupons.getSelectedCategories());
super.onBackPressed();
}
答案 0 :(得分:0)
但是,当我的活动与其他应用活动重叠时,请执行此操作 “onStop()”和“onPause”都没有被调用。
这对我来说似乎是一种合法的行为。如果Activity
与其他Activity
重叠,则会调用onPause
。如果“重叠”已完成(即您的Activity
不再可见),则还应调用其onStop
。
稍后,当您点击“悬停小部件”时,您的Activity
已经暂停(并且可能已停止),因此它不会再次获得这些生命周期回调。