好吧,这很奇怪。 我有两个 Activity (嗯,超过两个,但它们并不重要) - AppActivity 和 PopupActivity。 AppActivity 是我的主要应用程序 Activity ,它包含我的应用程序的设置。 PopupActivity 是一个当用户单击通知中的按钮(它是 RemoteViews 通知)时打开的对话框。嗯,它有效。效果很好。但前提是用户通过单击后退按钮关闭了AppActivity。如果他们点击home,PopupActivity 会在点击按钮几秒钟后打开。当用户使用主页按钮关闭 PopupActivity 时,也会发生同样的情况。单击通知中的按钮不会立即打开 PopupActivity,但需要几秒钟的时间才能终止仍存在于后台某处的上一个 Activity。
我尝试在 onStop 和 onPause 方法中调用 finish(),但它不能解决我的问题。
编辑:这是我的代码:
list :
<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( fragment ):
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需要很长时间。现在,原因是什么?
最佳答案
我认为您关注的是错误的问题(终止 Activity ),而不是关注真正的问题(10 秒重新启动它)。
首先,您需要了解如果您使用 home 键退出其他 Activity ,为什么需要 10 秒才能打开它!
如果您发布更多详细信息(带有源代码),它会更容易理解并为您提供帮助!
关于java - 当用户单击主页按钮时终止 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16757954/