我有一个带有 2 个活动(比如 A 和 B)和一个 appWidget 的小应用程序。当我单击小部件时,我启动了一个活动,该活动在做一些事情的同时为小部件设置动画。我是这样做的;
Intent intent = new Intent(context, DialogActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
RemoteViews.setOnClickPendingIntent(R.id.imageButton1, pendingIntent);
然后中的活动onCreate
这样做:
Paint p = new Paint();
p.setStyle(Paint.Style.FILL);
p.setColor(Color.TRANSPARENT);
logger.debug("inizializzo movie"); //$NON-NLS-1$
Movie movie;
InputStream is = null;
long moviestart = 0;
is = this.getResources().openRawResource(R.drawable.gifanimata);
movie = Movie.decodeStream(is);
taskIsrunning = true;
//this is an AsyncTask that at the end of the work sets taskIsrunning = false
new InitTask().execute(this);
while (taskIsrunning) {
Bitmap bitmap = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawRect(0, 0, 100, 100, p);
long now = android.os.SystemClock.uptimeMillis();
if (moviestart == 0) { // first time
moviestart = now;
}
int relTime = (int) ((now - moviestart) % movie.duration());
movie.setTime(relTime);
movie.draw(canvas, 0, 0);
updateViews.setImageViewBitmap(R.id.imageButton1, bitmap);
widgetManager.updateAppWidget(theWidget, updateViews);
try {
Thread.sleep(500);
} catch (Exception e) {
throw e;
}
}
最后,从这个活动(女巫是完全透明的)我显示了一个对话框。这就像我想要的那样工作,但问题是:
当我启动活动时,如果之前我打开了应用程序的第一个活动(活动 A)并使用主页按钮将其关闭(活动仍然在堆栈中)而不是使用后退按钮关闭它,则显示对话框的那一刻从小部件启动的活动中,显示了对话框,但也显示了活动 A。
我尝试在清单 android:noHistory="true" 上设置,问题解决了,但是如果从活动 A 我启动活动 B,后退按钮将不会显示活动 A。