好吧,这很奇怪。我有两个活动(嗯,不止两个,但它们并不重要) - AppActivity 和 PopupActivity。AppActivity 是我的主要应用程序活动,它包含我的应用程序的设置。PopupActivity 是一个对话框,当用户单击通知中的按钮时会打开(它是 RemoteViews 通知)。嗯,它有效。效果很好。但仅当用户通过单击后退按钮关闭 AppActivity 时。如果他们单击home,则 PopupActivity 在单击按钮后几秒钟后打开。当用户使用主页按钮关闭 PopupActivity 时,也会发生同样的事情。单击通知中的按钮不会立即打开 PopupActivity,但需要几秒钟才能杀死仍然存在于后台某处的上一个活动。
我尝试在 onStop 和 onPause 方法中调用 finish() ,但这并不能解决我的问题。
编辑:这是我的代码:
显现:
<activity
android:name="cc.lupine.quicksocial.AppActivity"
android:label="@string/app_name"
android:configChanges="orientation|screenSize" android:noHistory="true" android:clearTaskOnLaunch="true" android:finishOnTaskLaunch="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:configChanges="orientation|screenSize"
android:excludeFromRecents="true"
android:showOnLockScreen="false"
android:theme="@android:style/Theme.Holo.Light.Dialog.NoActionBar.MinWidth"
android:name="cc.lupine.quicksocial.PopupActivity"
android:noHistory="true"
android:launchMode="singleInstance"
android:taskAffinity=""
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<service android:name="cc.lupine.quicksocial.ShareService"></service>
ShareService.java(只是当用户单击通知中的按钮时调用的函数):
public static void startSharing(Context ctx, int n) {
Log.d("sn", "startsharing called in shareservice");
if(n == 1 || n == 2)
{
Intent i = new Intent(ThisApplication.getAppContext(), PopupActivity.class);
i.putExtra("shareType", n);
i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY|
Intent.FLAG_ACTIVITY_CLEAR_TOP|
Intent.FLAG_ACTIVITY_SINGLE_TOP|
Intent.FLAG_ACTIVITY_CLEAR_TASK|
Intent.FLAG_FROM_BACKGROUND|
Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(i);
} else if(n == 3) {
// doesn't matter for now
}
}
PopupActivity.java(片段):
public class PopupActivity extends Activity implements OnDataPass {
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("sn", "oncreate called in popupactivity");
super.onCreate(savedInstanceState);
instanceOfPopupActivity = this;
setContentView(R.layout.activity_popup);
ShareFragment sfrag = new ShareFragment();
Bundle args = new Bundle();
Bundle extras = getIntent().getExtras();
try {
//doesn't matter
} catch(Exception e) { e.printStackTrace(); finish(); }
sfrag.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.contFragment, sfrag);
fragmentTransaction.commit();
}
@Override
public void onPause()
{
Log.d("sn", "onpause called in popupactivity");
finish();
super.onPause();
}
@Override
public void onStop() {
Log.d("sn", "onstop called in popupactivity");
super.onStop();
}
@Override
public void onDestroy()
{
Log.d("sn", "ondestroy called in popupactivity");
super.onDestroy();
}
}
如果我第一次打开弹出窗口:
05-26 14:37:14.149: D/sn(7218): startsharing called in shareservice
05-26 14:37:14.179: D/sn(7218): oncreate called in popupactivity
但是当我用主页按钮关闭弹出窗口并尝试再次打开它时:
05-26 14:38:11.620: D/sn(7218): startsharing called in shareservice
05-26 14:38:14.103: D/sn(7218): oncreate called in popupactivity
调用 onCreate 需要很长时间。而现在,原因是什么?