Okey,这很奇怪。 我有两个活动(嗯,超过两个,但他们并不重要) - AppActivity和PopupActivity。 AppActivity是我的主要应用程序活动,它包含我的应用程序的设置。 PopupActivity是一个对话框,当用户单击通知中的按钮时(这是RemoteViews通知),它将被打开。那么,它的作品。很棒。但只有当用户通过单击后按钮关闭AppActivity时。如果他们点击主页,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(fragments):
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被调用。现在,这是什么原因?
我已经尝试在onPause和onStop中,在AppActivity,PopupActivity和两者中调用finish()。 onPause/onStop在每次home按钮被触发时被触发,finish()被调用,但它仍然不能解决我的问题 –
System.err.println(“HI!”);把它放在方法检测HOME按钮点击,anw手表日志猫。我认为你没有正确地检测到它。如果调用完成关闭应用程序,则无法工作。 – Dejan
我没有检测到主页按钮点击。如果用户使用多任务菜单切换到任何其他应用程序,这将非常棘手,并且不会涵盖该操作。这就是为什么我在onPause或onStop中调用'finish()' - 我想在每次离开前景时销毁该活动。 –